CSC148H5 Lecture 14: Recursion 5

21 views2 pages

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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents