Back to Freecodecamp

Step 17

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

latest1.6 KB
Original Source

--description--

The allowed disk movements between the rods exhibit a repetitive pattern occurring every three moves. For example, movements between rod A and rod C are allowed on the first, the fourth and the seventh move, where the remainder of the division between the move number and 3 is equal to 1.

Inside the previously created for loop, replace the existing print() call with an if statement that is triggered when (i + 1) % 3 == 1. Within this if statement, print f'Move {i + 1} allowed between {source} and {target}' using an f-string. Please, note that i + 1 is the move number since i is zero during the first iteration.

--hints--

You should have an if statement that should be executed when (i + 1) % 3 == 1.

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

You should print the string f'Move {i + 1} allowed between {source} and {target}' using an f-string.

js
({test: () => assert.match(code, /print\s*\(\s*f('|")Move\s\{\s*i\s*\+\s*1\s*\}\sallowed\sbetween\s\{\s*source\s*\}\sand\s\{\s*target\s*\s*\}\1\s*\)/)})

--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):
        print(i)
--fcc-editable-region--

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