Week 11: Recursion

Week 11 Goals

  • Introduction to Recursion

  • Tracing Recursive Functions

  • Developing Recursive Functions

    • Base Case

    • Recursive Case

    • Combining Recursive result and additional work to get final result

Week 11 Files

  • printmessage.py: prints a message n times using recursion

  • sum.py: sums the numbers from 1 to n using recursion

  • power.py: raises a number x to the power y

  • slicing.py: uses string slicing to count characters in a string recursively (no len() function used)

  • count_letter.py: counts the number of times a letter appears in a string (uses string slicing)

  • sorted.py: checks if a list is sorted (uses list slicing)

  • palindrome.py: checks if a string is a palindrome (uses string slicing)

Recursion

Any function that sometimes calls itself is known as a recursive function. In most cases, recursion can be considered an alternative to iteration (loops). Recursion often solves bigger problems by breaking them down into smaller problems of the exact same form. Once we know the solution to the smaller problem(s), we can do a small amount of work to combine the the smaller solutions to get the solution to the bigger problem.

There are three requirements for a successful recursive function:

  1. Base case: each function must have one or more base cases where the function does not make a recursive call to itself. Instead, the problem has reached its smallest point, and we can solve it directly.

  2. Recursive case: the function makes one (or more) calls to itself. The call must use a different argument to the function (or else we keep making the same recursive call over and over again!). In the recursive call, the original problem is made smaller. We often save the result of the recursive call as a partial result, and then do a small amount of work to combine the partial result with the original problem to get the final result.

  3. All possible recursive calls must be guaranteed to hit a base case. If you miss one, your program is susceptible to an infinite recursion bug!

Example: Print a Message

The printmessage.py program prints a given message n times. We’ve already seen how to solve this problem with a loop (this is known as an iterative solution, one that uses a loop to repeat). However, let’s explore how we can solve it without a loop using recursion.

To think about how to define printing a message n times in a recursive way, we want to define printing in terms of printing. In other words we want to think of a recursive definition of printing n things, and our recursive function’s structure will follow this with it recursive function calls:

printmessage(msg, num_times) is equivalent to:

  1. print(msg)
  2. printmessage(msg, num_times-1)  # here is our recursive definition

To understand what’s happening with recursion, it’s often helpful to draw a stack diagram. The key concept is that Python treats the recursive function call the same as any other function call — it places a new stack frame on top and begins executing the new function. The only difference is the function in this case has the same name.

Tips for Recursion

For any recursive problem, think of these questions:

  1. What is the base case? In what instances is the problem small enough that we can solve the problem directly? Sometimes you may need more than one base case, but rarely do you need several.

  2. What is the recursive call? Make one or calls in the function to itself. It is very important to use different arguments (usually "smaller") in the recursive call otherwise you obtain infinite recursion.

  3. Do all possible chains of recursion lead to a base case?

  4. If a recursive function is supposed to return a value, make sure all cases return a value and that recursive calls do something with the return value of the smaller problem before returning.