'How to print this pyramid pattern up side down?
I am trying to print below pyramid pattern. But not clicking how to build logic for it ?
Pattern :
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
I have tried this code seems like this is not a right approach to get it.Any solution is appreciated
import numpy as np
n = 5
cnt=0
var = np.arange(1,n+1)
for row in range(1,n+1):
print(var[::-1][cnt:])
cnt= cnt + 1
Output of above pattern:
[5 4 3 2 1]
[4 3 2 1]
[3 2 1]
[2 1]
[1]
Solution 1:[1]
You can create a function to reduce overall complexity
def pyramid(height):
L = 0
for i in range(height):
s = ' '.join(map(str, range(height-i, 0, -1)))
L = max(L, len(s))
print(s.rjust(L))
height = int(input('Enter the height of the Pyramid : '))
pyramid(height)
Solution 2:[2]
Any solution? Ok:
>>> print("\n".join([ "{:>10}".format(" ".join([ str(x) for x in range(n,0,-1) ])) for n in range(5,0,-1) ]))
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | vaizki |
