MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / get

Method get

data_structures/queues/queue_by_two_stacks.py:74–106  ·  view source on GitHub ↗

Get `item` from the Queue >>> queue = QueueByTwoStacks((10, 20, 30)) >>> queue.get() 10 >>> queue.put(40) >>> queue.get() 20 >>> queue.get() 30 >>> len(queue) 1 >>> queue.get() 40 >>> qu

(self)

Source from the content-addressed store, hash-verified

72 self._stack1.append(item)
73
74 def get(self) -> T:
75 """
76 Get `item` from the Queue
77
78 >>> queue = QueueByTwoStacks((10, 20, 30))
79 >>> queue.get()
80 10
81 >>> queue.put(40)
82 >>> queue.get()
83 20
84 >>> queue.get()
85 30
86 >>> len(queue)
87 1
88 >>> queue.get()
89 40
90 >>> queue.get()
91 Traceback (most recent call last):
92 ...
93 IndexError: Queue is empty
94 """
95
96 # To reduce number of attribute look-ups in `while` loop.
97 stack1_pop = self._stack1.pop
98 stack2_append = self._stack2.append
99
100 if not self._stack2:
101 while self._stack1:
102 stack2_append(stack1_pop())
103
104 if not self._stack2:
105 raise IndexError("Queue is empty")
106 return self._stack2.pop()
107
108
109if __name__ == "__main__":

Callers

nothing calls this directly

Calls 1

popMethod · 0.45

Tested by

no test coverage detected