CSC111 Lab 13
--D. Thiebaut 15:09, 28 April 2010 (UTC)
Contents |
This lab deals with recursive functions. |
Factorial
Create a copy of this simple example:
# factorial.py
# Demonstrates a recursive factorial function
def fact( n ):
if n<=1:
return 1
y = fact( n-1 )
result = n * y
return result
def main():
n = input( "Enter a positive number: " )
x = fact( n )
print "the factorial of %d is %d." % ( n , x )
main()
Run your program and verify that it computes the correct result. Below are some factorials numbers to compare your output to.
1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 10! = 3628800 11! = 39916800 12! = 479001600 13! = 6227020800 14! = 87178291200
- Question 1
- Add print statements to your fact function so that it will print exactly what it does. For example, before it tests to see if n is less than equal to 1, you could print:
print "fact function started. Receives n =", n print "testing if n is >= 1"
- Add print statements that will show the values of y and result.
- Run your program and observe the print statements. Can you see better how fact recursively calls itself?
- Question 2
- Create the more sophisticated program shown below. Observe it well and try to figure out what the indent variable does.
# factorialPrint.py
# Demonstrates a recursive factorial function
def fact( n, indent ):
print indent, "fact(%d) started" % n
print indent, "comparing %d to 1" % n
if n<=1:
print indent, "%d is <= 1. Returning 1" % 1
return 1
print indent, "%d is not <= 1. Calling fact(%d) and storing it in y" % (n, n-1)
y = fact( n-1, indent + " " )
print indent, "just received %d from fact( %d )." % ( y, n-1 )
result = n * y
print indent, "multiplied %d by %d. It's %d. Returning %d to caller" % ( n, y, result, result )
return result
def main():
# print first 15 factorials
n = input( "Enter a positive integer: " )
print "Main calls fact( %d )" % n
y = fact( n, " " )
print "Main receives result = ", y
main()
Binary Search
Create a copy of the binary search program we saw recently in class. Observe the main function. Make sure you figure out how large the array is.
Run the program and verify that the search function does a good job locating or not the numbers.
First problem
Modify the program so that it counts the number of comparisons that it performs. A good way to do this is to create a global variable, count that is declared at the beginning of your program, and which you increment by one, every time the binsearch function compares two quantities. These comparisons are a good indicator of the amount of "work" done by the function, since it is obvious just looking at the program to figure out how much time the function will call itself. The correct way to use count as a global variables is illustrated below. You initialize the variable at the beginning of the program, and in the functions that need to use it, you define count as global so that python will know that count is not just a local variable used inside the function, but refers to something defined outside the function.
- program header
count = 0
def binsearch( ... ):
global count
... count += 1
def main():
global count ... count = 0 binsearch( ... ) ...
Make the main program print the number of comparisons performed after the function returns to main.
Make sure you reset the counter to 0 for every new search!