Since im fairly new to python and programming i have a question of a very basic nature. I want to make a iteration function and a recursive function that does this:
If you can use range
, I would do it like this in Python 2.7:
f = lambda x : map(abs, range(-x,x+1))
for x in f(4):
print x,
Iterative implementation:
>>> def bounce(num):
... limit, delta = num + 1, -1
... while(num != limit):
... print num
... num += delta
... if num == 0:
... delta = 1
Recursive implementation:
>>> def bounce(num):
... print num
... if num:
... bounce(num - 1)
... print num
...
...
Output in both the cases:
>>> bounce(4)
4
3
2
1
0
1
2
3
4