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

Method peek

algorithms/data_structures/stack.py:111–122  ·  view source on GitHub ↗

Return the top element without removing it. Returns: The top element. Raises: IndexError: If the stack is empty.

(self)

Source from the content-addressed store, hash-verified

109 return value
110
111 def peek(self) -> object:
112 """Return the top element without removing it.
113
114 Returns:
115 The top element.
116
117 Raises:
118 IndexError: If the stack is empty.
119 """
120 if self.is_empty():
121 raise IndexError("Stack is empty")
122 return self._array[self._top]
123
124 def _expand(self) -> None:
125 """Double the size of the underlying array."""

Callers 1

test_array_stackMethod · 0.95

Calls 1

is_emptyMethod · 0.45

Tested by 1

test_array_stackMethod · 0.76