非再帰的アプローチを使用して、ハノイの塔の問題に対して次のコードを作成しました。移動の数が 2**n - 1 ではないため、正しくないと思います。たとえば、3 つのディスクを移動するには、7 つの移動を生成する必要があります。前もって感謝します。
######################
# Towers of Hanoi #
######################
numbers = []
def TowersofHanoi():
# Proram to simulate Towers of hanoi
# Objective is to move the disks from A to C
# with B as a temporary varialbel
print "Program to simulate Towers of Hanoi"
print "Users will input numbers of disks, which is 'A'"
print "The disks have to be moved from A to C"
print "With B as temporary placeholder"
print "Enter the number of disks to be moved:",
Num = int (raw_input("> "))
Src = Num
Aux = 0
Dst = 0
print "you have entered %d disks to be moved"
print "Source is -->", Src
print "Auxillary is -->", Aux
print "Destination is -->", Dst
print "Disk positions after the placement:"
#B = A-1
#print B
while Num >= 1:
print Num
Aux = Num-1
Src = Src-Aux
Dst = Src
print "Source is -->", Src
print "Auxillary is -->", Aux
print "Destination is -->", Dst
Src = Aux
Num = Num-1
numbers.append(Dst)
print numbers
print "The task of accomplishing the movements of disk is over"
print "This completes TOWERS OF HANOI!"
print "Final disk positions are:"
print "Source is -->", Src
print "Auxillary is -->", Aux
print "Destination is -->", len(numbers)
TowersofHanoi()