 |  |

 |
 |  |  |
 | Welcome to CS1315. Click on the python to add comments.
|  |
 |  |  |
|
This page removed for FERPA compliance
|
        |
Final Exam Review Fall 2006: Recursion
Post Questions REMOVED:
so for the second part of the question, if u ran question(3). .. would you the output of 6? ( 32=6)
for the fist part:
def addlist(list):
if len(list)==1:
return(list)
factorial = list[0]+addREMOVEDst[1:]
return(factorial)
for the second part
1. def question(n):
2. if n==0:
3. return 1
4. else:
5. return n * question(n-1)
1. defines the function so we can use it
2&3. if the number we are on is 0 return a 1 so the value we currently have is not changed
4&5. if we have a number that is not zero then we do line 4 multiply that number by 1 less than the number effectivly making this a function that gives the factorials of positive numbers.
Student4480
def addREMOVEDst(list):
if len(list)==1:
return(list)
total = list[0]+addREMOVEDst[1:]
return(total)
if def question(4)
would the function return 12 or 24?
it would be 24 b/c the function is doing 4*3*2*1 = 4!
Link to this Page