Number Diamond
Given an integer N as input, write a program to print a number diamond of 2*N -1 rows as shown below.
Note: There is a space after each number.
Input
The first line of input is an integer N.
Explanation
In the given example, the number of rows in the diamond is
5.So, the output should be
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Sample Input 1
5
Sample Output 1
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Sample Input 2
4
Sample Output 2
1
1 2
1 2 3
1 2 3 4
1 2 3
1 2
1
# Python 3.9.5
def enter_number():
number = input('Enter number: ')
return number
def build_diamond(rows):
rows = int(rows)
k = 2 * rows - 2
for i in range(0, rows):
for j in range(0, k):
print(end=" ")
k = k - 1
for j in range(1, i + 1):
print(j, end=" ")
print("")
k = rows - 2
for i in range(rows, -1, -1):
for j in range(k, 0, -1):
print(end=" ")
k = k + 1
for j in range(1, i + 1):
print(j, end=" ")
print("")
def main():
number = enter_number()
build_diamond(number)
if __name__ == '__main__':
main()
Comments
In the above code there should not be spaces before the diamond. What should be done for it?
Leave a comment