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(value)

Push a value onto the top. Raises OverflowError when at capacity.

pop()

Remove and return the top value. Raises IndexError on empty stack.

peek()

Return the top value without removing it. Raises IndexError on empty stack.

values()

Return all current elements, bottom-to-top order.

is_empty()

Return True when the stack has no elements.

is_full()

Return True when the stack has reached its capacity.

clear()

Remove all elements.

size

Number of elements currently on the stack (property).

Constructor parameters#

Parameter

Default

Description

capacity

8

Maximum number of elements.

box_width

120

Width of each element box in pixels.

box_height

40

Height of each element box in pixels.

margin

10

Margin around the stack in pixels.

background

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:

None

dialog#
is_empty()[Quellcode]#
Rückgabetyp:

bool

is_full()[Quellcode]#
Rückgabetyp:

bool

peek()[Quellcode]#

Return the top element without removing it.

Rückgabetyp:

object

Verursacht:

IndexError – when the stack is empty.

pop()[Quellcode]#

Remove and return the top element.

Rückgabetyp:

object

Verursacht:

IndexError – when the stack is empty.

push(value)[Quellcode]#

Push value onto the top of the stack.

Rückgabetyp:

None

Verursacht:

OverflowError – when the stack is already at capacity.

property size: int#

Number of elements currently on the stack.

values()[Quellcode]#

Return the current elements, bottom-to-top order.

Rückgabetyp:

List[object]