I would like to print the following pattern in Python 3.5 (I\'m new to coding):
*
***
*****
*******
*********
*******
*****
***
*
print('This is in python 3.7')
h=eval(input('Enter the diagonal?'))
j=1
for i in range(h,h//2,-1):
print(' '*(i-(h//2)-1),'*'*j)
j+=2
j-=4
for i in range(1,(h//2)+1,1):
print(' '*i,'*'*(j))
j-=2
a = 10
for x in range (a):
print(" " * (a - x) + "*" * (x+1) + "*" *(x))
#+ this = diamond
for x in reversed(range(a-1)):
print(" " * (a - x) + "*" * (x) + "*" *(x+1))
Another possibility. Depending on which (space or star) one uses, (I used space) convert it to absolute value. This implementation doesn't require splitting the diamond into two loops (upper and lower halves).
def diamond(n):
star = 1
main = ''
# if required to manage zero or negative n
if n%2 == 0:
return None
if n<0:
return None
else:
for i in range(1,n+1):
string = ''
space = abs(i - int((n+1)/2))
star = n - 2 * space
string = space * ' ' + star * '*' + '\n'
main += string
# not necessary but useful to visualize diamond
#print(main)
return(main)
#coder_rishabh_darmwal
#it_is_a_simple_codewith_an_easy_logic
row=int(input('enter the no. of rows')
for i in range(row):
if i<=row//2:
for j in range(row//2-i):
print(" ",end='')
for k in range(i*2-1):
print("*",end="")
print()
else:
for j in range(i-row//2):
print(" ",end="")
for k in range((row-i)*2-1):
print("*",end="")
print()
#the output will be
[output for row=30][1]
#i also wrote a programme fro hollow diamonds
row=int(input('enter the no. of rows')
for i in range(row):
if i<=row//2:
for j in range(row//2-i):
print(" ",end='')
for k in range(i*2-1):
if k==0 or k==i*2-2:
print("*",end="")
else:
print(' ',end='')
print()
else:
for j in range(i-row//2):
print(" ",end="")
for k in range((row-i)*2-1):
if k==0 or k==(row-i)*2-2:
print("*",end="")
else:
print(' ',end="")
print()
[out for hollow rhombus row=20
][2]
[1]: https://i.stack.imgur.com/3j0bx.png
[2]: https://i.stack.imgur.com/tCxI3.png
#author Tahir Baku
#have fun
print "\nWelcome to diamond builder"
print "\n----D.I.A.M.O.N.D B.U.I.L.D----"
diagonal=int(input("Give me the diagonal: "))
s=1
h1=1
h=(diagonal-1)/2
diagonal1=diagonal-2
while s<=diagonal:
print (' '*h+'*'*s+' '*h)
h=h-1
s=s+2
while diagonal1>=0:
print (' '*h1+'*'*diagonal1+' '*h1)
h1=h1+1
diagonal1=diagonal1-2
I learned a very simple solution today and would like to share it. :)
num = 9
for i in range(1, num+1):
i = i - (num//2 +1)
if i < 0:
i = -i
print(" " * i + "*" * (num - i*2) + " "*i)
The logic is the following:
(A space is represented as "0" here.)
# i = 1 | new i = 1 - 5 = -4 | * : 9 - 4 = 1 | 0000 + * + 0000
# i = 2 | new i = 2 - 5 = -3 | * : 9 - 3 = 3 | 000 + *** + 000
# i = 3 | new i = 3 - 5 = -2 | * : 9 - 2 = 5 | 00 + ***** + 00
# i = 4 | new i = 4 - 5 = -1 | * : 9 - 1 = 7 | 0 + ******* + 0
# i = 5 | new i = 5 - 5 = 0 | * : 9 - 0 = 9 | *********
# i = 6 | new i = 6 - 5 = 1 | * : 9 - 1 = 7 | 0 + ******* + 0
# i = 7 | new i = 7 - 5 = 2 | * : 9 - 2 = 5 | 00 + ***** + 00
# i = 8 | new i = 8 - 5 = 3 | * : 9 - 3 = 3 | 000 + *** + 000
# i = 9 | new i = 9 - 5 = 4 | * : 9 - 4 = 1 | 0000 + * + 0000
The result would be the following:
*
***
*****
*******
*********
*******
*****
***
*