I need help in doing this. We need to create a code where the output must be the inverse of this code
Sample code(our basis):
line = 1
while line <= 5:
number = 1
while number <= line:
print(number, end = '')
number = number 1
line = line 1
print('')
Output:
1
12
123
1234
12345
It must be:
12345
1234
123
12
1
May you help me?
CodePudding user response:
Here's some code
line = 5
while line >= 1:
number = 1
while number <= line:
print(number, end = '')
number = number 1
line = line - 1
print('')
CodePudding user response:
Well, there are several ways to write a pyramid.
Given the requirement to use while
:
from functools import reduce
def inverse_pyramid(lines: int) -> None:
while lines:
xs = reduce(lambda x, y: str(x) str(y), range(1, lines 1))
print(xs)
lines -= 1
How to without the use of loops:
from functools import reduce
def recursive_inverse_pyramid(lines: int) -> None:
if lines:
xs = reduce(lambda x, y: str(x) str(y), range(1, lines 1))
print(xs)
recursive_inverse_pyramid(lines - 1)
How to with map instead of reduce:
def pyramid_mapper(lines: int) -> None:
if lines:
xs = map(str, range(1, lines 1))
print(''.join(xs))
pyramid_mapper(lines - 1)
How to with a comprehension:
def pyramid_comprehension(lines: int) -> None:
while lines:
xs = (str(x) for x in range(1, lines 1))
print(''.join(xs))
lines -= 1