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))[Quellcode]#
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()
- clear()[Quellcode]#
Remove every element from the stack.
- Rückgabetyp:
- dialog#
- is_empty()[Quellcode]#
- Rückgabetyp:
- is_full()[Quellcode]#
- Rückgabetyp:
- peek()[Quellcode]#
Return the top element without removing it.
- Rückgabetyp:
- Verursacht:
IndexError – when the stack is empty.
- pop()[Quellcode]#
Remove and return the top element.
- Rückgabetyp:
- Verursacht:
IndexError – when the stack is empty.
- push(value)[Quellcode]#
Push
valueonto the top of the stack.- Rückgabetyp:
- Verursacht:
OverflowError – when the stack is already at capacity.
- values()[Quellcode]#
Return the current elements, bottom-to-top order.