Back to Freecodecamp

Step 54

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

latest1.5 KB
Original Source

--description--

There's still one thing you can do to improve the readability of your code.

Modify your if to execute when n is less than or equal to zero and add a return statement to stop the function execution.

--hints--

You should modify your existing if to check if n is less than or equal to 0.

js
({ test: () =>
  {
    const hanoi = __helpers.python.getDef(code, "move");
    const {function_body} = hanoi;    
    assert(function_body.match(/^\s{4}if\s+n\s*<=\s*0\s*:/m));
  }
})

You should add a return statement to the first line of your if block.

js
({ test: () =>
  {
    const hanoi = __helpers.python.getDef(code, "move");
    const {function_body} = hanoi;    
    assert(function_body.match(/^(\s{4})if\s+n\s*<=\s*0\s*:\s*\1\1return/m));
  }
})

--seed--

--seed-contents--

py
NUMBER_OF_DISKS = 5
A = list(range(NUMBER_OF_DISKS, 0, -1))
B = []
C = []
--fcc-editable-region--
def move(n, source, auxiliary, target):
    if n > 0:

--fcc-editable-region--        
        # move n - 1 disks from source to auxiliary, so they are out of the way
        move(n - 1, source, target, auxiliary)
        
        # move the nth disk from source to target
        target.append(source.pop())
        
        # display our progress
        print(A, B, C, '\n')
        
        # move the n - 1 disks that we left on auxiliary onto target
        move(n - 1,  auxiliary, source, target)
              
# initiate call from source A to target C with auxiliary B
move(NUMBER_OF_DISKS, A, B, C)