From 45af05b1af5fa5a65bbb8675e9092cf805da3ce3 Mon Sep 17 00:00:00 2001 From: Harshil Darji Date: Sun, 14 Aug 2016 17:43:32 +0530 Subject: [PATCH] improvement --- Tower_of_Hanoi.py | 33 --------------------------------- other/tower_of_hanoi.py | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 33 deletions(-) delete mode 100644 Tower_of_Hanoi.py create mode 100644 other/tower_of_hanoi.py diff --git a/Tower_of_Hanoi.py b/Tower_of_Hanoi.py deleted file mode 100644 index e985ca289..000000000 --- a/Tower_of_Hanoi.py +++ /dev/null @@ -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); - - - diff --git a/other/tower_of_hanoi.py b/other/tower_of_hanoi.py new file mode 100644 index 000000000..de0cb6218 --- /dev/null +++ b/other/tower_of_hanoi.py @@ -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()