def check():
for loc in hanoi.keys():
if hanoi[loc] != sorted(hanoi[loc], reverse=True):
raise Exception(f"Incorrect order in {loc}: {hanoi[loc]}")
def move(depth, source, target, helper):
if depth > 0:
move(depth-1, source, helper, target)
val = hanoi[source].pop()
hanoi[target].append(val)
print(f"Move {val} from {source} to {target} Status A:{str(hanoi['A']):10} B:{str(hanoi['B']):10} C:{str(hanoi['C']):10}")
check()
move(depth-1, helper, target, source)
check()
hanoi = {
'A': [4, 3, 2, 1],
'B': [],
'C': [],
}
check()
move(len(hanoi['A']), 'A', 'C', 'B')
check()
def check():
for loc in ['A', 'B', 'C']:
print(f"{loc} {hanoi[loc]}", end=' ')
if hanoi[loc] != sorted(hanoi[loc], reverse=True):
raise Exception(f"Incorrect order in {loc}: {hanoi[loc]}")
print('')
def move(source, target, helper):
#if not hanoi[source]:
# return
if len(hanoi[source]) == 1:
disk = hanoi[source].pop()
print(f"Move {disk} from {source} to {target}")
hanoi[target].append(disk)
return
big_disk = hanoi[source].pop(0) # pretend the biggest disk is not there
move(source, helper, target)
print(f"Move {big_disk} from {source} to {target}")
move(helper, target, source)
hanoi[target].insert(0, big_disk) # stop pretending
check()
hanoi = {
'A': [4, 3, 2, 1],
'B': [],
'C': [],
}
check()
move('A', 'C', 'B')
check()