Acknowledgment. This notebook has been adapted from Wellesley CS111 course materials: (http://cs111.wellesley.edu/spring19).
countDown
¶Notice the repeated printing, despite the lack of an explicit for
or while
loop in the body of the function.
def countDown(n):
'''Prints numbers from n down to 1'''
if n < 1: # Base case
pass # Do nothing
else: # Recursive case: n >= 1:
print(n)
countDown(n-1)
countDown(5)
countDown(-5)
countDown(10)
If the base case does nothing, we can re-write the conditional.
This is a general fact about if-statements, not necessarily related to recursion.
def countDown(n):
'''Prints numbers from n down to 1'''
if n > 0:
print(n)
countDown(n-1)
countDown(6)
def countDownGotcha(n):
'''Prints numbers from n down to 1'''
if n <= 0: # Base case
pass # Do nothing
else: # Recursive case: n>0:
print(n)
countDownGotcha(n)
countDownGotcha(10)
If you run the line above, you should see toward the end of the output, the dreaded error:
RuntimeError: maximum recursion depth exceeded while calling a Python object
It means that the memory allocated to your program doesn't have space anymore for all the opened execution frames that are opened while your function is recursively invoking itself, endlessly!
Restart the kernel if you ran this code and got the error.
The following example will also lead to infinite recursion. Can you explain why?
How can you fix the problem?
def printHalvesGotcha(n):
"""Prints n, n/2, down to ... 1"""
if n >= 1:
print(n)
printHalvesGotcha(n/2)
printHalvesGotcha(10)
countUp
¶Define a recursive function countUp
that counts up from 1 to n
rather than n
to 1. countUp(5)
should print:
1
2
3
4
5
def countUp(n):
'''Prints out integers from 1 up to n'''
if n < 1:
pass # do nothing
else:
countUp(n-1)
print(n)
countUp(5)
countUp(-5)
countDownUp
¶How about a recursive function that counts down and up? This one is a bit more complex, but very elegant.
def countDownUp(n):
"""Prints integers from n down to 1
and then from 1 up to n."""
if n < 1:
pass # do nothing
else:
print(n)
countDownUp(n-1)
print(n)
countDownUp(4)
countDownUp(10)
triangle
¶Lets create some recursive patterns.
Write a recursive function called triangle
that takes a length parameter n
and a character c
and draws a triangle pattern composed of lines of c
, starting with length len
going down to 1.
For example: triangle(10, '*')
should print:
**********
*********
********
*******
******
*****
****
***
**
*
def triangle(n, c):
if n < 1:
pass # do nothing
else:
print( n * c)
triangle(n-1, c)
triangle(10, '*')
triangle(6, '^')
triangleAlt
¶Write a recursive function called triangleAlt
that takes a length parameter n
and two characters c1
and c2
and draws a triangle pattern similar to the previous example but now the rows must alternate between c1
and c2
(starting with char1
).
For example: triangleAlt(8, '@', '#')
should print:
@@@@@@@@
#######
@@@@@@
#####
@@@@
###
@@
#
def triangleAlt(n, c1, c2):
if n < 1:
pass # do nothing
else:
print(n * c1)
triangleAlt(n-1, c2, c1)
triangleAlt(10, '@', '#')
triangleAlt(9, 'a', 'b')
triangleDownUp
¶Write a recursive function called triangleDownUp
that takes a length parameter n
and two characters c1
and c2
and draws a triangle of rows of characters getting smaller starting with length n
down to 1
and then should print the same triangle "inverted."
For example: triangleDownUp(8, '@', '#')
should print:
@@@@@@@@
#######
@@@@@@
#####
@@@@
###
@@
#
#
@@
###
@@@@
#####
@@@@@@
#######
@@@@@@@@
def triangleDownUp(n, c1, c2):
if n < 1:
pass
else:
print(n * c1)
triangleDownUp(n-1, c2, c1)
print(n * c1)
triangleDownUp(8, '@', '#')