# (edited) log from lecture 17, 4/24/17, DS # notes on recusion are linked from the course page >>> factorial_iterative(5) 120 >>> factorial(5) 120 # editing to add debugging output: >>> runfile('/Volumes/home/schar/Desktop/recursion.py', wdir=r'/Volumes/home/schar/Desktop') >>> factorial(5) >>> factorial(5) calling factorial( 5 ) calling factorial( 4 ) calling factorial( 3 ) calling factorial( 2 ) calling factorial( 1 ) calling factorial( 0 ) returning 1 in base case returning 1 returning 2 returning 6 returning 24 returning 120 120 >>> factorial_interesting(5) 120 >>> list(range(2,6)) [2, 3, 4, 5] >>> str(list(range(2,6))) '[2, 3, 4, 5]' >>> str(list(range(2,6)))[1:-1] '2, 3, 4, 5' >>> str(list(range(2,6)))[1:-1].replace(", ", "*") '2*3*4*5' >>> eval(str(list(range(2,6)))[1:-1].replace(", ", "*")) 120 >>> go(0) Stop! >>> go(1) Go 1 Stop! >>> go(3) Go 3 Go 2 Go 1 Stop! >>> goback(0) Stop! >>> goback(1) Go 1 Stop! Back 1 >>> goback(3) Go 3 Go 2 Go 1 Stop! Back 1 Back 2 Back 3 >>> get_positive() Please enter a positive number: 3 3 >>> get_positive() Please enter a positive number: -5 Please enter a positive number: 0 Please enter a positive number: 3 3 >>> reverse('hello') 'olleh' # editing to add debugging output: >>> runfile('/Volumes/home/schar/Desktop/recursion.py', wdir=r'/Volumes/home/schar/Desktop') >>> reverse('hello') calling reverse with some_string = hello calling reverse with some_string = ello calling reverse with some_string = llo calling reverse with some_string = lo calling reverse with some_string = o calling reverse with some_string = 'olleh' >>> >>> # editing to create infinite recursion by changing # return n * factorial(n-1) # to # return n * factorial(n) >>> runfile('/Volumes/home/schar/Desktop/recursion.py', wdir=r'/Volumes/home/schar/Desktop') >>> factorial(5) calling factorial with n = 5 calling factorial with n = 5 calling factorial with n = 5 calling factorial with n = 5 ... File "/Volumes/home/schar/Desktop/recursion.py", line 38, in factorial return n * factorial(n) File "/Volumes/home/schar/Desktop/recursion.py", line 38, in factorial return n * factorial(n) File "/Volumes/home/schar/Desktop/recursion.py", line 38, in factorial return n * factorial(n) ... File "/Volumes/home/schar/Desktop/recursion.py", line 32, in factorial print("calling factorial(", n, ")") RuntimeError: maximum recursion depth exceeded while calling a Python object >>>