COMPSCI 61A Study Guide - Final Guide: List Comprehension, Linked List, System On A Chip
zogo39484755 and 6 others unlocked
22
COMPSCI 61A Full Course Notes
Verified Note
22 documents
Document Summary
Tail recursion should only save your memory, not change the. Cascade function: cascade (0, 3: call (descending) Example: sum_reverse (return new linked list where each element is the sum of all terms after it) ([1, 2, 3]) return [6, 5, 3] (use ascending leg of cascade) def sum_reverse (link): return link if link is link. empty or link. rest is link. empty: sum_reverse(link. rest) link. first += link. rest. first return link. Example2: sum_reverse (modify a tree such that each element becomes the sum of its children) def sum_reverse (t): Don"t need a base case cause won"t error without; if empty, the list comprehension just won"t execute cause t. branches is empty. Def all_paths (t, path: parameter path allows us to take advantage of descending, (all previous code allows us to take advantage of ascending leg of cascade function. Example4: all_subsets [1,2,3,4,5] leg) def all_subsets (lst): return lst if not lst: sets = all_subsets(lst[1:]) return [[lst[0]] + p for p in sets] + sets.