Back to Freecodecamp

Step 18

curriculum/challenges/english/blocks/learn-recursion-by-solving-the-tower-of-hanoi-puzzle/65782342850feb3b8d62f936.md

latest1.1 KB
Original Source

--description--

Since you are going to use the expression (i + 1) % 3 multiple times, it is convenient to store it in a variable.

Just above your if statement, declare a remainder variable and assign the value (i + 1) % 3 to this variable.

--hints--

You should declare a remainder variable just above the if block.

js
({ test: () => assert.match(code, /^\s{8}remainder\s*=/m) })

The value of your remainder variable should be the expression (i + 1) % 3.

js
({ test: () => assert.match(code, /^\s{8}remainder\s*=\s*\(\s*i\s*\+\s*1\s*\)\s*%\s*3/m) })

--seed--

--seed-contents--

py
NUMBER_OF_DISKS = 3
number_of_moves = 2**NUMBER_OF_DISKS - 1
rods = {
    'A': list(range(NUMBER_OF_DISKS, 0, -1)),
    'B': [],
    'C': []
}

--fcc-editable-region--
def move(n, source, auxiliary, target):
    # display starting configuration
    print(rods)
    for i in range(number_of_moves):
        
        if (i + 1) % 3 == 1:
            print(f'Move {i + 1} allowed between {source} and {target}')
--fcc-editable-region--

# initiate call from source A to target C with auxiliary B
move(NUMBER_OF_DISKS, 'A', 'B', 'C')