Hola, buenas, alguien me ayudaría a entender este código y la lógica detrás de él?
def pyramid(n):
nums = ''.join(str(i+1) + ' ' for i in range(n))
for i in range(n):
blank = ' ' * 2 * (n-i-1)
prefix = nums[:2*i]
postfix = prefix[::-1]
print(f'{blank}{prefix}{i+1}{postfix}')
pyramid(9)
Salida del programa:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
def pyramid(n):
nums = ''.join(str(i+1) + ' ' for i in range(n))
for i in range(n):
blank = ' ' * 2 * (n-i-1)
prefix = nums[:2*i]
postfix = prefix[::-1]
print(f'{blank}{prefix}{i+1}{postfix}')
pyramid(9)
Salida del programa:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1