Practice · 1 of 3
Protocol-complete Stack
Implement class Stack that supports:
- push(x), pop() (raise IndexError on empty), peek() (raise IndexError on empty)
- len(stack), stack[i] (int indexing, negative included), x in stack, iteration (bottom→top)
- slicing: stack[1:3] returns a NEW Stack
- __eq__ against another Stack comparing element order (return NotImplemented for non-Stack)
- __repr__ as Stack(<items joined by ', '>)
Difficulty: advanced
Press Submit to check your solution.
Back to lesson: Practice: Custom Container Practice