StackWorld#
StackWorld visualizes a LIFO stack as a column of boxes growing upward.
The topmost box is highlighted in red to indicate the top-of-stack.
Operations#
Method |
Description |
|---|---|
|
Push a value onto the top. Raises |
|
Remove and return the top value. Raises |
|
Return the top value without removing it. Raises |
|
Return all current elements, bottom-to-top order. |
|
Return |
|
Return |
|
Remove all elements. |
|
Number of elements currently on the stack (property). |
Constructor parameters#
Parameter |
Default |
Description |
|---|---|---|
|
|
Maximum number of elements. |
|
|
Width of each element box in pixels. |
|
|
Height of each element box in pixels. |
|
|
Margin around the stack in pixels. |
|
light grey |
Background fill color. |
Example#
from miniworlds_data import StackWorld
world = StackWorld(capacity=5)
world.push("A")
world.push("B")
world.push("C")
print(world.peek()) # "C"
print(world.pop()) # "C"
world.run()
API Reference#
- class miniworlds_data.stack_world.StackWorld(*, capacity=8, box_width=120, box_height=40, margin=10, background=(245, 245, 245, 255))[source]#
Pixel world that visualizes a LIFO stack growing upward.
Elements are pushed onto the top and popped from the top. The topmost box is highlighted in red.
Example
from miniworlds_data import StackWorld world = StackWorld() world.push(3) world.push(7) world.push(2) print(world.peek()) # 2 print(world.pop()) # 2 world.run()
- dialog#
- peek()[source]#
Return the top element without removing it.
- Return type:
- Raises:
IndexError – when the stack is empty.
- pop()[source]#
Remove and return the top element.
- Return type:
- Raises:
IndexError – when the stack is empty.
- push(value)[source]#
Push
valueonto the top of the stack.- Return type:
- Raises:
OverflowError – when the stack is already at capacity.