Answer to Question #227991 in Python for varun

Question #227991

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



1
Expert's answer
2021-08-20T18:01:27-0400
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)

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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS