Skip to main content

Practice · 1 of 1

A generic Stack[T]

CHALLENGE
Difficulty: advanced+25 XP

Implement Stack(Generic[T]) with push(item: T), pop() -> T (IndexError on empty), peek() -> T (IndexError on empty), and __len__. Also implement first(xs: List[T]) -> T raising ValueError('empty sequence') on an empty list. Runtime must behave exactly like the untyped version — the generic parameter is a design contract (your annotations must reference T where relevant).

Back to lesson: Practice: Generics Practice