## Mutables Linked Lists ## # Q2 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 ***" # Q3 def list_to_link(lst): """Takes a Python list and returns a Link with the same elements. >>> link = list_to_link([1, 2, 3]) >>> print_link(link) <1 2 3> """ "*** YOUR CODE HERE ***" # Q4 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 ***" # Q5 def insert(link, value, index): """Insert a value into a Link at the given index. >>> link = Link(1, Link(2, Link(3))) >>> insert(link, 9001, 0) >>> print_link(link) <9001 1 2 3> >>> insert(link, 100, 2) >>> print_link(link) <9001 1 100 2 3> >>> insert(link, 4, 5) IndexError """ "*** YOUR CODE HERE ***" # Linked List Class class Link: empty = () def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest def print_link(link): """Print elements of a linked list link. >>> link = Link(1, Link(2, Link(3))) >>> print_link(link) <1 2 3> >>> link1 = Link(1, Link(Link(2), Link(3))) >>> print_link(link1) <1 <2> 3> >>> link1 = Link(3, Link(Link(4), Link(5, Link(6)))) >>> print_link(link1) <3 <4> 5 6> """ print('<' +helper(link).rstrip() +'>') def helper(link): if link == Link.empty: return '' elif isinstance(link.first, Link): return '<' +helper(link.first).rstrip() + '> '+ helper(link.rest) else: return str(link.first) +' '+ helper(link.rest)