← Course home

Python reference · Reference

Stacks

Use last-in, first-out behavior for parsing, monotonic scans, and stack-based algorithms.

ReferenceAbout 2 min

This page lists the library modules for Stacks. The lesson is Stack basics.

This page is the module index for stacks.

This legacy page is the implementation inventory. Start with stack basics, then choose monotonic stack patterns or stack parsing and expressions for focused overviews.

  • stacks/stack.py — bounded stack: capacity, decorator guards, amortized O(1) push, O(1) pop and peek.
  • stacks/stack.py — Stack(limit=None) is the unbounded stack used by algorithm lessons. The default limit stays 10.
  • stacks/symbol_balance.py — one pass. O(n) time, O(n) worst extra memory for openers.
  • stacks/min_stack.py — second stack of minima. push, pop, and minimum are O(1).
  • stacks/next_greater.py — monotonic stack. O(n) time, O(n) extra memory.
  • stacks/stock_span.py — same monotonic pattern. O(n) time.
  • stacks/largest_rectangle.py — histogram rectangle. O(n) time.
  • stacks/infix_postfix.py — shunting yard plus postfix evaluation. O(n) time.
  • stacks/sort_stack.py — one extra unbounded stack. Worst case O(n²) time, O(n) extra memory.

Industry

A working engineer uses a Python list as a stack. The language tutorial says to add with append and to retrieve with pop() and no index, so the last item added is the first retrieved. The time-complexity documentation lists an end append and an end pop as amortized O(1).

Implementation and tests Optional reference

Library code 8

  • bitsandbytes.stacks.stack
  • bitsandbytes.stacks.symbol_balance
  • bitsandbytes.stacks.min_stack
  • bitsandbytes.stacks.next_greater

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.