Reputation: 498
I have to print a pyramid of numbers with this rule:
odd index: 1 to index, even index: index to 1:
1
21
123
4321
12345
654321
1234567
87654321
123456789
I wrote this code:
def printFigure(rows):
if rows > 0:
if rows%2 == 0:
printFigure(rows-1)
while(rows>0):
print(str(rows)[::-1], end = '')
rows -= 1
print('')
if rows%2 == 1:
printFigure(rows-1)
while (rows>0):
print(str(rows),end = '')
rows -= 1
print('')
but the output is:
1
21
321,
4321
54321
654321
7654321
87654321
987654321
I'm a beginner with recursion, I'll be glad for your explanations too. thanks.
Upvotes: 3
Views: 1188
Reputation: 71471
You can use simple recursion:
def print_pyramid(_count = 1):
if _count < 10:
print((lambda x:x[::-1] if not _count%2 else x)(''.join(map(str, range(1, _count+1)))))
print_pyramid(_count+1)
print_pyramid()
Output:
1
21
123
4321
12345
654321
1234567
87654321
123456789
Upvotes: 1
Reputation: 27251
Here's the simplest loop version:
is_reversed = False
for i in range(1, 10):
step = -1 if is_reversed else 1
print(''.join(map(str, range(1, i + 1)))[::step])
is_reversed = not is_reversed
This regenerates a string every iteration. We can also store the value of the previous result and build on that:
s = ''
is_reversed = False
for i in range(1, 10):
s += str(i)
step = -1 if is_reversed else 1
print(s[::step])
is_reversed = not is_reversed
And of course, this can easily (and pointlessly) be converted to a tail-recursive function by just passing along the stack:
def f(s, i, max_i, is_reversed):
if i == max_i:
return
s += str(i)
step = -1 if is_reversed else 1
print(s[::step])
is_reversed = not is_reversed
i += 1
f(s, i, max_i, is_reversed)
f('', 1, 10, False)
The results for each of these increasingly weird looking pieces of code:
1
21
123
4321
12345
654321
1234567
87654321
123456789
Upvotes: 1
Reputation: 522396
You have two major problems with your current code. First, the check on whether the rows
is even or odd should be happening after the recursive call to printFigure
. The reason for this is that it is after backing out from the recursive call that you want to decide to print the line in order or backwards.
The second problem was with the logic in the else
printing condition. You need to print starting at 1, up to the number of rows. I used a dummy variable to achieve this, though there are probably a few other ways of doing it.
def printFigure(rows):
if rows > 0:
printFigure(rows-1)
if rows%2 == 0:
while(rows>0):
print(str(rows)[::-1], end='')
rows -= 1
print('')
else:
i = 1
while (i <= rows):
print(str(i), end='')
i += 1
print('')
printFigure(9)
1
21
123
4321
12345
654321
1234567
87654321
123456789
Upvotes: 2
Reputation:
if you don't need to use recursivion, other simple solution is:
def printFigure(rows):
for x in range(rows):
items = [str(i) for i in range(1, x + 1)]
if x % 2 == 0:
items = items[::-1]
print(''.join(items))
Upvotes: 2
Reputation: 61920
You could use an inner function that prints from bottom to top, and the call this function from an outer function, for instance:
def print_rows(n, limit):
if n < limit:
numbers = range(1, n + 1) if n % 2 == 1 else reversed(range(1, n + 1))
print(''.join(map(str, numbers)))
print_rows(n + 1, limit)
def print_pyramid_recursive(n):
if n > 0:
print_rows(1, n)
print_pyramid_recursive(10)
Output
1
21
123
4321
12345
654321
1234567
87654321
123456789
In this case the inner function is print_rows
and the outer function is print_pyramid_recursive
. Note, that this problem has a very simple non-recursive solution, for example:
def print_pyramid(n):
for i in range(1, n + 1):
numbers = range(1, i + 1) if i % 2 == 1 else reversed(range(1, i + 1))
print(''.join(map(str, numbers)))
Upvotes: 1