I’m working on Step 31 of the Learn Recursion by Solving the Tower of Hanoi Puzzle challenge, but I’m stuck. I’ve tried multiple approaches, but the test still doesn’t pass. I understand that some code needs to be moved to a new function, but I’m unsure how much of it should be moved.
I reset the lesson to avoid confusion, so now I’m wondering: Should I move everything except the print() statement, or is there a specific part that needs to stay?
If anyone has completed this step, I’d really appreciate some guidance. Thanks in advance!
Your code so far
NUMBER_OF_DISKS = 3
number_of_moves = 2**NUMBER_OF_DISKS - 1
rods = {
'A': list(range(NUMBER_OF_DISKS, 0, -1)),
'B': [],
'C': []
}
# User Editable Region
def make_allowed_move(rod1, rod2):
pass
def move(n, source, auxiliary, target):
# display starting configuration
print(rods)
for i in range(number_of_moves):
remainder = (i + 1) % 3
if remainder == 1:
print(f'Move {i + 1} allowed between {source} and {target}')
forward = False
if not rods[target]:
forward = True
elif rods[source] and rods[source][-1] < rods[target][-1]:
forward = True
if forward:
print(f'Moving disk {rods[source][-1]} from {source} to {target}')
rods[target].append(rods[source].pop())
else:
print(f'Moving disk {rods[target][-1]} from {target} to {source}')
rods[source].append(rods[target].pop())
# display our progress
print(rods)
# User Editable Region
elif remainder == 2:
print(f'Move {i + 1} allowed between {source} and {auxiliary}')
elif remainder == 0:
print(f'Move {i + 1} allowed between {auxiliary} and {target}')
# initiate call from source A to target C with auxiliary B
move(NUMBER_OF_DISKS, 'A', 'B', 'C')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Challenge Information:
Learn Recursion by Solving the Tower of Hanoi Puzzle - Step 31
Move the code nested inside the first if statement (except the first print() call) to your new function. Pay close attention to the indentation. Don’t forget to remove the pass keyword.
if remainder == 1:
print(f'Move {i + 1} allowed between {source} and {target}')
forward = False
if not rods[target]:
forward = True
elif rods[source] and rods[source][-1] < rods[target][-1]:
forward = True
if forward:
print(f'Moving disk {rods[source][-1]} from {source} to {target}')
rods[target].append(rods[source].pop())
else:
print(f'Moving disk {rods[target][-1]} from {target} to {source}')
rods[source].append(rods[target].pop())
# display our progress
print(rods)
Sorry, your code does not pass. You're getting there.
You should move the code nested inside the first if statement (except the first print() call) to your new function.
But did I?
def make_allowed_move(rod1, rod2):
if remainder == 1:
print(f'Move {i + 1} allowed between {source} and {target}')
forward = False
if not rods[target]:
forward = True
elif rods[source] and rods[source][-1] < rods[target][-1]:
forward = True
if forward:
print(f'Moving disk {rods[source][-1]} from {source} to {target}')
rods[target].append(rods[source].pop())
else:
print(f'Moving disk {rods[target][-1]} from {target} to {source}')
rods[source].append(rods[target].pop())
# display our progress
print(rods)
def move(n, source, auxiliary, target):
# display starting configuration
print(rods)
for i in range(number_of_moves):
remainder = (i + 1) % 3
inside the first if statement does not include the if statement itself. The first line nested inside the if statement, and after the first print() is forward = False.
def make_allowed_move(rod1, rod2):
if not rods[target]:
forward = True
elif rods[source] and rods[source][-1] < rods[target][-1]:
forward = True
if forward:
print(f'Moving disk {rods[source][-1]} from {source} to {target}')
rods[target].append(rods[source].pop())
else:
print(f'Moving disk {rods[target][-1]} from {target} to {source}')
rods[source].append(rods[target].pop())
# display our progress
print(rods)
def move(n, source, auxiliary, target):
# display starting configuration
print(rods)
for i in range(number_of_moves):
remainder = (i + 1) % 3
if remainder == 1:
print(f'Move {i + 1} allowed between {source} and {target}')
forward = False
Hmm Console giving error:
Sorry, your code does not pass. Keep trying.
You should move the code nested inside the first if statement (except the first print() call) to your new function.
and perview not:
{'A': [3, 2, 1], 'B': [], 'C': []}
Move 1 allowed between A and C
Move 2 allowed between A and B
Move 3 allowed between B and C
Move 4 allowed between A and C
Move 5 allowed between A and B
Move 6 allowed between B and C
Move 7 allowed between A and C
def move(n, source, auxiliary, target):
# display starting configuration
print(rods)
for i in range(number_of_moves):
remainder = (i + 1) % 3
if remainder == 1:
print(f'Move {i + 1} allowed between {source} and {target}')
forward = False
Only these are in old functions and for forward it is throwing error if i place it to new function.
And these in new function:
def make_allowed_move(rod1, rod2):
if not rods[target]:
forward = True
elif rods[source] and rods[source][-1] < rods[target][-1]:
forward = True
if forward:
print(f'Moving disk {rods[source][-1]} from {source} to {target}')
rods[target].append(rods[source].pop())
else:
print(f'Moving disk {rods[target][-1]} from {target} to {source}')
rods[source].append(rods[target].pop())
# display our progress
print(rods)
Python position is harder than CSS centering! I finally passed it—just had to fix the position of forward = False in new function. Thanks for the help!