CSC148H5 Lecture 14: Recursion 5
Document Summary
Csc148h5s - introduction to computer science (winter 2017) Return max number in possibly nested list of numbers. 24 nums = [] for element in lst: if isinstance(element, int): else : nums. append(element) nums. append(rec_max(element)) return max(nums) >>> rec_max([4, 3, [1, [5]]]) nums = [4, 3] element = [1, [5]] Function is on hold nowwe have to figure out rec_max([1, [5]]) nums = [1] element = [5] This call is now suspended we have to figure out rec_max([5]) nums = [5] return max(nums) . return 5 def rec(n): return 1 + rec(n-1) rec(10) 1 + function is on hold, because we have to first figure out rec (9) rec(9) = 1 + function is on hold, waiting for rec(8) rec(8) = 1 + on hold waiting for rec(7) There is no termination and it keeps searching for a base case that it will never find. Csc108 def search_list (lst,value): for element in lst: return false if element == value: return true.