Tower of Hanoi Coding Experiment

From BITPlan cr Wiki
Revision as of 06:55, 23 October 2024 by Wf (talk | contribs) (Created page with "= Tower of Hanoi Module with Python = This page documents the creation of a Python module for solving the Tower of Hanoi problem. The development follows specific coding pref...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Tower of Hanoi Module with Python

This page documents the creation of a Python module for solving the Tower of Hanoi problem. The development follows specific coding preferences and includes three sections: the module implementation, an example usage, and the test code.

Preferences

1. Clean, readable, and maintainable code. 2. Google-style docstrings and type hints for functions needing explanation. 3. Avoid one-liners and list comprehensions to make code debuggable. 4. Never return function results directly; assign them to a variable first. 5. Provide delta lines when refactoring. 6. Preserve header comments and comment out code. 7. Never change an API unless explicitly requested. 8. Keep example and test code separate from implementation. 9. Code modifications must pass existing tests; ask for the test code if a decision is needed. 10. Avoid hallucinations or assumptions. 11. No unsolicited suggestions or comments. 12. No speculative or unnecessary logic. 13. No changes that introduce new dependencies without feedback. 14. Responses should be concise and use direct, specific examples.

Module Implementation

# hanoi_solver.py

from typing import List, Tuple

def solve_tower_of_hanoi(n: int, source: str, auxiliary: str, destination: str) -> List[Tuple[str, str]]:
    """
    Solves the Tower of Hanoi problem and returns the sequence of moves.

    Args:
        n (int): The number of disks.
        source (str): The source rod identifier.
        auxiliary (str): The auxiliary rod identifier.
        destination (str): The destination rod identifier.

    Returns:
        List[Tuple[str, str]]: A list of tuples representing the moves, where each tuple is (from_rod, to_rod).
    """
    moves = []
    
    def move_disks(count: int, from_rod: str, to_rod: str, aux_rod: str) -> None:
        if count == 1:
            moves.append((from_rod, to_rod))
        else:
            move_disks(count - 1, from_rod, aux_rod, to_rod)
            moves.append((from_rod, to_rod))
            move_disks(count - 1, aux_rod, to_rod, from_rod)

    move_disks(n, source, destination, auxiliary)
    return moves

Example Usage

from hanoi_solver import solve_tower_of_hanoi

# Example usage
moves = solve_tower_of_hanoi(3, 'A', 'B', 'C')
for move in moves:
    print(f"Move disk from {move[0]} to {move[1]}")

Test Code

def test_solve_tower_of_hanoi():
    expected_moves = [
        ('A', 'C'), ('A', 'B'), ('C', 'B'), 
        ('A', 'C'), ('B', 'A'), ('B', 'C'), ('A', 'C')
    ]
    result = solve_tower_of_hanoi(3, 'A', 'B', 'C')
    assert result == expected_moves, f"Expected {expected_moves} but got {result}"

test_solve_tower_of_hanoi()