From d65abee274d1ea60b7fefe3ab5414c8be546dd0a Mon Sep 17 00:00:00 2001 From: Yashwanth Sai <68785131+theyashwanthsai@users.noreply.github.com> Date: Tue, 11 Oct 2022 19:03:13 +0530 Subject: [PATCH 1/3] Create Tower of Hanoi.md --- Python/Tower of Hanoi/Tower of Hanoi.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Python/Tower of Hanoi/Tower of Hanoi.md diff --git a/Python/Tower of Hanoi/Tower of Hanoi.md b/Python/Tower of Hanoi/Tower of Hanoi.md new file mode 100644 index 0000000..a13f4b2 --- /dev/null +++ b/Python/Tower of Hanoi/Tower of Hanoi.md @@ -0,0 +1,12 @@ +# Tower of Hanoi + +This is a very popular puzzle. There are 3 rods A, B, C. The goal is to move the stack of disks from A to C while following the rules below: + +- Only one disk can be moved at a time and it has to be the upper disk in the stack. +- Bigger disk should not be placed on top of smaller disc. + +For a N disks, to move from A to C, + +1. Shift N-1 disks to B using C as a temporary rod. +2. Shift the last disk to C. +3. Now use A as a temporary rod and shift all the disks from B to C. From 690efc0ce0d0c4fe477b97712815a0dc3bba117a Mon Sep 17 00:00:00 2001 From: Yashwanth Sai <68785131+theyashwanthsai@users.noreply.github.com> Date: Tue, 11 Oct 2022 19:04:14 +0530 Subject: [PATCH 2/3] Add files via upload --- Python/Tower of Hanoi/towerofhanoi.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Python/Tower of Hanoi/towerofhanoi.py diff --git a/Python/Tower of Hanoi/towerofhanoi.py b/Python/Tower of Hanoi/towerofhanoi.py new file mode 100644 index 0000000..4b630e3 --- /dev/null +++ b/Python/Tower of Hanoi/towerofhanoi.py @@ -0,0 +1,14 @@ +# Recursive logic to solve tower of hanoi problem +def tower_of_hanoi(number, source, temporary, destination): + if number == 1: + print("Move disk 1 from", source, "to", destination) + return + tower_of_hanoi(n-1, source, temporary, destination) + print("Move disk", n, "from", source, "to", destination) + tower_of_hanoi(n-1, temporary, destination, source) + + +n = int(input("Enter the number of disks")) +tower_of_hanoi(n, 'A', 'B', 'C') +# A is the source, C is the destination + \ No newline at end of file From 1eaaa72d29d9ad926bf81812373a0962e04a8955 Mon Sep 17 00:00:00 2001 From: Yashwanth Sai <68785131+theyashwanthsai@users.noreply.github.com> Date: Tue, 11 Oct 2022 19:05:32 +0530 Subject: [PATCH 3/3] Update towerofhanoi.py --- Python/Tower of Hanoi/towerofhanoi.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Python/Tower of Hanoi/towerofhanoi.py b/Python/Tower of Hanoi/towerofhanoi.py index 4b630e3..4ad4753 100644 --- a/Python/Tower of Hanoi/towerofhanoi.py +++ b/Python/Tower of Hanoi/towerofhanoi.py @@ -7,8 +7,11 @@ def tower_of_hanoi(number, source, temporary, destination): print("Move disk", n, "from", source, "to", destination) tower_of_hanoi(n-1, temporary, destination, source) +# 1. Shift N-1 disks to B using C as a temporary rod. +# 2. Shift the last disk to C. +# 3. Now use A as a temporary rod and shift all the disks from B to C. n = int(input("Enter the number of disks")) tower_of_hanoi(n, 'A', 'B', 'C') # A is the source, C is the destination - \ No newline at end of file +