## Recursive Objects ## # Q2 def insert(link, value, index): """Insert a value into a Link at the given index. >>> link = Link(1, Link(2, Link(3))) >>> print_link(link) <1 2 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 ***" # Q4 def same_shape(t1, t2): """Returns whether two Trees t1, t2 have the same shape. Two trees have the same shape if they have the same number of branches and each of their children have the same shape. >>> t, s = Tree(1), Tree(3) >>> same_shape(t, t) True >>> same_shape(t, s) True >>> t = Tree(1, [Tree(2), Tree(3)]) >>> same_shape(t, s) False >>> s = Tree(4, [Tree(7)]) >>> same_shape(t, s) False """ "*** YOUR CODE HERE ***" # Linked List Class class Link: """ >>> s = Link(1, Link(2, Link(3))) >>> s Link(1, Link(2, Link(3))) >>> len(s) 3 >>> s[2] 3 >>> s = Link.empty >>> len(s) 0 """ empty = () def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest def __repr__(self): if self.rest is not Link.empty: rest_str = ', ' + repr(self.rest) else: rest_str = '' return 'Link({0}{1})'.format(repr(self.first), rest_str) def __len__(self): """ Return the number of items in the linked list. >>> s = Link(1, Link(2, Link(3))) >>> len(s) 3 >>> s = Link.empty >>> len(s) 0 """ return 1 + len(self.rest) def __getitem__(self, i): """Returning the element found at index i. >>> s = Link(1, Link(2, Link(3))) >>> s[1] 2 >>> s[2] 3 """ if i == 0: return self.first else: return self.rest[i-1] 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) # Tree Class class Tree: def __init__(self, entry, branches=()): self.entry = entry for branch in branches: assert isinstance(branch, Tree) self.branches = list(branches) def __repr__(self): if self.branches: branches_str = ', ' + repr(self.branches) else: branches_str = '' return 'Tree({0}{1})'.format(self.entry, branches_str) def is_leaf(self): return not self.branches