Question #266258

Given an integer value N as input, write a program to print a shaded diamond 2*N -1 rows using an asterisk ( * ).


Note: There is a space after each asterisk ( * ) character.


Example N = 5. There for the output should be:


*

* *

* * *

* * * *

* * * * *

* *

* *

* *

*


Expert's answer

N = int(input())
diamond = []
for i in range(1, N + 1):
    line = ' '.join(['*' for x in range(i)])
    diamond.append(line.center(2 * N, ' ')) 
for i in range(N - 1, 0, -1):
    line = ' '.join(['*' for x in range(i)])
    if i == 1:
        line = line[::-2]
    diamond.append(line.center(2 * N, ' '))
print('\n'.join(diamond))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS