sonal bắt đầu chủ đề từ 10 tháng trước

@Sharpcorey ·


Hi,I am having difficulty understanding the code for the Tower of Hanoi problem. I understand the logic as far as implementing the steps that must be followed, but I am not sure of how to write it in code. Any help would be greatly appreciated.

The code I have written so far is below:

def tower_of_hanoi(num_disks, from_peg, to_peg, aux_peg): 
    if num_disks == 1:  
        print("Move 1 from peg", from_peg, "to peg", to_peg)  
        return
    tower_of_hanoi(num_disks - 1, from_peg, aux_peg, to_peg) 
    print("Move", num_disks, "from peg", from_peg, "to peg", to_peg) 
    tower_of_hanoi(num_disks - 1, aux_peg, to_peg, from_peg) 

Thanks in advance for your help!``

Viết câu trả lời

Drop Images

0 Bình luận