from lab07 import * # Q5 def reverse_other(t): """Reverse the labels of every other level of the tree using mutation. >>> t = Tree(1, [Tree(2), Tree(3), Tree(4)]) >>> reverse_other(t) >>> t Tree(1, [Tree(4), Tree(3), Tree(2)]) >>> t = Tree(1, [Tree(2, [Tree(5, [Tree(7), Tree(8)]), Tree(6)]), Tree(3)]) >>> reverse_other(t) >>> t Tree(1, [Tree(3, [Tree(5, [Tree(8), Tree(7)]), Tree(6)]), Tree(2)]) """ "*** YOUR CODE HERE ***" # Q6 def cumulative_sum(t): """Return a new Tree, where each entry is the sum of all entries in the corresponding subtree of t. >>> t = Tree(1, [Tree(3, [Tree(5)]), Tree(7)]) >>> cumulative = cumulative_sum(t) >>> t Tree(1, [Tree(3, [Tree(5)]), Tree(7)]) >>> cumulative Tree(16, [Tree(8, [Tree(5)]), Tree(7)]) >>> cumulative_sum(Tree(1)) Tree(1) """ "*** YOUR CODE HERE ***" # Q7 def link_to_list(link): """Takes a Link and returns a Python list with the same elements. >>> link = Link(1, Link(2, Link(3, Link(4)))) >>> link_to_list(link) [1, 2, 3, 4] >>> link_to_list(Link.empty) [] """ "*** YOUR CODE HERE ***" # Q8 def reverse(link): """Returns a Link that is the reverse of the original. >>> print_link(reverse(Link(1))) <1> >>> link = Link(1, Link(2, Link(3))) >>> new = reverse(link) >>> print_link(new) <3 2 1> >>> print_link(link) <1 2 3> """ "*** YOUR CODE HERE ***" # Q9 def deep_map_mut(fn, link): """Mutates a deep link by replacing each item found with the result of calling fn on the item. Does NOT create new Links (so no use of Link's constructor) Does not return the modified Link object. >>> link1 = Link(3, Link(Link(4), Link(5, Link(6)))) >>> deep_map_mut(lambda x: x * x, link1) >>> print_link(link1) <9 <16> 25 36> """ "*** YOUR CODE HERE ***" # Q10 def has_cycle(link): """Return whether link contains a cycle. >>> s = Link(1, Link(2, Link(3))) >>> s.rest.rest.rest = s >>> has_cycle(s) True >>> t = Link(1, Link(2, Link(3))) >>> has_cycle(t) False >>> u = Link(2, Link(2, Link(2))) >>> has_cycle(u) False """ "*** YOUR CODE HERE ***" def has_cycle_constant(link): """Return whether link contains a cycle. >>> s = Link(1, Link(2, Link(3))) >>> s.rest.rest.rest = s >>> has_cycle_constant(s) True >>> t = Link(1, Link(2, Link(3))) >>> has_cycle_constant(t) False """ "*** YOUR CODE HERE ***"