improvement

This commit is contained in:
Harshil Darji 2016-08-14 17:43:32 +05:30
parent 8d5be8a436
commit 45af05b1af
2 changed files with 25 additions and 33 deletions

View File

@ -1,33 +0,0 @@
F=[];
U=[];
T=[];
def show():
print "\n\nF : ", F;
print "U : ", U;
print "T : ", T;
def mov(From, To):
To.append(From.pop());
def TOH(n, From, Using, To):
if n==1:
mov(From, To);
show();
else:
TOH(n-1, From, To, Using);
mov(From, To);
show();
TOH(n-1, Using, From, To);
n=input("Enter The Height of Hanoi Tower : ");
F=range(n,0,-1);
show();
TOH(n, F, U, T);

25
other/tower_of_hanoi.py Normal file
View File

@ -0,0 +1,25 @@
def moveTower(height, fromPole, toPole, withPole):
'''
>>> moveTower(3, 'A', 'B', 'C')
moving disk from A to B
moving disk from A to C
moving disk from B to C
moving disk from A to B
moving disk from C to A
moving disk from C to B
moving disk from A to B
'''
if height >= 1:
moveTower(height-1, fromPole, withPole, toPole)
moveDisk(fromPole, toPole)
moveTower(height-1, withPole, toPole, fromPole)
def moveDisk(fp,tp):
print('moving disk from', fp, 'to', tp)
def main():
height = int(input('Height of hanoi: '))
moveTower(height, 'A', 'B', 'C')
if __name__ == '__main__':
main()