def test_peek(self): my_stack.push(3) my_stack.push(5) self.assertEqual(5, my_stack.peek()) self.assertEqual(3, my_stack.peek(1)) self.assertEqual(5, my_stack.peek())
def test_peek(self): my_stack.push(3) my_stack.push(5) self.assertEqual(my_stack.peek(), 5) self.assertEqual(my_stack.peek(1), 3) self.assertEqual(my_stack.peek(), 5)
def test_multiple_pushes_pops(self): items = [i for i in range(10)] for i in items: my_stack.push(i) received_items = [] for _ in items: received_items.append(my_stack.pop()) self.assertEqual(items[::-1], received_items)
def test_multiple_pushes_pops(self): items = [i for i in range(10)] for i in items: my_stack.push(i) received_items = [] for _ in items: received_items.append(my_stack.pop()) # Добавила преобразование в list() первого аргумента, иначе типы сравниваемых объектов не совпадали self.assertEqual(list(reversed(items)), received_items)
def dfs(g: nx.Graph, start_node: Hashable) -> List[Hashable]: """ Do an depth-first search and returns list of nodes in the visited order :param g: input graph :param start_node: starting node of search :return: list of nodes in the visited order """ visited = [] st.push(start_node) while st.leng() > 0: start_node = st.pop() visited.append(start_node) neighbors = list(g.neighbors(start_node)) for neigh in neighbors: if neigh not in visited: st.push(neigh) return visited
def check_brackets(brackets_row: str) -> bool: """ Check whether input string is a valid bracket sequence Valid examples: "", "()", "()()(()())", invalid: "(", ")", ")(" :param brackets_row: input string to be checked :return: True if valid, False otherwise """ m.clear() for i in brackets_row: global some_list if i == "(": m.push(i) if i == ")": if len(some_list) > 0: m.pop() else: return False print(some_list) return len(some_list) == 0
def dfs(g: nx.Graph, start_node: Hashable) -> List[Hashable]: """ Do an depth-first search and returns list of nodes in the visited order :param g: input graph :param start_node: starting node of search :return: list of nodes in the visited order """ list_of_visit = [] push(start_node) while peek(0) != None: node_now = pop() list_of_visit.append(node_now) neighbor_list = list(g.neighbors(node_now)) for i in neighbor_list: if i in list_of_visit: pass else: push(i) return list_of_visit
def check_brackets(brackets_row: str) -> bool: """ Check whether input string is a valid bracket sequence Valid examples: "", "()", "()()(()())", invalid: "(", ")", ")(" :param brackets_row: input string to be checked :return: True if valid, False otherwise """ import regex stack.clear() for simb in brackets_row: if simb == "(": stack.push(")") elif simb == ")": if simb != stack.pop(): return False if len(stack.lis) != 0: return False return True
def test_peek(self): my_stack.push(7) my_stack.push(3) my_stack.push(5) self.assertEqual(5, my_stack.peek()) self.assertEqual(3, my_stack.peek(1)) self.assertEqual(5, my_stack.peek()) self.assertIsNone(my_stack.peek(100), msg="Should return None if no elements")
def test_clear(self): my_stack.push(3) my_stack.clear() self.assertIsNone(my_stack.pop())
def test_push_pop(self): initial_elem = 3 my_stack.push(initial_elem) self.assertEqual(initial_elem, my_stack.pop())
def test_clear(self): my_stack.push(3) my_stack.clear() self.assertIsNone(my_stack.pop(), msg="Did you clear the stack?")