Question

## We will be simulating the ideal solution to Hanoi's tower problem. ## The discs will...

## We will be simulating the ideal solution to Hanoi's tower problem.
## The discs will be numers from 0 to n-1, with the larger number being the smaller disc.
## each rod with discs will be encoded by a list, from bottom to top. For example, a rod with discs 0, 3, 5 will be encoded as [0,3,5]
## current configuration of rods will be encoded as a list of 3 lists. For example, starting state with 3 discs will be [[0,1,2],[],[]]
## You need to fill out the missing parts from the function 'move' and the function 'TowerHanoi_recur'
## Once you are finished, run TowerHanoi_start(5).


def initialize_rods(n): ##this function initializes the rods and the discs. Rod 0 will have all discs 0~n-1.
global global_counter
global_counter = 0
L = []
for i in range(n):
L = L + [i]
return [L,[],[]]

def pop(L): ##Given a list L, gets the last entry.
i = len(L)
return L[i-1]

def after_pop(L): ##Given a list L, deletes the last entry.
i = len(L)
return L[0:i-1]

def insert(i,L): ##Given a list L, adds i to the end. We make sure disc i is the smaller than the current top-disc of the rod.
j = len(L)
if j >0 and i < L[j-1]:
print('Illegal move!')
return L + [i]

def move(C,rod_from,rod_to): ##Given configuration C, moves a top disc from rod_from to rod_to.
global global_counter
disc = ####FILL THIS PART : disc stores the disc we are moving.
C_after = C
global_counter = global_counter+1
print('(Moving disc',disc,'Rod',rod_from,'-> Rod',rod_to,'Step number :',global_counter,')')
C_after[rod_from] = ####FILL THIS PART#### : how the rod rod_from would look like after the move
C_after[rod_to] = ####FILL THIS PART#### : how the rod rod_to would look like after the move
return C_after

def report_status(L): ##prints out the current configuration.
print('Rod 0:',L[0],'Rod 1:',L[1],'Rod 2:',L[2])


def TowerHanoi_recur(C,n,rod_from,rod_to,rod_other): ##This is the main recurrence algorithm we are running. C is the current configuration of rods and discs, we are trying to move n top discs.
if n == 1:
report_status(C)
return move(C,rod_from,rod_to)
T = ####FILL THIS PART. The first half of our algorithm. Move top n-1 discs from rod_from to rod_other.####
report_status(T)
Q = ####FILL THIS PART. Move the largest disc from rod_from to rod_to.####
return ####FILL This PART. We return the result of the second half of our algorithm: move the n-1 discs from rod_other to rod_to.####


def TowerHanoi_start(n): ##This is the function we will be running. n is the number of discs.
Current_Configuration = initialize_rods(n)
report_status(TowerHanoi_recur(Current_Configuration,n,0,2,1))

Homework Answers

Know the answer?
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
Similar Questions
# Parts to be completed are marked with '<<<<< COMPLETE' import random N = 8 MAXSTEPS...
# Parts to be completed are marked with '<<<<< COMPLETE' import random N = 8 MAXSTEPS = 5000 # generates a random n-queens board # representation: a list of length n the value at index i is # row that contains the ith queen; # exampe for 4-queens: [0,2,0,3] means that the queen in column 0 is # sitting in row 0, the queen in colum 1 is in row, the queen in column 2 # is in row 0,...
I am a student taking python programming. Can this problem be modified using the define main...
I am a student taking python programming. Can this problem be modified using the define main method, def main()? import random #function definition #check for even and return 0 if even def isEven(number): if(number%2==0): return 0 #return 1 if odd else: return 1 #count variables even =0 odd = 0 c = 0 #loop iterates for 100 times for i in range(100): #generate random number n = random.randint(0,1000) #function call val = isEven(n) #check value in val and increment if(val==0):...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return the length of the longest chain of 1's that start from the beginning. You MUST use a while loop for this! We will check. >>> longest_chain([1, 1, 0]) 2 >>> longest_chain([0, 1, 1]) 0 >>> longest_chain([1, 0, 1]) 1 """ i = 0 a = [] while i < len(submatrix) and submatrix[i] != 0: a.append(submatrix[i]) i += 1 return sum(a) def largest_rectangle_at_position(matrix: List[List[int]], x:...
Python problem: write a main function that ask user for 2 file name, open files and...
Python problem: write a main function that ask user for 2 file name, open files and read the 1st line of each and compare them using hamming function(below is the hamming function I wrote). Just assume the fist line of each file only contains 0s and 1s, such as 0100111101. The main function may have to do some extra work to remove newline characters or other whitespace from the text read from each file. This is hamming function that need...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21,...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21, all players who are still standing win. But the players that are not standing have already lost. If the Dealer does not go over 21 but stands on say 19 points then all players having points greater than 19 win. All players having points less than 19 lose. All players having points equal to 19 tie. The program should stop asking to hit if...
Develop a Traceroute application in python using ICMP. Your application will use ICMP but, in order...
Develop a Traceroute application in python using ICMP. Your application will use ICMP but, in order to keep it simple, will not exactly follow the official specification in RFC 1739.. Below you will find the skeleton code for the client. You are to complete the skeleton code. The places where you need to fill in code are marked with #Fill in start and #Fill in end. Code from socket import * import os import sys import struct import time import...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None:...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None: """This method adds new value to the tree, maintaining BST property. Duplicates must be allowed and placed in the right subtree.""" Example #1: tree = BST() print(tree) tree.add(10) tree.add(15) tree.add(5) print(tree) tree.add(15) tree.add(15) print(tree) tree.add(5) print(tree) Output: TREE in order { } TREE in order { 5, 10, 15 } TREE in order { 5, 10, 15, 15, 15 } TREE in order {...
convert this code to accept int value instead of float values using python. Make sure to...
convert this code to accept int value instead of float values using python. Make sure to follow the same code. do not change the steps and make sure to point to what code you replaced. make sure to have 2 files Method:----------------------- #define a python user difined method def get_float_val (prompt): is_num = False str_val = input (prompt) #prming read for our while #while is_num == False: (ignore this but it works) old school while not is_num: try: value =...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will look like: Enter number of players: 2 Player 1: 7S 5D - 12 points Player 2: 4H JC - 14 points Dealer: 10D Player 1, do you want to hit? [y / n]: y Player 1: 7S 5D 8H - 20 points Player 1, do you want to hit? [y / n]: n Player 2, do you want to hit? [y / n]: y...
Problem 1. (1 point) The SolEc algorithm shown below is used with a = 0, b...
Problem 1. (1 point) The SolEc algorithm shown below is used with a = 0, b = 2 and n = 3. What is the value of c on the line Write c? Function z <- f (x) z = 85−99x + 15x2 − x3; End Function algorithm SolEc Read a, b, n; For i From 1 To n Do c = (a + b) / 2; Write a, ",", c, ",", b; If f (a) * f (c) <0...