how to draw a 'x' shape like below - python

I am new to python, please someone help to draw a shape like below. The program have to take the number of star as an input.
number of star = 7
* *
** **
*** ***
********
*** ***
** **
* *
My code:
count = int(input('star count : '))
pattern_size = count + 1
for t in range(1, pattern_size):
pattern = list(" " * pattern_size)
pattern[:t] = "*" * t
pattern[-t:] = '*' * t
print(''.join(pattern))
star count : 9
* *
** **
*** ***
**** ****
**********
**********
**********
**********
**********

Here's a easy-to-understand example using two for-loops with your approach: One just the inverse of another
count = int(input('star count : '))
pattern_size = count + 1
for t in range(1, int(pattern_size/2)):
pattern = list(" " * pattern_size)
pattern[:t] = "*" * t
pattern[-t:] = '*' * t
print(''.join(pattern))
for t in range(int(pattern_size/2), 0, -1):
pattern = list(" " * pattern_size)
pattern[:t] = "*" * t
pattern[-t:] = '*' * t
print(''.join(pattern))
And a more advanced method would be to use the string method .center() like so:
count = int(input('star count : '))
pattern_size = count + 1
for t in range(1, int(pattern_size/2)):
print((" " * (pattern_size-t*2)).center(pattern_size, '*'))
for t in range(int(pattern_size/2), 0, -1):
print((" " * (pattern_size - t * 2)).center(pattern_size, '*'))

Just like my comment, this is the complete code:
count = int(input('star count : '))
pattern_size = count + 1
for t in range(1, pattern_size):
pattern = list(" " * pattern_size)
len_t = t if t <= (pattern_size / 2) else (pattern_size - t)
pattern[:len_t] = "*" * len_t
pattern[-len_t:] = '*' * len_t
print(''.join(pattern))
Hope this help.

Related

printing shapes in one line in python

I am trying to print 2 circles pattern in one row and two in the next row like this
Here is my Code:
cell = {}
row = 5
col = 5
for i in range(0,row):
for j in range(0,col):
if((j == 0 or j == col-1) and (i!=0 and i!=row-1)) :
cell[(i,j)] = '*'
#end='' so that print statement should not change the line.
elif( ((i==0 or i==row-1) and (j>0 and j<col-1))):
cell[(i,j)] = '*'
else:
cell[(i,j)] = " "
print(cell[(i, j)], end=" ")
print(end='\n')
And with this code I'm getting the output as follows:
what should I change in this code to make it correct?
You essentially need a template for top/bottom of a circle and the middle part of a circle.
Then you need to print enough of them per line:
nums = 7 # only squares supported
amount = 3 # shapes per line & shape rows in total
spacer = 2 # space horizontally, vertically it is 1 line
# prepare shapes
top_botton = f" {'*'*(nums-2)} "
middle = f"*{' '*(nums-2)}*"
space_h = " " * spacer
for row in range(amount * nums):
# detect which row we are in
mod_row = row % nums
# bottom or top of row
if mod_row in (0, nums-1):
print(*([top_botton]*amount), sep=space_h )
if mod_row == nums-1:
print()
# middle of row
else:
print(*([middle]*amount), sep=space_h)
Output:
# nums = 5, count = 2
*** ***
* * * *
* * * *
* * * *
*** ***
*** ***
* * * *
* * * *
* * * *
*** ***
# nums = 7, count = 3
***** ***** *****
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
***** ***** *****
***** ***** *****
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
***** ***** *****
***** ***** *****
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
***** ***** *****
The distance between the circles is handled by the sep=... of the print statement. It prints the decomposed list of (amount) shapes.
You could as well handle a "single char" printer like you did for your single cirle, but all those loops in loops and values modular checking are getting confusing fast.
for i in range(0,row):
for j in range(0,col):
if((j == 1 or j == col-1) and (i!=0 and i!=row+1)) :
cell[(i,j)] = ''
elif( ((i==0 or i==row-1) and (j>0 and j<col+1))):
cell[(i,j)] = ''
else:
cell[(i,j)] = " "
print(cell[(i,j)], end=" ")
print(end='\n')

How to transform this code that prints a hollow diamond to solid diamond?

Hello I'm fairly new to python and have a question I'm stuck on:
This is the original code to print a hollow diamond:
def print_diamond(height):
"""prints hollow diamond"""
print("{:^{}}".format("*", height))
for i in range(1, height // 2):
print("{:^{}}".format("*" + " " * (2*i - 1) + "*", height))
for i in range(height // 2, 0, -1):
print("{:^{}}".format("*" + " " * (2*i - 1) + "*", height))
print("{:^{}}".format("*", height))
#test code
print_diamond(5)
print_diamond(3)
print_diamond(7)
This outputs:
*
* *
* *
* *
*
*
* *
*
*
* *
* *
* *
* *
* *
*
How would you transform this code to print a solid diamond?
*
*
***
*
*
***
*****
***
*
*
***
*****
*******
*****
***
*
Using these tests:
print_diamond(1)
print_diamond(2)
print_diamond(3)
print_diamond(4)
I tried changing the formatting by adding " * " but that pushes the other " * " on the side. Thank you.
Adding " * " to the spaces " " does not fix the problem as the formating does not work with solid diamond test code.
It shouldn't output this currently:
*
*
*
***
*
*
***
*
Just replace " " with "*" in the following lines :
print("{:^{}}".format("*" + "*" * (2*i - 1) + "*", height))

How to alternatively print 2 different strings while using end=''?

I am trying to print these two strings alternatively, but cannot find a way around it without using end=''. I am trying to use as little strings as possible, my goal is to use only 2 strings.
num_of_stars = int(input("How many stars would you like on each line?:
"))
lines1 = 0
lines2 = 0
alternator = 0
def line1(num_of_stars):
for i in range(num_of_stars):
print("*", end=' ')
def line2(num_of_stars):
for i in range(num_of_stars):
print(" *", end='')
while lines1 <= 4 and lines2 <= 4:
if alternator == 0:
line1(num_of_stars)
lines1 += 1
alternator = 1
elif alternator == 1:
line2(num_of_stars)
lines2 += 1
alternator = 0
My results have just been this: * * * * * * * * * * * ** * * * * * * * * * * ** * * * * * * * * * * ** * * * * * * * * * * ** * * * * *
It's not clear what you want.
But you probably don't want to use the print( ... , end=...) technique
shown in your code.
It would be much simpler to assign
stars = ['*'] * num_of_stars
line = ' '.join(stars)
and then
for i in range(4):
indent = ' '[:i % 2]
print(indent + line)
The indent string will be empty or a blank,
according to whether i is even or odd.
First of all, the end of the line 2 function is just a blank space, which explains why there are groups of 2 stars grouped together at intervals.
Try this:
num_of_stars = int(input("How many stars would you like on each line?: "))
lines1 = 0
lines2 = 0
alternator = 0
def line1(num_of_stars):
for i in range(num_of_stars):
print("*", end="")
print("\n")
def line2(num_of_stars):
for i in range(num_of_stars):
print("*", end="")
print("\n")
while lines1 <= 4 and lines2 <= 4:
if alternator == 0:
line1(num_of_stars)
lines1 += 1
alternator = 1
elif alternator == 1:
line2(num_of_stars)
lines2 += 1
alternator = 0

Printing a simple rhombus pattern in Python 3

I want to print this pattern in Python 3 (I'm a beginner):
What i have tried :
n = 5
for x in range(1, (n+5) //2 + 1):
for y in range( (n+5) //2 - x):
print(" ", end = "")
for z in range( (x*2)-1 ):
print("*", end = "")
print()
for x in range( (n+5)// 2 + 1, n + 5):
for y in range(x - (n+5) //2):
print(" ", end = "")
for z in range( (n+5 - x) *2 - 1):
print("*", end = "")
print()
But the result is like this:
How can I make the middle hollow like in the image?
Thanks.
The coordinates of the points on the hollow rhombus satisfies |x|+|y|==m (where m = n-1). Therefore, you could use
In [29]: m = n-1; print('\n'.join([''.join(['*' if abs(row)+abs(col)==m else ' ' for col in range(-m,m+1)]) for row in range(-m,m+1)]))
*
* *
* *
* *
* *
* *
* *
* *
*
or, equivalently, but without list comprehensions:
n = 5
m = n-1
for row in range(-m, m+1):
for col in range(-m, m+1):
if abs(row) + abs(col) == m:
c = '*'
else:
c = ' '
print(c, end='')
print()
To make the solid rhombus, simply change the condition to abs(row) + abs(col) <= m.
With recursion:
def print_stars(i,n):
if i:
print( ' '*(n-i-1) + '*' + ' '*(2*i-1) + '*')
else:
print( ' '*(n-1) + '*')
# recursive
def r(n, i=0):
print_stars(i,n) # up
if i<n-1:
r(n, i+1) # recurse
print_stars(i,n) # down
# start
r(5)
prints:
*
* *
* *
* *
* *
* *
* *
* *
*
Simple python code without using functions
rows = 7
for i in range(1, rows + 1):
for j in range(1, rows - i + 1):
print(end = ' ')
for k in range(1, 2 * i):
if k == 1 or k == i * 2 - 1:
print('*', end = '')
else:
print(' ', end = '')
print()
for i in range(rows - 1, 0, -1):
for j in range(1, rows - i + 1):
print(' ', end = '')
for k in range(1, 2 * i):
if k == 1 or k == i * 2 - 1:
print('*', end = '')
else:
print(' ', end = '')
print()
Output:
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
How to make a "Hollow Diamond In Python 3.10"
You can use this code here:
my personal code

How to make an unfilled diamond using while loops in python

I can only create a filled diamond, i can't figure out how to get it unfilled.`#
# The size of the diamond
N = 7
# The top part (includes the middle row)
i = 0
while i < N // 2 + 1:
print((N // 2 - i) * " " + (2 * i + 1) * "*")
i += 1
# The bottom part
i = 0
while i < N // 2:
print(" " * (i + 1) + "*" * (N - 2 - 2 * i))
i += 1
You just need to print (2*i-1) spaces in between '*' characters instead instead of only '*'. And have to deal with the very top and very bottom separately:
# The size of the diamond
N = 7
# The top part (includes the middle row)
print((N // 2) * " " + '*')
i = 1
while i < N // 2 + 1:
print((N // 2 - i) * " " + '*' + (2 * i - 1) * " " + '*')
i += 1
# The bottom part
i = 0
while i < N // 2 - 1:
print(" " * (i + 1) + '*' + " " * (N - 4 - 2 * i) + '*')
i += 1
print((N // 2) * " " + '*')
*
* *
* *
* *
* *
* *
*
# The size of the diamond
N = 7
# The top part (includes the middle row)
i = 0
while i < N // 2 + 1:
print((N // 2 - i) * " " + "*" +( ((2 * i -1) * " " + "*") if i > 0 else ""))
i += 1
# The bottom part
i = 0
while i < N // 2:
print(" " * (i + 1) + "*" + ( ( " " * (N - 2 - 2 * i - 2) + "*") if i < (N//2-1) else ""))
i += 1
simply print two less spaces than you were printing *'s inbetween two *'s unless on top or bottom.
def diamond(size, sym_func):
s = ''
for row in xrange(size):
for column in xrange(size):
if row > size//2: # if bottom half reflect top
row = size - row - 1
if column > size//2: # if right half reflect left
column = size - column - 1
s += sym_func(row,column,size)
s+= '\n'
return s
def solid(row,column,size):
if column >= (size // 2 - row):
return "*"
return " "
def hollow(row,column,size):
if column == (size // 2 - row):
return "*"
return " "
def circle(row,column,size):
if (size//2-row)**2+(size//2-column)**2 <= (size//2)**2:
return '*'
return ' '
print diamond(size=7, sym_func=solid) # The size of the diamond
print diamond(size=7, sym_func=hollow) # The size of the diamond
print diamond(size=17, sym_func=circle) # The size of the diamond
look at the difference between the hollow and solid symbol functions if you use a >= then you get a solid thing if you use a == for exact comperison then it is just the parimiter

Categories

Resources