Question

Write a Python program that implements the Towers of Hanoi, using the recursive algorithm discussed in...

Write a Python program that implements the Towers of Hanoi, using the recursive algorithm discussed in class.

Use command-line arguments to pass parameters to the program -

spring% python towersOfHanoi.py
USAGE: towersOfHanoi.py <# rings> <FROM peg> <TO peg>
spring% python towersOfHanoi.py  4 1 3
Move disk from peg 1 to peg 2
Move disk from peg 1 to peg 3
Move disk from peg 2 to peg 3
Move disk from peg 1 to peg 2
Move disk from peg 3 to peg 1
Move disk from peg 3 to peg 2
Move disk from peg 1 to peg 2
Move disk from peg 1 to peg 3
Move disk from peg 2 to peg 3
Move disk from peg 2 to peg 1
Move disk from peg 3 to peg 1
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code is as follows:

import sys #import sys for command line arguments

def TowerOfHanoi(n , from_peg, to_peg, aux_peg): #TowerOfHanoi function with n as no of disks from_peg(source),to_peg(destination),aux_peg(auxiliary)
if n == 1:
print("Move disk from peg",from_peg,"to peg",to_peg)
return
TowerOfHanoi(n-1, from_peg, aux_peg, to_peg) #recusrsive call to TowerOfHanoi
print("Move disk","from peg",from_peg,"to peg",to_peg)
TowerOfHanoi(n-1, aux_peg, to_peg, from_peg) #recusrsive call to TowerOfHanoi

pegs = [1,2,3] #pegs are 1,2 and 3
n = int(sys.argv[1]) #get n from command line argv 1
from_peg = int(sys.argv[2]) #get source from argv 2
to_peg = int(sys.argv[3]) #get destination from argv 3

pegs.remove(from_peg) #remove from_peg from pegs
pegs.remove(to_peg) #remove to_peg from pegs
aux_peg = pegs[0] #then take the remaiing peg as auxiliary peg
  

TowerOfHanoi(n,str(from_peg),str(to_peg),str(aux_peg)) #function call

************************************************************************************************************************************

Screesnhot of the code:

Output:

Add a comment
Know the answer?
Add Answer to:
Write a Python program that implements the Towers of Hanoi, using the recursive algorithm discussed in...
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
  • Describe a recursive algorithm for solving the Towers of Hanoi puzzle for an arbitrary n (see...

    Describe a recursive algorithm for solving the Towers of Hanoi puzzle for an arbitrary n (see Creativity Exercise C-5.16 for more details). C-5.16 In the Towers of Hanoi puzzle, we are given a platform with three pegs, a, b, and c, sticking out of it. On peg a is a stack of n disks, each larger than the next, so that the smallest is on the top and the largest is on the bottom. The puzzle is to move all...

  • Write a recursive C++ program to solve the famous Towers of Hanoi problem. In addition to showing the moves, also display the current status of three pegs as follows.

    Write a recursive C++ program to solve the famous Towers ofHanoi problem. In addition to showing the moves, alsodisplay the current status of three pegs as follows.Initial pegsA54321BCMove disk 1 from peg A to peg BA5432B1C…

  • 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,...

  • 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...

  • 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...

  • Assignment 2 In this assignment, you will write two short programs to solve problems using recursion....

    Assignment 2 In this assignment, you will write two short programs to solve problems using recursion. 1. Initial Setup Log in to Unix. Run the setup script for Assignment 2 by typing: setup 2 2. Towers of Hanoi Legend has it that in a temple in the Far East, priests are attempting to move a stack of disks from one peg to another. The initial stack had 64 disks threaded onto one peg and arranged from bottom to top by...

  • 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.

  • 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...

  • 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...

  • 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