NUMBER_OF_DISKS = 3
number_of_moves = 2\*\*NUMBER_OF_DISKS - 1
rods = {
‘A’: list(range(NUMBER_OF_DISKS, 0, -1)),
‘B’: [ ],
‘C’: [ ]
}
print(rods)
source = ‘A’
auxiliary = ‘B’
target = ‘C’
for i in range(number_of_moves):
remainder = (i + 1) % 3
forward = False
if remainder == 1:
print(f'Move {i+1} allowed between {source} and {target}')
if not rods[target]:
forward = True
elif rods[source] and rods[source][-1] < rods[target][-1]:
forward = True
elif remainder == 2:
print(f'Move {i+1} allowed between {source} and {auxiliary}')
if not rods[auxiliary]:
forward = True
elif rods[source] and rods[source][-1] < rods[auxiliary][-1]:
forward = True
elif remainder == 0:
print(f'Move {i+1} allowed between {auxiliary} and {target}')
if not rods[target]:
forward = True
elif rods[auxiliary] and rods[auxiliary][-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())
Please Tell us what’s happening in your own words.
Learning to describe problems is hard, but it is an important part of learning how to code.
Also, the more you say, the more we can help!
Welcome to the forum @afifaanjumtrina
If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge. This button only appears if you have tried to submit an answer at least three times.
The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.
Is there a message in the console?
Happy coding
