Answer to Question #289485 in Python for Bubby

Question #289485

Trapezium Order

You are given an integer N. Print N rows starting from 1 in the trapezium order as shown in the output of the below examples.

Input:

The input contains an integer N.

Output:

The output should have N lines.

Each of the N lines should have space-separated integers as per the trapezium order.

Sample Input:

4

Sample Output:

1 2 3 4 17 18 19 20

5 6 7 14 15 16

8 9 12 13

10 11



1
Expert's answer
2022-01-23T10:33:29-0500

The code is given by


num = int(input("Number of lines: "))
lterm = 1
rterm = num * num + 1
  
for ​i in range(num, -1, -1):
        
    for space in range(num, i-1, -1):
        print(" ", end ="")
  
    for j in range(1, i + 1): 
        print(str(lterm)+" ", end ="")
        lterm += 1
 
    for j in range(1, i + 1): 
        print(rterm, end ="")
        if j < i:
            print(" ", end ="")
        rterm += 1
        
    rterm = rterm - (i - 1) * 2 - 1 
    print()


For example, we can have something of the form


Sample input:

Number of lines: 4

Sample output:
 1 2 3 4 17 18 19 20
  5 6 7 14 15 16
   8 9 12 13
    10 11




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