Exercises
Exercise 1
Draw a stack diagram for the following program. What does the program print?
Exercise 2
Write a recursive function named ackerman
that evaluates Ackerman's function. Use your function to evaluate ackerman(3, 4)
, which should be 125. What happens for larger values of m
and n
?
Exercise 3
A palindrome is a word that is spelled the same backward and forward, like noon
and redivider
. Recursively, a word is a palindrome if the first and last letters are the same and the middle is a palindrome. The following are functions that take a string argument and return the first, last, and middle letters:
Type these functions into a file named
.py
and test them out. What happens if you callmiddle
with a string with two letters? One letter? What about the empty string, which is written''
and contains no letters?Write a recursive function called
is_palindrome
that takes a string argument and returnsTrue
if it is a palindrome andFalse
otherwise. Remember that you can use the built-in functionlen
to check the length of a string.
Exercise 4
Exercise 5
Write a recursive function called gcd
that takes parameters a
and b
and returns their greatest common divisor.
Last updated