def explore_stack(): stack = list() stack.append(Mate("Alice", 1)) stack.append(Mate("Bob", 3)) stack.append(Mate("Cara", 6)) stack.append(Mate("Dan", 5)) stack.append(Mate("Emile", 2)) print(stack)
def explore_priority_queue(): sorted_list = list() bisect.insort(sorted_list, Mate("Alice", 1)) bisect.insort(sorted_list, Mate("Bob", 3)) bisect.insort(sorted_list, Mate("Cara", 6)) bisect.insort(sorted_list, Mate("Dan", 5)) bisect.insort(sorted_list, Mate("Emile", 2)) print(sorted_list)
def explore_queue(): queue = list() queue.append(Mate("Alice", 1)) queue.append(Mate("Bob", 3)) queue.append(Mate("Cara", 6)) queue.append(Mate("Dan", 5)) queue.append(Mate("Emile", 2)) print(queue)
def explore_sort(): l = list() l.append(Mate("Alice", 1)) l.append(Mate("Bob", 3)) l.append(Mate("Cara", 6)) l.append(Mate("Dan", 5)) l.append(Mate("Emile", 2)) l.sort() print(l)
def test_lt(self): try: for i in range(10): a_value = random.randrange(-3, 3) b_value = random.randrange(-3, 3) expected = a_value < b_value a = Mate("Kim", a_value) b = Mate("Fin", b_value) result = a < b if result != expected: pytest.fail( "Method '__lt__' failed for '(Kim, {}) < (Fin, {})'." "\n Returned {}. Expected {}.".format( a_value, b_value, result, expected)) return None except: pytest.fail("The method '__lt__' does not quite work yet.") return None
def test_repr(self): try: a = Mate("Kim", 42) result = str(a) if result != '(Kim, 42)': pytest.fail("Method '__repr__' failed to return '(Kim, 42)'," "\n instead returned {}".format(result)) except: pytest.fail("The method '__repr__' does not quite work yet") return None
def test_insert_left(self): alice = Mate("Alice", 14) bob = Mate("Bob", 14) carol = Mate("Carol", 13) dave = Mate("Dave", 14) eve = Mate("Eve", 15) a_list = [alice, bob, carol, dave, eve] a_list.sort() bob.value = 15 update_element_left(a_list, bob) assert a_list == [carol, alice, dave, bob, eve], \ "The list should be [(Carol, 13), (Alice, 14), (Dave, 14), (Bob, 15), (Eve, 15)] " bob.value = 14 update_element_left(a_list, bob) assert a_list == [carol, bob, alice, dave, eve], \ "The list should be [(Carol, 13), (Bob, 14), (Alice, 14), (Dave, 14), (Eve, 15)] " bob.value = 13 update_element_left(a_list, bob) assert a_list == [bob, carol, alice, dave, eve], \ "The list should be [(Bob, 13), (Carol, 13), (Alice, 14), (Dave, 14), (Eve, 15)] " frank = Mate("Frank", "35") update_element_left(a_list, frank) assert a_list == [bob, carol, alice, dave, eve], \ "The list should be [(Bob, 13), (Carol, 13), (Alice, 14), (Dave, 14), (Eve, 15)] "