## Lab 5: Mutable Sequences and Trees ## # Sequences def map(fn, seq): """Applies fn onto each element in seq and returns a list. >>> map(lambda x: x*x, [1, 2, 3]) [1, 4, 9] """ "*** YOUR CODE HERE ***" def filter(pred, seq): """Keeps elements in seq only if they satisfy pred. >>> filter(lambda x: x % 2 == 0, [1, 2, 3, 4]) [2, 4] """ "*** YOUR CODE HERE ***" def reduce(combiner, seq): """Combines elements in seq using combiner. >>> reduce(lambda x, y: x + y, [1, 2, 3, 4]) 10 >>> reduce(lambda x, y: x * y, [1, 2, 3, 4]) 24 >>> reduce(lambda x, y: x * y, [4]) 4 """ "*** YOUR CODE HERE ***" # pyTunes def make_pytunes(username): """Return a pyTunes tree as shown in the diagram with USERNAME as the value of the root. >>> pytunes = make_pytunes('i_love_music') >>> print_tree(pytunes) i_love_music pop justin bieber single what do you mean? 2015 pop mashup trance darude sandstorm """ "*** YOUR CODE HERE ***" def num_songs(t): """Return the number of songs in the pyTunes tree, t. >>> pytunes = make_pytunes('i_love_music') >>> num_songs(pytunes) 3 """ "*** YOUR CODE HERE ***" def add_song(t, song, category): """Returns a new tree with SONG added to CATEGORY. Assume the CATEGORY already exists. >>> indie_tunes = tree('indie_tunes', ... [tree('indie', ... [tree('vance joy', ... [tree('riptide')])])]) >>> new_indie = add_song(indie_tunes, 'georgia', 'vance joy') >>> print_tree(new_indie) indie_tunes indie vance joy riptide georgia """ "*** YOUR CODE HERE ***" # Tree ADT def tree(root, branches=[]): for branch in branches: assert is_tree(branch), 'branches must be trees' return [root] + list(branches) def root(tree): return tree[0] def branches(tree): return tree[1:] def is_tree(tree): if type(tree) != list or len(tree) < 1: return False for branch in branches(tree): if not is_tree(branch): return False return True def is_leaf(tree): return not branches(tree) def print_tree(t, indent=0): """Print a representation of this tree in which each node is indented by two spaces times its depth from the entry. >>> print_tree(tree(1)) 1 >>> print_tree(tree(1, [tree(2)])) 1 2 >>> numbers = tree(1, [tree(2), tree(3, [tree(4), tree(5)]), tree(6, [tree(7)])]) >>> print_tree(numbers) 1 2 3 4 5 6 7 """ print(' ' * indent + str(root(t))) for b in branches(t): print_tree(b, indent + 1) def copy_tree(t): """Returns a copy of t. Only for testing purposes. >>> t = tree(5) >>> copy = copy_tree(t) >>> t = tree(6) >>> print_tree(copy) 5 """ return tree(root(t), [copy_tree(b) for b in branches(t)])