CS150 - Fall 2013 - Class 18
exercises
write a function called factorial that takes a single parameter and returns the factorial of that number
>>> factorial(1)
1
>>> factorial(2)
2
>>> factorial(3)
6
>>> factorial(8)
40320
- look at factorial_iterative function in recursion.py
- I'm guessing most people wrote it this way
- does a loop from 1 up to n and multiplies the numbers
- could we have done it any other way?
recursion
- a recursive function is defined with respect to itself
- what does this mean?
- somewhere inside the function, the function calls itself
- just like any other function call
- the recursive call should be on a "smaller" version of the problem
- can we write factorial recursively?
- key idea: try and break down the problem into some computation, plus a smaller subproblem that looks similar
5! = 5 * 4 * 3 * 2 * 1
5! = 5 * 4!
- a first try:
def factorial(n):
return n * factorial(n-1)
- what happens if we run this with say 5?
5 * factorial(4)
|
V
4 * factorial(3)
|
V
3 * factorial(2)
|
V
2 * factorial(1)
|
V
1 * factorial(0)
|
V
0 * factorial(-1)
...
- at some point we need to stop. this is called the "base case" for recursion
- when is that?
- when n = 1 (or if you want to account for 0!, at 0)
- how can we change our function to do this?
- look at factorial function in
recursion.py code
- first thing, check to see if we're at the base case (if n == 0)
- if so, just return 1 (this lets 0! be 1)
- otherwise, we fall into our recursive case:
- n * factorial(n-1)
writing recursive functions
1. define what the function header is
- what parameters does the function take?
2. define the recursive case
- pretend you had a working version of your function, but it only works on smaller versions of your current problem, how could you write your function?
- the recursive problem should be getting "smaller", by some definition of smaller
- other ideas:
- sometimes define it in English first and then translate that into code
- often nice to think about it mathematically, using equals
3. define the base case
- recursive calls should be making the problem "smaller"
- what is the smallest (or simplest) problem
4. put it all together
- first, check the base case
- return something (or do something) for the base case
- if the base case isn't true
- calculate the problem using the recursive definition
- return the answer
- recursion has a similar feel to "induction" in mathematics
- proof by induction in mathematics:
1. show something works the first time (base case)
2. assume that it works for this time
3. show it will work for the next time
4. therefore, it must work for all the times
write a recursive function called reverse that takes a string as a parameter and reverses the string
1. define what the function header is
def reverse(some_string)
2. define the recursive case
- pretend like we have a function called reverse that we can use but only on smaller strings
- to reverse a string
- remove the first character
- reverse the remaining characters
- put that first character at the end
reverse(some_string) = reverse(some_string[1:]) + some_string[0]
3. define the base case
- in each case the string is going to get shorter
- eventually, it will be an empty string. what is the reverse of the empty string?
- ""
4. look at reverse function in
recursion.py code
- check the base case first: if the length of the string is 0
- otherwise
- call reverse again on the shorter version of the string
- append on what is returned by some_string
- if we added a print statement to reverse to print out each call to reverse what would we see?
- e.g. print "Reverse: " + some_string
>>> reverse("abcd")
Reverse: abcd
Reverse: bcd
Reverse: cd
Reverse: d
Reverse:
- we can also change the function to see what is being returned each time:
>>> reverse("abcd")
Reverse: abcd
Reverse: bcd
Reverse: cd
Reverse: d
Reverse:
Returning:
Returning: d
Returning: dc
Returning: dcb
Returning: dcba
- to reverse the string "abcd", reverse is called four times recursively
We can write lots of other functions this way as well. Write the following functions that take as input a list and calculate their values recursively:
- sum
- len(length)
- max
- min