class Keyboard: """A Keyboard takes in an arbitrary amount of buttons, and has a dictionary of positions as keys, and values as Buttons. >>> b1 = Button(0, "H") >>> b2 = Button(1, "I") >>> k = Keyboard(b1, b2) >>> k.buttons[0].key 'H' >>> k.press(1) 'I' >>> k.typing([0, 1]) 'HI' >>> k.typing([1, 0]) 'IH' >>> b1.pressed 2 >>> b2.pressed 3 """ def __init__(self, *args): "*** YOUR CODE HERE ***" def press(self, info): """Takes in a position of the button pressed, and returns that button's output""" "*** YOUR CODE HERE ***" def typing(self, typing_input): """Takes in a list of positions of buttons pressed, and returns the total output""" "*** YOUR CODE HERE ***" class Button: def __init__(self, pos, key): self.pos = pos self.key = key self.pressed = 0 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 Link.empty: return 'Link({})'.format(self.first) else: return 'Link({}, {})'.format(self.first, repr(self.rest)) 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 __setitem__(self, index, element): """Sets the value at the given index to the element >>> s = Link(1, Link(2, Link(3))) >>> s[1] = 5 >>> s Link(1, Link(5, Link(3))) >>> s[4] = 5 Traceback (most recent call last): ... IndexError """ if index == 0: self.first = element elif self.rest is Link.empty: raise IndexError else: self.rest[index - 1] = element def __contains__(self, e): return self.first == e or e in self.rest def map(self, f): self.first = f(self.first) if self.rest is not Link.empty: self.rest.map(f) class Tree: def __init__(self, entry, children=[]): for c in children: assert isinstance(c, Tree) self.entry = entry self.children = children def __repr__(self): if self.children: children_str = ', ' + repr(self.children) else: children_str = '' return 'Tree({0}{1})'.format(self.entry, children_str) def is_leaf(self): return not self.children def every_other(s): """Mutates a linked list so that all the odd-indiced elements are removed (using 0-based indexing). >>> s = Link(1, Link(2, Link(3, Link(4)))) >>> every_other(s) >>> s Link(1, Link(3)) >>> odd_length = Link(5, Link(3, Link(1))) >>> every_other(odd_length) >>> odd_length Link(5, Link(1)) >>> singleton = Link(4) >>> every_other(singleton) >>> singleton Link(4) """ "*** YOUR CODE HERE ***" def long_paths(tree, n): """Return a list all paths in tree with length at least n. >>> t = Tree(3, [Tree(4), Tree(4), Tree(5)]) >>> left = Tree(1, [Tree(2), t]) >>> mid = Tree(6, [Tree(7, [Tree(8)]), Tree(9)]) >>> right = Tree(11, [Tree(12)]) >>> whole = Tree(0, [left, Tree(13), mid, right]) >>> for path in long_paths(whole, 2): ... print(path) ... Link(0, Link(1, Link(2))) Link(0, Link(1, Link(3, Link(4)))) Link(0, Link(1, Link(3, Link(4)))) Link(0, Link(1, Link(3, Link(5)))) Link(0, Link(6, Link(7, Link(8)))) Link(0, Link(6, Link(9))) Link(0, Link(11, Link(12))) >>> for path in long_paths(whole, 3): ... print(path) ... Link(0, Link(1, Link(3, Link(4)))) Link(0, Link(1, Link(3, Link(4)))) Link(0, Link(1, Link(3, Link(5)))) Link(0, Link(6, Link(7, Link(8)))) >>> long_paths(whole, 4) [] """ "*** YOUR CODE HERE ***" def remainders_generator(m): """ Takes in an integer m, and yields m different remainder groups of m. >>> remainders_mod_four = remainders_generator(4) >>> for rem_group in remainders_mod_four: ... for _ in range(3): ... print(next(rem_group)) 0 4 8 1 5 9 2 6 10 3 7 11 """ "*** YOUR CODE HERE ***" def friends(songs, judge): for song in songs: try: if song["rating"] > 1: judge.send(song) except StopIteration: break judge.close() def playlist(): good_playlist = False threshold = 15 while True: try: tammys_slappers = yield print('Playlist created.') total = sum([i[1] for i in tammys_slappers]) good_playlist = total >= threshold except GeneratorExit: if not good_playlist: print('There were not enough slappers on the playlist. Try again.') raise def judge(playlist): """ >>> bad_playlist = playlist() >>> next(bad_playlist) >>> tammy = judge(bad_playlist) >>> next(tammy) >>> friends([{'name': 'shake it off', 'artist': 'swift', 'rating': 20}], tammy) The judge is done There were not enough slappers on the playlist. Try again. >>> good_playlist = playlist() >>> next(good_playlist) >>> tammy = judge(good_playlist) >>> next(tammy) >>> friends([{'name': 'one dance', 'artist': 'drake', 'rating': 5}, \ {'name': 'treat you better', 'artist': 'mendes', 'rating': 1000}, \ {'name': 'hello', 'artist': 'adele', 'rating': 20} \ ], tammy) Playlist created. The judge is done """ total_songs = 3 songs = [] artists = ['gucci', 'drake', 'bieber'] min_rating = 2 "*** YOUR CODE HERE ***"