






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







st
1 2 3
import lec print lec12.num_es('ae') code with function call s num_es:1 3 'e' s num_es:1 3 'ae'
def num_es(s): if s == '': return 0 return ((1 if s[0] == 'e' else 0) + num_es(s[1:])) 1 2 3 inside module lec 12 import creates the function objects that are defined in lec12, like num_es, so we can call them. s num_es:1 2 '' ✗ ✗ ✗ ✗
print oops('ae') code with function call
def oops(s): 1 return ((1 if s[0] == 'e' else 0)^ +^ oops (s[1:])) hypothetical function definition s oops: 1 'ae' s oops: 1 'e' s oops: 1 '' ... error (index error) when splitting not applicable
print oops('ae') code with function call
def oops(s): 1 return ((1 if^ s[0:1]^ == 'e' else 0)^ +^ oops (s[1:])) hypothetical function definition s oops: 1 'ae' s oops: 1 'e' s oops: 1 '' s oops: 1 '' s oops: 1 '' ... [lots of copies of the same message] RuntimeError: maximum recursion depth exceeded
Test cases generally handy here
Verify recursive cases with the specification
Arguments of calls must somehow get “smaller”, so each recursive call gets closer to a base case
When the recursive case doesn't apply When the argument values are as "small" as possible When the answer is determined with little calculation.
def ispalindrome(s):! """Returns: True if string s is a palindrome, False otherwise"""" if len(s) < 2:" return True
st against last character, then interior (see posted code for solutions)
Returns: reverse of s
Suppose we can reverse a smaller string (e.g., one fewer character) Can we use that solution to reverse whole string? H e l l o! ! o l l e H e l l o! ! o l l e