MCPcopy
hub / github.com/keon/algorithms / push

Method push

algorithms/stack/ordered_stack.py:50–64  ·  view source on GitHub ↗

Push an item while maintaining sorted order. Args: item: The value to push.

(self, item: int)

Source from the content-addressed store, hash-verified

48 self.items.append(item)
49
50 def push(self, item: int) -> None:
51 """Push an item while maintaining sorted order.
52
53 Args:
54 item: The value to push.
55 """
56 temp_stack = OrderedStack()
57 if self.is_empty() or item > self.peek():
58 self._push_direct(item)
59 else:
60 while item < self.peek() and not self.is_empty():
61 temp_stack._push_direct(self.pop())
62 self._push_direct(item)
63 while not temp_stack.is_empty():
64 self._push_direct(temp_stack.pop())
65
66 def pop(self) -> int:
67 """Remove and return the top element.

Callers 1

test_ordered_stackMethod · 0.95

Calls 5

is_emptyMethod · 0.95
peekMethod · 0.95
_push_directMethod · 0.95
popMethod · 0.95
OrderedStackClass · 0.85

Tested by 1

test_ordered_stackMethod · 0.76