If possible, this tower of hanoi code is written in java. Could
anyone make an attempt to write it in python?
package Hanoi;
import java.util.Scanner;
private int n,current,r;
public class Towers {
private Scanner in;
private int[] hasItMoved,toDest;
Towers(){
this.in=new Scanner(System.in);
this.current=1;
System.out.println("Enter the number of disks:
");
this.n = in.nextInt();
this.hasItMoved=new int[12];
this.toDest=new int[12];
for(int j=0;j<this.toDest.length;j++)
this.toDest[j]=1;
for(int i=1;i<=n;i++)
this.toDest[i]=0;
r=n;
hanoiStart(n,"Start","Aux1","Aux3","Aux2","Dest",this.current);}
public void hanoiStart(int numOfDisks, String Start
,String source, String dest, String aux, String last, int
current){
move(1, Start, source, this.current);
this.current++;
H1(n,"Start","Aux1","Aux3","Aux2","Dest",this.current);
move(1, dest, last, this.current);
this.current++;
}
public int H1(int numOfDisks, String Start ,String
source, String dest, String aux, String last, int current) {
if (numOfDisks == 1) {
move(numOfDisks, source, aux, this.current);
this.current++;
move(numOfDisks, aux, dest, this.current);
this.current++;
} else if (numOfDisks == 2) {
move(numOfDisks-1, source, aux, this.current);
this.current++;
move(numOfDisks-1, aux, dest, this.current);
this.current++;
if(this.current==4)
{ move(numOfDisks, Start, source, this.current);
this.current++;}
move(numOfDisks, source, aux, this.current);
this.current++;
move(numOfDisks-1, dest, aux, this.current);
this.current++;
move(numOfDisks-1, aux, source, this.current);
this.current++;
move(numOfDisks, aux, dest, this.current);
this.current++;
if(this.r==2)
{move(2, dest, last, this.current);
this.current++;}
move(numOfDisks-1, source, aux, this.current);
this.current++;
move(numOfDisks-1, aux, dest, this.current);
this.current++;
} else if (numOfDisks > 2) {
this.current = H1(numOfDisks-1, Start, source, dest,
aux,last, this.current);
if(this.hasItMoved[numOfDisks]!=1)
{move(numOfDisks, Start, source, this.current);
this.current++;this.hasItMoved[numOfDisks]=1;}
move(numOfDisks, source, aux, this.current);
this.current++;
this.current = H1(numOfDisks-1,Start, dest, source,
aux,last, this.current);
move(numOfDisks, aux, dest, this.current);
this.current++;
if(this.toDest[numOfDisks+1]!=0)
{move(numOfDisks, dest, last, this.current);
this.current++;this.toDest[numOfDisks]=1;}
if(numOfDisks==r)
this.r--;
this.current = H1(numOfDisks-1, Start,source, dest,
aux,last, this.current);
};
return this.current;
}
public void move(int aDisk, String source, String
dest, int currentStep) {
System.out.println("Move " + this.current + ": Move
disk " + aDisk + " from " + source + " to " + dest);}
Hanoi.java
package Hanoi;
public class Hanoi {
public static void main(String[] args) {
Towers test=new Towers();}}
Code:
File: hanoi.py
Code as text:
# A class to solve tower of hanoi problem
class Towers:
hasItMoved = []
toDest = []
# constructor or initialiser
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", "Aux2", "Dest", self.current)
self.move(1, dest, last, self.current)
self.current += 1
# helper function
def H1(self, numOfDisks, start, source, dest, aux, last, current):
if numOfDisks == 1:
self.move(numOfDisks, source, aux, self.current)
self.current += 1
self.move(numOfDisks, aux, dest, self.current)
self.current += 1
elif numOfDisks == 2:
self.move(numOfDisks-1, source, aux, self.current)
self.current += 1
self.move(numOfDisks-1, aux, dest, self.current)
self.current += 1
if self.current==4:
self.move(numOfDisks, start, source, self.current)
self.current += 1
self.move(numOfDisks, source, aux, self.current)
self.current += 1
self.move(numOfDisks-1, dest, aux, self.current)
self.current += 1
self.move(numOfDisks-1, aux, source, self.current)
self.current += 1
self.move(numOfDisks, aux, dest, self.current)
self.current += 1
#continued
if self.r==2:
self.move(2, dest, last, self.current)
self.current += 1
self.move(numOfDisks-1, source, aux, self.current)
self.current += 1
self.move(numOfDisks-1, aux, dest, self.current)
self.current += 1
elif numOfDisks > 2:
self.current = self.H1(numOfDisks-1, start, source, dest, aux,last, self.current)
if self.hasItMoved[numOfDisks]!=1:
self.move(numOfDisks, start, source, self.current)
self.current += 1
self.hasItMoved[numOfDisks]=1
self.move(numOfDisks, source, aux, self.current)
self.current += 1
self.current = self.H1(numOfDisks-1, start, dest, source, aux,last, self.current)
self.move(numOfDisks, aux, dest, self.current)
self.current += 1
if self.toDest[numOfDisks+1]!=0:
self.move(numOfDisks, dest, last, self.current)
self.current += 1
self.toDest[numOfDisks]=1
if numOfDisks==self.r:
self.r -= 1
self.current = self.H1(numOfDisks-1, start,source, dest, aux,last, self.current)
return self.current;
# function to print the move performed
def move(self, aDisk, source, dest, currentStep):
print("Move ", self.current, ": Move disk ", aDisk, " from ", source, " to ", dest)
File: main.py
Code as text:
import hanoi
if __name__=="__main__":
test = hanoi.Towers()
Sample run:
P.s. Ask any doubts in comments and don't forget to rate the answer.
If possible, this tower of hanoi code is written in java. Could anyone make an attempt...
JAVA: How do I output all the data included for each employee? I can only get it to output the name, monthly salary and annual salary, but only from the Employee.java file, not Salesman.java or Executive.java. Employee.java package project1; public class Employee { private String name; private int monthlySalary; public Employee(String name, int monthlySalary) { this.name = name; this.monthlySalary = monthlySalary; } public int getAnnualSalary() { int totalPay = 0; totalPay = 12 * monthlySalary; return totalPay; } public String...
JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe { private final int BOARDSIZE = 3; // size of the board private enum Status { WIN, DRAW, CONTINUE }; // game states private char[][] board; // board representation private boolean firstPlayer; // whether it's player 1's move private boolean gameOver; // whether...
please make a pretty JAVA GUI for this code this is RED BLACK TREE and i Finished code already jus need a JAVA GUI for this code ... if poosible make it pretty to look thanks and please make own GUI code base on my code thanks ex: (GUI only have to show RBTree) ---------------------------------------- RBTree.java import java.util.Stack; public class RBTree{ private Node current; private Node parent; private Node grandparent; private Node header; private Node...
USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list and use a Merge Sort to sort the object by age. Create a class called Person : name and age Create methods that add, and delete Person from the link list Create a method that sorts the persons' objects by age. package mergesort; public class MergeSortExample { private static Comparable[] aux; // auxiliary array for merges public static void sort(Comparable[] a) { aux =...
Below, you can find the description of your labwork for today. You can also find the expected output of this code in the Application Walkthrough section. You are going to improve your existing Money & Stock Trading Platform on previous week’s labwork by incorporating Collections. In previous labworks, you have used arrays for holding Customer and Item objects. For this labwork you need to use ArrayList for holding these objects. So, rather than defining Customer[] array, you need to define...
This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...
CONVERT THIS JAVA CODE WITH THE JAVA DOC GUIDELINES WHERE APPROPRIATE. DO NOT CHANGE THE CODE. SAME CODE SHOULD BE USED FOR THE DOCUMENTATION PURPOSES. ONLY INSERT THE JAVA DOC. //Vehicle.java public class Vehicle { private String manufacturer; private int seat; private String drivetrain; private float enginesize; private float weight; //create getters for the attributes public String getmanufacturer() { return manufacturer; } public String getdrivetrain() { return drivetrain;...
*JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> { private int top, size; private E arrS[]; private static final int MAX_STACK_SIZE = 10; public ArrayStack() { this.arrS = (E[]) new Object[MAX_STACK_SIZE]; this.top = size; this.size = 0;...
Can Anyone help me to convert Below code to C++! Thanks For example, in C++, the function headers would be the following: class MaxHeap { vector<int> data; public: MaxHeap() { // ... } int size() { // ... } int maxLookup() { // ... } void extractMax() { // ... } void insert(int data) { // ... } void remove(int index) { // ... } }; ======================== import java.util.Arrays; import java.util.Scanner; public class MaxHeap { Integer[] a; int size; //...
Java Im doing a binary search on an array and need to allow as many queries as possible and cannot figure out how to. The output should read something like this. Enter a number. 3 3 is a prime number. Enter another number. 4 4 is not a prime number. Enter another number. 8 Current file not large enough for 8. Enter another number. -1 Bye. My code so far looks like this. public static void main(String[] args) {...