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

Method peek

data_structures/stacks/stack.py:76–93  ·  view source on GitHub ↗

Peek at the top-most element of the stack. >>> S = Stack() >>> S.push(-5) >>> S.push(10) >>> S.peek() 10 >>> Stack().peek() Traceback (most recent call last): ... data_structures.stacks.stack.StackUnderflowError

(self)

Source from the content-addressed store, hash-verified

74 return self.stack.pop()
75
76 def peek(self) -> T:
77 """
78 Peek at the top-most element of the stack.
79
80 >>> S = Stack()
81 >>> S.push(-5)
82 >>> S.push(10)
83 >>> S.peek()
84 10
85
86 >>> Stack().peek()
87 Traceback (most recent call last):
88 ...
89 data_structures.stacks.stack.StackUnderflowError
90 """
91 if not self.stack:
92 raise StackUnderflowError
93 return self.stack[-1]
94
95 def is_empty(self) -> bool:
96 """

Callers 3

infix_to_postfixFunction · 0.95
test_stackFunction · 0.95

Calls

no outgoing calls

Tested by 1

test_stackFunction · 0.76