Sponsored Reserved space — layout preview until AdSense is connected
easy +12 pts

Stack class

LIFO with exception on underflow.

Implement class `Stack` with: - `push(val)` — add to top - `pop() -> any` — remove and return top; raise `IndexError` if empty - `peek() -> any` — return top without removing; raise `IndexError` if empty - `is_empty() -> bool` - `size() -> int`

Constraints

Any hashable values.

Example

s = Stack()
s.push(1); s.push(2)
s.pop()  # 2
s.peek() # 1
12 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use a list internally; pop() maps to list.pop().
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.
Sponsored Reserved space — layout preview until AdSense is connected