Professional Documents
Culture Documents
Wk04+Recursion Vs Iterations Debugging
Wk04+Recursion Vs Iterations Debugging
def factorialR(n): 1 if 𝑛 = 0
if n == 1: 𝑛! = ቊ
𝑛−1 !×𝑛 otherwise
return 1
else:
return factorialR(n-1) * n
Recursion
• Rules of recursion Must have a terminal
condition
def factorialR(n):
if n == 1:
return 1
else:
return factorialR(n-1) * n Must call itself
with reduced
problem size for
every layer
deeper
Making a recursive call
4
n
def factorialR(n):
if n == 1:
return 1
else:
return n * factorialR(n-1)
Making a recursive call
n
factorialR(1)
n-1 4 n
1 1
n-1
def factorialR(n):
factorialR(2) if n == 1:
def factorialR(n): return 1
if n == 1: else:
2 2 return 1 return n * factorialR(n-1)
else:
factorialR(3) return n * factorialR(n-1)
6 3
Knowledge: pass by value, variable scope , call stack
Today
• More about Recursion
– Recursion vs Iteration
– Debugging
– Briefing on Examplify
Recursion vs Iteration
Reversing a String
• What about reversing a string? Of course, we can just use string slicing
c output
a a
b ba
c cba
d dbca
e edbca
Reversing String (Recursive Version)
• reverseStringR(‘abcde’)
• reverseStringR(‘bcde’)+’a’
• reverseStringR(‘cde’)+’b’+’a’
• reverseStringR(‘de’)+’c’+’b’+’a’
• reverseStringR(‘e’)+’d’+’c’+’b’+’a’
• reverseStringR(‘’)+’e’+’d’+’c’+’b’+’a’
• ‘’+’e’+’d’+’c’+’b’+’a’
• ‘edcba’
Taylor Series
Taylor Series
• Using iteration
n=k
𝑘−1
(−1)𝑛 2𝑛+1 (−1)𝑘
𝑠𝑖𝑛𝑅 𝑥, 𝑘 = 𝑥 + 𝑥 2𝑘+1 𝑖𝑓 𝑘 ≥ 0
2𝑛 + 1 ! 2𝑘 + 1 !
𝑛=0
(−1)𝑘
= 𝑠𝑖𝑛𝑅 𝑥, 𝑘 − 1 + 𝑥 2𝑘+1 𝑖𝑓 𝑘 ≥ 0
2𝑘 + 1 !
−1
(−1)𝑛 2𝑛+1
𝑠𝑖𝑛𝑅 𝑥, 0 = 𝑥 =0 𝑖𝑓 𝑘 < 0
2𝑛 + 1 !
𝑛=0
Computing sine by Recursion
0, 𝑘<0
𝑠𝑖𝑛𝑅 𝑥, 𝑘 = ൞ (−1)𝑘 2𝑘+1
𝑠𝑖𝑛𝑅 𝑥, 𝑘 − 1 + 𝑥 , 𝑘≥0
2𝑘 + 1 !
Recursion Common Patterns
Base cases
Base case The answer for previous k – 1 terms The kth term
Iteration/Recursion Conversion
Base case The answer for previous k – 1 terms The kth term
“Homework”
29
Tower of Hanoi
Towers after steps 1 and 2:
A B C
30
Tower of Hanoi
Towers after steps 1 and 2:
A B C
31
1. move four disks from peg A to peg C
Tower of Hanoi 2. move disk #5 from peg A to peg B
3. move four disks from peg C to peg B
Towers after steps 1 and 2:
A B C
36
Bugs and debugging
Bugs?
• In 1947, Grace Murray Hopper
was working on the Harvard
University Mark II Aiken Relay
Calculator (a primitive
computer).
Fail!
3
4
• fact(2.1)
• fact(-1)
Common Types of Errors
• Infinite loop (from not decrementing)
• def fact_iter(n):
• counter, result = n, 1
• while counter!= 0:
• result *= counter
• return result
Common Types of Errors
• Numerical imprecision
def foo(n):
counter, result = 0,0
while counter != n:
result += counter
counter += 0.1
return result
counter never exactly equals n
• foo(5)
Common Types of Errors
• Logic
def fib(n):
if n < 2:
return n
else:
return fib(n-1) + fib(n-1)
How to debug?
• Think like a detective
• Look at the clues: error messages, variable values.
• Eliminate the impossible.
• Run the program again with different inputs.
• Does the same error occur again?
How to debug?
• Work backwards
• From current sub-problem backwards in time
• Use a debugger
• IDLE has a simple debugger
• Overkill for our class
• Trace a function
• Display variable values
Displaying variables
debug_printing = True
def debug_print(msg):
if debug_printing:
print(msg)
def foo(n):
counter, result = 0,0
while(counter != n):
debug_print(f'{counter}, {n}, {result}')
counter, result = counter + 0.1, result + counter
return result
Example
def fib(n):
debug_print(f'n:{n}')
if n < 2:
return n
else:
return fib(n-1) + fib(n-1)
Other tips
• State assumptions clearly.
def factorial(n): # n integer >= 0
if n == 0:
return 1
else:
return n * factorial(n-1)