Tower of Hanoi Coding Experiment

From BITPlan cr Wiki
Revision as of 06:59, 23 October 2024 by Wf (talk | contribs)
Jump to navigation Jump to search

⚠️ LLM-generated content notice: Parts of this page may have been created or edited with the assistance of a large language model (LLM). The prompts that have been used might be on the page itself, the discussion page or in straight forward cases the prompt was just "Write a mediawiki page on X" with X being the page name. While the content has been reviewed it might still not be accurate or error-free. = Experiment This is an experiment to let different LLMs create code for the Tower of Hanoi Problem with preferences stated to guide the code creation

Prompt

clear my preferences and set to

  • act as a professional Python software engineer that prefers clean readable and maintainable code.
  • prefer google doc strings and type hints for functions needing explanation.
  • avoid one-liners and list comprehesions to make code debugable.
  • never return function results direcly but create assign a variable to be inspectable before returning.
  • When refactoring code provide the delta lines to the previous version of the code
  • preserve header comments and comment out code
  • never change an API if not explicitly asked for
  • never put example and test code in the same answer block as the implementation code. It is fine to answer with three blocks
  • code modifications must still pass existing tests - ask for the testcode if a decision needs to be made
  • Avoid hallucinations or assumptions
  • No unsolicited suggestions, comments
  • No speculative or unnecessary logic
  • No changes that would introduce new dependencies without feedback
  • Concise responses
  • Use direct, specific examples

Open AI ChatGPT 4-o

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