Answer to Question #242279 in Python for saras

Question #242279

Hollow Inverted Half Pyramid - 2

Given the number of rows N, write a program to print the hallow inverted half pyramid pattern similar to the pattern shown below.

1 2 3 4 5

1 4

1 3

1 2

1

Input


The input will be a single line containing a positive integer (N).

Output


The output should be N rows containing the numbers in the hollow inverted half pyramid pattern.

Note: There is a space after each number.


1
Expert's answer
2021-09-26T02:42:56-0400
N = int(input())


length = N * 2 - 1
pattern = [' '.join([str(x) for x in range(1, N + 1)])]
for i in range(1, N - 1):
    center_width = length - 2 * i - 2
    pattern.append('{0}{1}{2}'.format('1', ' ' * center_width, str(N - 1)).rjust(length , ' '))
    N -= 1
pattern.append('1'.rjust(length , ' '))
    
for i in pattern:
    print(i)

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