Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

use python please and i need at least 3 different test cases or movements of the disk Problem Summary Draw a call stack for the

use python please and i need at least 3 different "test cases" or movements of the disk
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Problem Summary Draw a call stack for the Tower of Hanoi problem. Assume that you start with a stack of three disks. trivial you mignt even say. I nis sounas ke a base case in the making. Here is a high-level outline of how to move a tower from the starting pole, to the goal pole, using an intermediate pole: 1. Move a tower of height-1 to an intermediate pole, using the final pole. 2. Move the remaining disk to the final pole. 3. Move the tower of height-1 from the intermediate pole to the final pole using the original pole. As long as we always obey the rule that the larger disks remain on the bottom of the stack, we can use the three steps above recursively, treating any larger disks as though they were not even there. The only thing missing from the outline above is the identification of a base case. The simplest Tower of Hanoi problem is a tower of one disk. In this case, we need move only single disk to its final destination. A tower of one disk will be our base case. In addition, the steps outlined above move us toward the base case by reducing the height of the tower in steps 1 and 3. Listing 1 shows the Python code to solve the Tower of Hanoi puzzle. Listing 1 def moveTower (height, fromPole, toPole, withP ole): if height s= 1 : moveTower(height-1, fromPole, withPole , toPole) moveDisk (fromPole, toPole) moveTower (height-1, withPole, toPole, f rompole) Notice that the code in Listing 1 is almost identical to the English description. The key to the simplicity of the algorithm is that we make two different recursive calls, one on line 3 and a second on The Tower of Hanoi puzzle was invented by the French mathematician Edouard Lucas in 1883. He was inspired by a legend that tells of a Hindu temple where the puzzle was presented to young priests. At the beginning of time, the priests were given three poles and a stack of 64 gold disks, each disk a little smaller than the one beneath it. Their assignment was to transfer all 64 disks from one of the three poles to another, with two important constraints. They could only move one disk at a time, and they could never place a larger disk on top of a smaller one. The priests worked very efficiently, day and night, moving one disk every second. When they finished their work, the legend said, the temple would crumble into dust and the world would vanish. Although the legend is interesting, you need not worry about the world ending any time soon. The number of moves required to correctly move a tower of 64 disks is 2641=18,446,744,073,709,551,615. At a rate of one move per second, that is 584,942,417,355 years! Clearly there is more to this puzzle than meets the eye. Figure 1 shows an example of a configuration of disks in the middle of a move from the first peg to the third. Notice that, as the rules specify, the disks on each peg are stacked so that smaller disks are always on top of the larger disks. If you have not tried to solve this puzzle before, you should try it now. You do not need fancy disks and poles-a pile of books or pieces of paper will work. trivial you might even say. Ihis sounds like a base case in the making. Here is a high-level outline of how to move a tower from the starting pole, to the goal pole, using an intermediate pole: 1. Move a tower of height-1 to an intermediate pole, using the final pole. 2. Move the remaining disk to the final pole. 3. Move the tower of height-1 from the intermediate pole to the final pole using the original pole. As long as we always obey the rule that the larger disks remain on the bottom of the stack, we can use the three steps above recursively, treating any larger disks as though they were not even there. The only thing missing from the outline above is the identification of a base case. The simplest Tower of Hanoi problem is a tower of one disk. In this case, we need move only a single disk to its final destination. A tower of one disk will be our base case. In addition, the steps outlined above move us toward the base case by reducing the height of the tower in steps 1 and 3. Listing 1 shows the Python code to solve the Tower of Hanoi puzzle. Listing 1 def moveTower (height, fromPole, toPole, withP ole): if height >=1 : moveTower (height-1, fromPole, withPole , toPole) moveDisk (fromPole, toPole) moveTower(height-1, withPole, toPole, f rompole) Notice that the code in Listing 1 is almost identical to the English description. The key to the simplicity of the algorithm is that we make two different recursive calls, one on line 3 and a second on line 5. On line 3 we move all but the bottom disk on the initial tower to an intermediate pole. The next line simply moves the

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions

Question

help me to solve by finite difference

Answered: 1 week ago