Question

You will implement the simple Tower of Hanoi problem using Python. You can find the implementation...

You will implement the simple Tower of Hanoi problem using Python. You can find the implementation in just about any book that talks about recursion. Here is the problem description: o There are n disks labled 1,2,3, ..., n and the three towers A, B and C. o No disk can be on top of a smaller disk at anytime. o All the disks are initially placed on tower A. o Only one disk can be moved at a time, and it must be the smallest disk on a tower. What you need to do: 1. Study and understand how recursion works for this problem. 2. Write a Python function and test it first. 3. Paste your Python code 4. Paste your run solution for n=2, n=3 and n=4.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In 1983 Edouard Lucas a French mathematician invented the Tower of Hanoi Problem.

The main constraints of the puzzle are:

  1. Should move only one disk at a time
  2. Should not place larger disk on the top of the smaller disk
  3. Should move only top of the disk

Figure 1 shows an example of a outline of disks in the middle of a move from the first tower to the third tower. As per the rules, the disks on each tower are placed so that smaller disks are always on top of the larger disks.

solving this problem recursively

Suppose we are having a tower of five disks, originally on tower one.

Here is a high-level outline of how to move a tower from the starting tower, to the ending tower, using an intermediate tower:

  1. Move a tower of height-1 to an intermediate tower, using the final tower.
  2. Move the remaining disk to the final tower.
  3. Move the tower of height-1 from the intermediate tower to the final tower using the original tower.

Here the Tower of Hanoi problem is a tower of one disk. In this case, we need move only a single disk to its final destination tower. A tower of one disk will be our base case

1. Create function TOH which takes the number of disk n and the names of the tower1, tower2 and tower3 as arguments.
2. Base case: When number of disks is n=1, int this case move disk from tower1 to tower3 and return.
3. Move n – 1 disks from tower1 to tower2 using the tower3 as the intermediate.
4. Move the one remaining disk on the tower1 to the tower3.
5. Move the n – 1 disks on the tower2 to the tower3 using the tower1 as the intermediate.

For n disks, total 2n – 1 moves are required.

Ex:

If n=2

Total moves from source to destination 22-1= 3

If n=3

Total moves from source to destination 23-1= 7

If n=4

Total moves from source to destination 24-1= 15

#Python Implementation

def TOH(disk, Tower1, IntermediateTower2, Finaltower3):
if disk == 1:
print('Move disk 1 from Tower {} to Tower {}.'.format(Tower1, Finaltower3)) # base case
return

TOH(disk - 1, Tower1, Finaltower3, IntermediateTower2)  #here tower2 acts as the intermendiate tower
print('Move disk {} from Tower {} to Tower {}.'.format(disk, Tower1, Finaltower3))
TOH(disk - 1, IntermediateTower2, Tower1, Finaltower3)  # here tower1 acts as the intermediate tower


disk = int(input('Enter number of disks: '))
TOH(disk, 'A', 'B', 'C')

# here A is the Sourse tower i.e first tower1

# B is the Intermediate Tower i.e tower2

# C is the taget Tower i.e Finaltower3

#Output

>>>
===================== RESTART: E:/Desktop 7/HomeworkLib/TOH.py =====================
Enter number of disks: 2
Move disk 1 from Tower A to Tower B.
Move disk 2 from Tower A to Tower C.
Move disk 1 from Tower B to Tower C.
>>>
===================== RESTART: E:/Desktop 7/HomeworkLib/TOH.py =====================
Enter number of disks: 3
Move disk 1 from Tower A to Tower C.
Move disk 2 from Tower A to Tower B.
Move disk 1 from Tower C to Tower B.
Move disk 3 from Tower A to Tower C.
Move disk 1 from Tower B to Tower A.
Move disk 2 from Tower B to Tower C.
Move disk 1 from Tower A to Tower C.
>>>
===================== RESTART: E:/Desktop 7/HomeworkLib/TOH.py =====================
Enter number of disks: 4
Move disk 1 from Tower A to Tower B.
Move disk 2 from Tower A to Tower C.
Move disk 1 from Tower B to Tower C.
Move disk 3 from Tower A to Tower B.
Move disk 1 from Tower C to Tower A.
Move disk 2 from Tower C to Tower B.
Move disk 1 from Tower A to Tower B.
Move disk 4 from Tower A to Tower C.
Move disk 1 from Tower B to Tower C.
Move disk 2 from Tower B to Tower A.
Move disk 1 from Tower C to Tower A.
Move disk 3 from Tower B to Tower C.
Move disk 1 from Tower A to Tower B.
Move disk 2 from Tower A to Tower C.
Move disk 1 from Tower B to Tower C.
>>>

Add a comment
Know the answer?
Add Answer to:
You will implement the simple Tower of Hanoi problem using Python. You can find the implementation...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Recursion Write a program to solve the Towers of Hanoi problem for a tower of size...

    Recursion Write a program to solve the Towers of Hanoi problem for a tower of size n, using both recursion and iteration. Time each method separately. Be very carefull to time only the actual work and avoid superfluous module calls and initialization, etc. Compare and contrast your two versions of the problem. Are they what you expected? Your analysis must contain a table of the times obtained for each run. For a tower of a particular size, your output should...

  • Write the recursive MIPS code (with abundant explanatory comments) for the Tower of Hanoi problem of...

    Write the recursive MIPS code (with abundant explanatory comments) for the Tower of Hanoi problem of transferring a stack of N disks (smaller sized disks stacked over the larger sized ones) from a source peg to a destination peg via a third (temporary rest peg) under the constraints: 1. Only one disk is moved at a time from one peg to another 2. At no time, a larger disk will sit on a smaller one.

  • In the classic problem of the Towers of Hanoi, you have 3 rods and N disks...

    In the classic problem of the Towers of Hanoi, you have 3 rods and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size from top to bottom (e.g., each disk sits on top of an even larger one).You have the following constraints: (A) Only one disk can be moved at a time. (B) A disk is slid off the top of one rod onto the next rod....

  • dont use a struct use the std::stack library In this assignment, you will finish the implementation...

    dont use a struct use the std::stack library In this assignment, you will finish the implementation of an iterative solution to the Towers of Hanoi puzzle. Specifically, you will implement the puzzle initialization and the move operation utilizing the stacks provided. Do not rename existing functions, nor add any additional functions nor member variables! REQUIREMENTS The program will read in a text file with containing a single integer: the number of disks for the puzzle. The program will output the...

  • Writing in Python. Need help making this program: Hanoi's tower is an old game that can...

    Writing in Python. Need help making this program: Hanoi's tower is an old game that can be solved with a recursive algorithm. Wikipedia contains example implementations of this. You will write a variant of this implementation that prints the condition of the three stacks of slices after each move. Remember that the different recursive calls will refer to different stacks, so in order to print a status that is comparable between calls, the method that prints the stacks must have...

  • Please write a recursive Java program to solve the Tower of Hanoi game for n disks...

    Please write a recursive Java program to solve the Tower of Hanoi game for n disks on pole A. Please read the textbook page 176 – 180 to fully understand this game or puzzle. The game consists of n disks and three poles: A (the source), B (the destination), and C (the spare). Initially, all the disks are on pole A. The game is to move all disks (one by one) from pole A to pole B using pole C...

  • write in c programming language . question 5.36 Fibonaco for its rets ing terms, a) Write...

    write in c programming language . question 5.36 Fibonaco for its rets ing terms, a) Write a warruse function fibonacci(n) that calculatus 0, 1, 1, 2, 3, 5, 8, 13, 21,... (n) that calculates the n" Fib begins with the terms and 1 and has the property preceding terms. a) Write a cand unsigned long long int for i number. Use unsigned int for the function's paramete her that can be printed on your system. type. b) Determine the largest...

  • Program Purpose In this program you will demonstrate your knowledge in programming OOP concepts, such as classes, encapsulation, and procedural programming concepts such as lınked lists, dynamic me...

    Program Purpose In this program you will demonstrate your knowledge in programming OOP concepts, such as classes, encapsulation, and procedural programming concepts such as lınked lists, dynamic memory allocation, pointers, recursion, and debugging Mandatory Instructions Develop a C++ object oriented solution to the Towers of Hanoi puzzle. Your solution will involve designing two classes one to represent individual Disk and another to represent the TowersOfHanoi game. TowersOfHanoi class will implement the game with three linked lists representing disks on each...

  • Towers of Hanoi (15 points) Given: n disks, all of different sizes the size of the...

    Towers of Hanoi (15 points) Given: n disks, all of different sizes the size of the ith disk is į .3 pegs - A, B, C the number of pegs might change in a future version of the game Inally, all the disks are stacked on peg A Requirement: Never place a larger disk on top of a smaller disk (disk 5 is larger than disk 4, etc.) Objective: Move the disks from peg A to peg C Input: n,...

  • A python algorithm written for Tower Of Hanoi. class Towers:     hasItMoved = []     toDest = []...

    A python algorithm written for Tower Of Hanoi. class Towers:     hasItMoved = []     toDest = []     def __init__(self):         self.current = 1         self.n = int(input("Enter the number of disks: "))         self.hasItMoved = [None] * 12         self.toDest = [1] * 12         for i in range(self.n):             self.toDest[i] = 0         self.r = self.n         self.hanoiStart(self.n, "Start", "Aux1", "Aux3", "Aux2", "Dest", self.current) # function to start     def hanoiStart(self, numOfDisks, start, source, dest, aux, last, current):         self.move(1, start, source, self.current)         self.current += 1         self.H1(self.n, "Start", "Aux1", "Aux3",...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT