From c1576239217b266ad7f307f3391c5ae52d7bd269 Mon Sep 17 00:00:00 2001 From: tushar ulhare Date: Thu, 24 Oct 2019 11:14:25 +0530 Subject: [PATCH] Create TowerOfHanoi.py Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules: 1) Only one disk can be moved at a time. 2) Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack. 3) No disk may be placed on top of a smaller disk. --- TowerOfHanoi.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 TowerOfHanoi.py diff --git a/TowerOfHanoi.py b/TowerOfHanoi.py new file mode 100644 index 0000000..463fe47 --- /dev/null +++ b/TowerOfHanoi.py @@ -0,0 +1,12 @@ + +def TowerOfHanoi(n , from_rod, to_rod, aux_rod): + if n == 1: + print "Move disk 1 from rod",from_rod,"to rod",to_rod + return + TowerOfHanoi(n-1, from_rod, aux_rod, to_rod) + print "Move disk",n,"from rod",from_rod,"to rod",to_rod + TowerOfHanoi(n-1, aux_rod, to_rod, from_rod) + +n = 4 +TowerOfHanoi(n, \'A\', \'C\', \'B\') +