mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-18 16:27:02 +00:00
improvement
This commit is contained in:
parent
8d5be8a436
commit
45af05b1af
|
@ -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
25
other/tower_of_hanoi.py
Normal 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()
|
Loading…
Reference in New Issue
Block a user