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.
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
Your code:
N = int(input())
rows = []
for number in range(1, N + 1):
rows.append(' '.join([str(x) for x in range(1, number + 1)]))
for number in rows[::-1][1::]:
rows.append(number)
diamond = '\n'.join([x.center(2* N - 1) for x in rows])
print(diamond)
the above code works fine but only 7/10 test cases are passed
for input 15 the output is not the same as expected
Refer to the document in the link below
https://docs.google.com/document/d/1Un_RPlLjEqNbbkJDegaqVkYLedITyQdXRKugY24hJWk/edit?usp=sharing
row = int(input("Enter the number or rows: "))
sol = []
for n in range(1, row + 1):
sol.append(' '.join([str(i) for i in range(1, n + 1)]))
for n in sol[::-1][1::]:
sol.append(n)
solution = '\n'.join([i.center(2* row - 1) for i in sol])
print(solution)
Comments
Leave a comment