# # recursion_examples.py # # Example recursive functions from lecture # # Computer Science 111 # def power(b, p): """ returns b raised to the p power inputs: b is a number (int or float) p is a non-negative integer """ if p == 0: return 1 else: pow_rest = power(b, p-1) return b * pow_rest def mylen(s): """ returns the length of the sequence s input: s is a sequence (a string or list) """ if s == '' or s == []: return 0 else: len_rest = mylen(s[1:]) return 1 + len_rest def num_vowels(s): """ returns the number of vowels in the string s input: s is a string of 0 or more lowercase letters """ if s == '': print('num_vowels(' + s + '), returning 0') return 0 else: num_in_rest = num_vowels(s[1:]) if s[0] in 'aeiou': print('num_vowels(' + s + '), returning', 1 + num_in_rest) return 1 + num_in_rest else: print('num_vowels(' + s + '), returning', 0 + num_in_rest) return 0 + num_in_rest ## test cases here: if __name__ == '__main__': print("num_vowels('testing')", num_vowels('testing')) # print("num_vowels('the song remains the same')", # num_vowels('the song remains the same'))