Tower of Hanoi Coding Experiment
⚠️ 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
Prompts
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
create a module for solving the tower of hanoi
create a mediawiki markup section documenting this interaction with the preferences and the three code blocks as sections using <source lang='python'>
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 movesExample 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()Claude 3.5 Sonnet
Python Coding Preferences
* Professional Python engineer focused on clean, readable, maintainable code * Google docstrings and type hints for functions needing explanation * Avoid one-liners and list comprehensions for debuggability * Use inspectable variables instead of direct returns * Show delta lines for refactoring changes * Preserve header comments, comment out old code * No API changes without explicit request * Separate implementation, example, and test code * Code must pass existing tests * No hallucinations or assumptions * No unsolicited suggestions/comments * No speculative/unnecessary logic * No new dependencies without feedback * Concise responses * Direct, specific examplesImplementation Code
from typing import List, Optional def move_tower(height: int, source: str, target: str, auxiliary: str) -> List[tuple[str, str]]: """Solve Tower of Hanoi puzzle and return list of moves. Args: height: Number of disks in the tower source: Name of source peg target: Name of target peg auxiliary: Name of auxiliary peg Returns: List of tuples containing (source, target) for each move Example: moves = move_tower(3, 'A', 'C', 'B') """ moves: List[tuple[str, str]] = [] def _move_disks(n: int, src: str, tgt: str, aux: str) -> None: if n == 1: moves.append((src, tgt)) return _move_disks(n - 1, src, aux, tgt) moves.append((src, tgt)) _move_disks(n - 1, aux, tgt, src) _move_disks(height, source, target, auxiliary) return movesExample Usage
def print_solution(moves: List[tuple[str, str]]) -> None: """Print the solution moves in readable format.""" for i, (src, tgt) in enumerate(moves, 1): print(f"Move {i}: {src} -> {tgt}") # Solve for 3 disks solution = move_tower(3, 'A', 'C', 'B') print_solution(solution)Test Code
def test_tower_of_hanoi(): # Test with 1 disk result = move_tower(1, 'A', 'C', 'B') assert result == [('A', 'C')], "Single disk move failed" # Test with 2 disks result = move_tower(2, 'A', 'C', 'B') assert result == [('A', 'B'), ('A', 'C'), ('B', 'C')], "Two disk moves failed" # Test with 3 disks result = move_tower(3, 'A', 'C', 'B') expected = [ ('A', 'C'), ('A', 'B'), ('C', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('A', 'C') ] assert result == expected, "Three disk moves failed" print("All tests passed!") test_tower_of_hanoi()