Tell us what’s happening:
I’ve already implemented the logic for the algorithm, but I can’t figure out what is wrong with the formatting. Can anyone please explain to me what is wrong with the return string?
Your code so far
rod1 = []
rod2=[]
rod3=[]
def hanoi_solver(num_of_disks: int) -> str:
return_str = ''
global rod1
rod1 = [disk for disk in range(num_of_disks, 0, -1)]
return_str = get_lists_string()
return_str += move_tower(rod1, rod3, rod2, num_of_disks)
return_str = return_str.strip()
return return_str
def move_tower(start, target, auxiliary, num_of_disks)->str:
if(num_of_disks == 1):
move_disk(start, target)
return get_lists_string()
result = ""
result += move_tower(start, auxiliary, target, num_of_disks-1)
move_disk(start, target)
result += get_lists_string()
result += move_tower(auxiliary, target, start, num_of_disks-1)
return result
def move_disk(start, target):
if(len(start) < 1):
print("Cannot move disk for an empty rod.")
return;
target_disk = start[len(start)-1]
start.remove(target_disk)
target.append(target_disk)
def get_lists_string()->str:
return f"{rod1} {rod2} {rod3}\n"
print(hanoi_solver(3))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0
Challenge Information:
Implement the Tower of Hanoi Algorithm - Implement the Tower of Hanoi Algorithm