Towers

class towers.core.towers.Towers(height=1, rods=None, moves=0, verbose=False)[source]

A representation of the towers including all logic.

__bool__()[source]

A Towers is considered True if it’s state is completed.

Return type:bool
__call__()[source]

Run the towers. Convenience method.

Raises:See Towers.move_tower().
__contains__(x)[source]

Does this Towers contain the given Rod.

Parameters:x (Rod) – The Rod to find.
Return type:bool
__copy__()[source]

Return a shallow copy of this instance.

Return type:Towers
__deepcopy__(*d)[source]

Return a deep copy of this instance.

Parameters:d (dict) – Memoisation dict.
Return type:Towers
__enter__()[source]

Context-Manager entry, validate our entry state for towers-start conditions.

Raises:See Towers.validate_start().
__eq__(other)[source]

Compare Towers instances for equivalence.

Parameters:other (Towers) – The other Towers to compare.
Return type:bool
__exit__(*args, **kwargs)[source]

Context-Manager exit, validate our exit state for towers-end conditions.

Raises:See Towers.validate_end().
__getitem__(index)[source]

Get the Rod at the given index.

Parameters:index (int) – The index to get the Rod at.
Return type:Rod
__init__(height=1, rods=None, moves=0, verbose=False)[source]
Parameters:
  • height (int) – The height of the towers (ie: max number of disks each one rod can hold).
  • rods (Rods) – An existing Rods instance to use with this Towers (the heights must match).
  • moves (int) – The number of moves already taken.
  • verbose – True=enable verbose logging mode.
__iter__()[source]

Run the towers, yielding Move instances.

__len__()[source]

Determine how many Rod‘s this Towers contains.

Return type:int
__nonzero__()[source]

A Towers is considered non-zero if it’s state is completed.

Return type:bool
context(*args, **kwds)[source]

Create a temp context for performing moves. The state of this instance will be reset at context exit.

Parameters:
  • reset_on_success (bool) – Reset this instance’s state on exit from the context if no error occurred. Default = True.
  • reset_on_error (bool) – Reset this instance’s state on exit from the context if an error occurred. Default = False.
end_rod

Retrieve the end Rod for this towers.

Return type:Rod
classmethod from_json(d)[source]

Return a class instance from a json serializable representation.

Parameters:d (str|dict) – The json or decoded-json from which to create a new instance.
Return type:Towers
Raises:See Towers.__new__.
height

Obtain the height of the Towers (ie: max number of disks each one rod can hold).

Return type:int
move_disk(start, end)[source]

Move the Disk from one Rod to another.

Note:

Generator, yields Move instances.

Parameters:
  • start (Rod) – The Rod to remove the Disk from.
  • end (Rod) – The Rods to move the Disk to.
move_tower(height, start, end, tmp)[source]

Move the stack of Disks on a Rod.

Parameters:
  • height (int) – The height of the Disk to move.
  • start (Rod) – The Rod to move the Disk from.
  • end (Rod) – The Rod to move the Disk to.
  • tmp (Rod) – The intermediary Rod to use when moving the Disk.
moves

Determine how many moves have occurred so far.

Return type:int
static moves_for_height(height)[source]

Determine the max number of moves required to solve the puzzle for the given height

Parameters:height (int) – The height of the Rods (number of Disk on a Rod).
Return type:int
start_rod

Retrieve the start Rod for this towers.

Return type:Rod
tmp_rod

Retrieve the temporary Rod for this towers.

Return type:Rod
to_json()[source]

Return a json serializable representation of this instance.

Return type:object
validate()[source]

Perform self validation.

Raises:
validate_end()[source]

Validate the end conditions for this towers.

Raises:
validate_start()[source]

Validate the start conditions for this towers

Raises:
verbose

Obtain this instance’s verbose flag.

Return type:bool