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)
| 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 | """ |
no outgoing calls