I'm trying to find the majority or leader, in a stack that isn't sorted, and i'm having trouble with my tos (top of stack variable). Below is my code with main included. The majority of an array or stack is any element which appears in the array more than half the time (arrSize/2).
public class findLeader {
static class ArrayStack{
private int[] stackArr;
private int tos;//top of stack
public ArrayStack(){
stackArr = new int[10];
tos = -1;
}
public ArrayStack(int size){
stackArr = new int[size];
tos = -1;
}
public ArrayStack(int[] arr, int tos){
stackArr = arr;
this.tos = tos;
}
public boolean isEmpty(){
return(tos == -1);
}
public int peek(){
if(isEmpty()){
return -999;
}
return stackArr[tos];
}
public void push(int x){
if(tos == stackArr.length - 1){
return;
}
stackArr[++tos] = x;
}
public int pop(){
if(isEmpty()){
return -999;
}
int popValue = stackArr[tos];
stackArr[tos] = 0;
--tos;
return popValue;
}
public void print(){
if(isEmpty()){
return;
}
for(int i = 0; i <= tos; ++i){
System.out.print(stackArr[i] + " ");
}
System.out.println();
}
}
public static int leader(ArrayStack myStack){
int initSize = myStack.tos + 1; //gets initial size of stack.
int leader; //initialize leader or majority.
while(!myStack.isEmpty()){
leader = myStack.peek();//set first leader variable to the element at the tos.
System.out.println("leader " + leader); //just for debugging
System.out.println("tos " + myStack.tos); //debugging
//System.out.println(isLeader(myStack, initSize, leader)); //debugging
if(isLeader(myStack, initSize, leader)){
return 1;
}
else{
myStack.pop();
}
System.out.println("after function tos " + myStack.tos); //debugging
}
return -1;
}
public static boolean isLeader(ArrayStack myStack, int initSize, int leader){
ArrayStack as = myStack;
int count = 0;
while(!as.isEmpty()){
if(as.peek() == leader){
as.pop();
++count;
}
else{
as.pop();
}
}
//System.out.println(count);
if(count > initSize / 2)
return true;
else{
return false;
}
}
public static void main(String[] args) {
int[] arr = {2, 5, 6, 2, 8, 2, 8, 2, 2};
ArrayStack stack = new ArrayStack();
stack.push(5);
stack.push(2);
stack.push(6);
stack.push(2);
stack.push(8);
stack.push(2);
stack.push(2);
stack.push(2);
stack.push(5);
System.out.println(leader(stack));
}
Where the problem arises is in the leader, and isLeader methods, after the initial call of isLeader, tos gets returned as tos = -1 as is demonstrated from my output below.
Output:
leader 5
tos 8
after function tos -1
-1
My intent is after every call to isLeader() if false is returned, i want to pop the tos variable from the top and call isLeader() once more with the new smaller stack.
Any help would really be appreciated, and I hope everyone is well!
Thank you
I'd personally add a method public Integer leader() to your ArrayStack that returns the stack's leader or null if the stack has no leader:
public Integer leader() {
final Map<Integer, Integer> counts = new HashMap<>();
final int neededLeaderCount = ((tos + 1) / 2) + 1;
for (int i = 0; i <= tos; ++i) {
int currentElementCount = counts.getOrDefault(stackArr[i], 0) + 1;
if (currentElementCount >= neededLeaderCount) {
return stackArr[i];
}
counts.put(stackArr[i], currentElementCount);
}
return null;
}
Your isLeader function could then be rewritten to:
public static boolean isLeader(ArrayStack myStack, int leader) {
// use Objects.equals as myStack.leader() may be null
return Objects.equals(myStack.leader(), leader);
}
This approach is much cleaner as it does not modify or require any internals of your ArrayStack outside of the class itself.
I was able to get it to work by also tracking a currSize of the stack to pass to the isLeader function. I feel like i am complicating things, if anyone has a simpler solution and would like to share it, I would be open to it. Below is my code if anyone is interested.
public static int leader(ArrayStack myStack){
int currSize = myStack.tos;
int initSize = myStack.tos + 1; //gets initial size of stack.
int leader; //initialize leader or majority.
while(!myStack.isEmpty()){
leader = myStack.peek();//set first leader variable to the element at the tos.
System.out.println("leader " + leader); //just for debugging
System.out.println("tos " + myStack.tos); //debugging
//System.out.println(isLeader(myStack, initSize, leader)); //debugging
if(isLeader(myStack, initSize, leader, currSize)){
return 1;
}
else{
--currSize;
myStack.pop();
}
System.out.println("after function tos " + myStack.tos); //debugging
}
return -1;
}
public static boolean isLeader(ArrayStack myStack, int initSize, int leader, int currSize){
ArrayStack as = myStack;
int count = 0;
while(as.tos > -1){
if(as.peek() == leader){
--as.tos; //Just move tos pointer down stack rather than popping
//to maintain original stack.
++count;
}
else{
--as.tos;
}
}
if(count > initSize / 2)
return true;
else{
as.tos = currSize;//reset the tos pointer back to the currSize of
//stack.
return false;
}
}
Related
I have to create an array that will hold a user input that is 3 letter code followed by a ticket number. ex) Ama-34. How do I do this?
I know long is not correct, I just am modeling it off of another project.
I also have to allow for user input and manipulation which I am having a hard time.
This is what I have so far...
class QueueOrder{
//Global Variables
static Scanner orderScan = new Scanner(System.in);
//Variables
public int MaxSize;
//How to make an array hold both names and numbers??
public long[] BodaciousArray;
public int Front; //Track the front pointer
public int Rear; //track the last pointer
public int NumberOfOrders; //track the number of orders in the system
//Constructor
public QueueOrder(int size){
MaxSize = size;
BodaciousArray = new long[MaxSize];
Front = 0;
Rear = -1;
NumberOfOrders = 0;
}
//Enqueue - add to the rear of the queue
//Allow the server to add one to the array
public void Enqueue(){
long j = 0;
//Add a wrap around
if(Rear == MaxSize - 1){
Rear = -1;
}
//Increment the rear and insert a new item
BodaciousArray[++Rear] = j;
NumberOfOrders++;
}
//Dequeue - remove one from the array
//Allow the server to remove what is next in line
public long Dequeue(){
//Get the first value and incrament the front
long temp = BodaciousArray[Front++];
//Add a wrap around
if(Front == MaxSize){
Front = 0;
}
//Remove one item
NumberOfOrders--;
return temp;
}
//Peek at the front of the queue
//Allow the server to see what order is next
public long peekFront(){
return BodaciousArray[Front];
}
//Check to is the queue is empty
public boolean isEmpty(){
return(NumberOfOrders == 0);
}
//Check to see if the queue is full
public boolean isFull(){
return(NumberOfOrders == MaxSize);
}
//Check how many items are in the queue
public int size(){
return NumberOfOrders;
}
public void DisplayQueueOrder(){
int i;
if(Front == Rear){
System.out.println("There are no orders to fill");
}else{
for(i = Front; i < Rear; i++){
System.out.print("The current orders are: "
+ BodaciousArray[i] + ", ");
}
}
}
If you want to keep the two seperate, and the 3 letter code is unique HashMap is the way to go:
HashMap<String, Integer> queueArray = new HashMap<String, Integer>();
queueArray.put("Ama", 34);
System.out.println(arr.get("Ama"));
outputs:
34
Otherwise, why not just do something like this:
String[] tickets = {"Ama-34", "Abc-60", "Xyz-76"};
public String getTicketCode(int index) {
return tickets[index].split("-")[0];
}
public int getTicketNumber(int index) {
return Integer.parseInt(tickets[index].split("-")[1]);
}
used like:
System.out.println(getTicketCode(0));
System.out.println(getTicketNumber(0));
prints:
Ama
76
I was asked to program a method that receives a scanner, and returns a sorted array of words which contain only letters, with no repetitions (and no bigger in length than 3000). Then, I was asked to program a method that checks whether a certain given string is contained in a given vocabulary. I used a simple binary search method.
This is what I've done:
public static String[] scanVocabulary(Scanner scanner){
String[] array= new String[3000];
int i=0;
String word;
while (scanner.hasNext() && i<3000) {
word=scanner.next();
if (word.matches("[a-zA-Z]+")){
array[i]=word.toLowerCase();
i++;
}
}int size=0;
while (size<3000 && array[size]!=null ) {
size++;
}
String[] words=Arrays.copyOf(array, size);
if (words.length==0 || words.length==1) {
return words;
}
else {
Arrays.sort(words);
int end= removeDuplicatesSortedArr(words);
return Arrays.copyOf(words, end);
}
}
private static int removeDuplicatesSortedArr(String[] array) { //must be a sorted array. returns size of the new array
int n= array.length;
int j=0;
for (int i=0; i<n-1; i++) {
if (!array[i].equals(array[i+1])) {
array[j++]=array[i];
}
}
array[j++]=array[n-1];
return j;
}
public static boolean isInVocabulary(String[] vocabulary, String word){
//binary search
int n=vocabulary.length;
int left= 0;
int right=n-1;
while (left<=right) {
int mid=(left+right)/2;
if (vocabulary[mid].equals(word)){
return true;
}
else if (vocabulary[mid].compareTo(word)>0) {
right=mid-1;
}else {
right=mid+1;
}
}
return false;
}
while trying the following code:
public static void main(String[] args) {
String vocabularyText = "I look at the floor and I see it needs sweeping while my guitar gently weeps";
Scanner vocabularyScanner = new Scanner(vocabularyText);
String[] vocabulary = scanVocabulary(vocabularyScanner);
System.out.println(Arrays.toString(vocabulary));
boolean t=isInVocabulary(vocabulary, "while");
System.out.println(t);
System.out.println("123");
}
I get nothing but-
[and, at, floor, gently, guitar, i, it, look, my, needs, see, sweeping, the, weeps, while]
nothing else is printed out nor returned. Both functions seem to be working fine separately, so I don't get what I'm doing wrong.
I would be very happy to hear your thoughts, thanks in advance :)
This has nothing to do with the console. Your isInVocabulary method is entering an infinite loop in this block:
if (!isInVocabulary(vocabulary, "while")) {
System.out.println("Error");
}
If you were to debug through isInVocabulary, you would see that after a few iterations of the while loop,
left = 0;
right = 2;
mid = 1;
if (vocabulary[mid].equals(word)){
// it doesn't
} else if (vocabulary[mid].compareTo("while") > 0) {
// it doesn't
} else {
right = mid + 1;
// this is the same as saying right = 1 + 1, i.e. 2
}
So you'll loop forever.
I know similar questions have been asked before but I have found the answers confusing. I am trying to make a program that will find every combination of an array-list with no repetitions and only of the maximum size. If the list has 4 items it should print out only the combinations with all 4 items present. This is what I have so far:
public main(){
UI.initialise();
UI.addButton("Test", this::testCreate);
UI.addButton("Quit", UI::quit);
}
public void createCombinations(ArrayList<String> list, String s, int depth) {
if (depth == 0) {
return;
}
depth --;
for (int i = 0; i < list.size(); i++) {
if (this.constrain(s + "_" + list.get(i), list.size())) {
UI.println(s + "_" + list.get(i));
}
createCombinations(list, s + "_" + list.get(i), depth);
}
}
public void testCreate() {
ArrayList<String> n = new ArrayList<String>();
n.add("A"); n.add("B"); n.add("C"); n.add("D");
this.createCombinations(n , "", n.size());
}
public boolean constrain(String s, int size) {
// Constrain to only the maximum length
if ((s.length() != size*2)) {
return false;
}
// Constrain to only combinations without repeats
Scanner scan = new Scanner(s).useDelimiter("_");
ArrayList<String> usedTokens = new ArrayList<String>();
String token;
while (scan.hasNext()) {
token = scan.next();
if (usedTokens.contains(token)) {
return false;
} else {
usedTokens.add(token);
}
}
// If we fully iterate over the loop then there are no repitions
return true;
}
public static void main(String[] args){
main obj = new main();
}
This prints out the following which is correct:
_A_B_C_D
_A_B_D_C
_A_C_B_D
_A_C_D_B
_A_D_B_C
_A_D_C_B
_B_A_C_D
_B_A_D_C
_B_C_A_D
_B_C_D_A
_B_D_A_C
_B_D_C_A
_C_A_B_D
_C_A_D_B
_C_B_A_D
_C_B_D_A
_C_D_A_B
_C_D_B_A
_D_A_B_C
_D_A_C_B
_D_B_A_C
_D_B_C_A
_D_C_A_B
_D_C_B_A
This works for small lists but is very inefficient for larger ones. I am aware that what I have done is completely wrong but I want to learn the correct way. Any help is really appreciated. Thanks in advance.
P.S. This is not homework, just for interest although I am a new CS student (if it wasn't obvious).
Implementing Heap's algorithm in Java:
import java.util.Arrays;
public class Main {
public static void swap(final Object[] array, final int index1, final int index2) {
final Object tmp = array[index1];
array[index1] = array[index2];
array[index2] = tmp;
}
public static void printPermutations_HeapsAlgorithm(final int n, final Object[] array) {
final int[] c = new int[n];
for (int i = 0; i < c.length; ++i)
c[i] = 0;
System.out.println(Arrays.toString(array)); //Consume first permutation.
int i=0;
while (i < n) {
if (c[i] < i) {
if ((i & 1) == 0)
swap(array, 0, i);
else
swap(array, c[i], i);
System.out.println(Arrays.toString(array)); //Consume permutation.
++c[i];
i=0;
}
else
c[i++] = 0;
}
}
public static void main(final String[] args) {
printPermutations_HeapsAlgorithm(4, new Character[]{'A', 'B', 'C', 'D'});
}
}
Possible duplicate of this.
I have been trying to troubleshoot this java program from hours now and has not been able to find what is wrong with the execution. I think that the main class is not defined correctly.
It compiles successfully but the output is blank which should not be the case right? I intially tried to call the main class using the objects but still no luck. Any suggestions will work.
The original program:It compiles successfully on adding the main method but the output is blank.
import java.lang.Math; // headers MUST be above the first class
// one class needs to have a main() method
public class OrdSetSimple
{
// arguments are passed using the text field below this editor
public static void main(String[] args){
OrdSetSimple obj = new OrdSetSimple(10);
System.out.print("Success");
}
private int _set_size;
private int _set[];
private int _last;
public OrdSetSimple(int size) {
int k;
_set_size=size;
_set = new int[_set_size];
for(k=0; k<_set_size; k++)
_set[k]=0;
_last=-1;
}
private int make_a_free_slot(int val) {
int pos, i, j;
pos = binSearch(val);
if (pos >= 0)
return -1;
for (i=0; i<=_last; i++) {
if (_set[i] > val)
break;
}
if ((i<=_last)||(_last==-1)) {
for (j=_last; j>=i; j--)
_set[j+1] = _set[j];
pos = i;
} else {
pos = _last+1;
}
_last++;
return pos;
}
public void addElement(int n) {
int pos;
if (n<0) {
System.out.println("Addition of a negative integer impossible\n");
return;
}
if (_last+1>=_set_size) {
System.out.print("Addition of " + n);
System.out.println(" impossible since the array is already full");
System.out.println("The array is: " + toString());
} else {
pos = make_a_free_slot(n);
if (pos>=0)
_set[pos]=n;
}
return;
}
public int getSize() {
return _last+1;
}
public int getElementAt(int i) {
if ((i<0)||(i>_last))
return -1;
else
return _set[i];
}
private int binSearch(int x) {
int i=0;
int j=_last-1;
int m=0;
if (_last==-1)
return -1;
while(i<j) {
m= (i+j)/2;
if (x>_set[m])
i=m+1;
else
j=m;
}
if (x == _set[i]) return i;
else return -1;
}
public OrdSetSimple difference(OrdSetSimple s2) {
OrdSetSimple s1 = this;
int size1=s1.getSize();
int size2=s2.getSize();
OrdSetSimple set=new OrdSetSimple(size2);
int k;
for(k=0; k<size1; k++)
if (s2.binSearch(s1.getElementAt(k)) < 0)
set.addElement(s1.getElementAt(k));
return set;
}
public String toString() {
int k = 0;
String s="";
for (k=0; k<=_last; k++)
s += _set[k] + " ";
return s;
}
}
Your very first statement is wrong.
OrdSetSimple obj = new OrdSetSimple();//This will call the default constructor which will not initialize anything. This constructor will be added to your program by compiler, hence you don't get any compilation error.
Correct it like
OrdSetSimple obj = new OrdSetSimple(100);
I am trying to round an array of numbers to three decimal places in java, the reason being that I am running into an OutOfMemoryError (array is exceeding the VM's limit). I was curious if there was a way to do this without writing an entire new method or anything drastic like that.
EDIT: here is all the code
public class GuitarHero {
public static void main(String[] args) {
int index = 0;
double sample = 0.0;
String keyboard ="1234567890qwertyuiopasdfghjklzxcvbnm,";
GuitarString[] string = new GuitarString[keyboard.length()];
for(int i = 0; i < 37; i++) {
double concert = 110.0 * Math.pow(2,i-24);
string[i] = new GuitarString(concert);
}
while (true){
if (StdDraw.hasNextKeyTyped()) {
char key = StdDraw.nextKeyTyped();
index = keyboard.indexOf(key);
if (index >= 0 && index < 37){
string[index].pluck();
}
//sample = string[index].sample() + string[index+1].sample();
//StdAudio.play(sample);
}
for(int i=0; i<37; i++){
sample = string[i].sample();
StdAudio.play(sample);
}
for(int i = 0; i < 37; i++){
string[i].tic();
}
}
}
}
end of code 1
public class GuitarString {
private RingBuffer buffer; // ring buffer
// YOUR OTHER INSTANCE VARIABLES HERE
private int ticTimes = 0;
// create a guitar string of the given frequency
public GuitarString(double frequency) {
// YOUR CODE HERE
int N;
N = (int)(44100/frequency);
buffer = new RingBuffer(N);
for (int i=1; i <=N; i++ ){
buffer.enqueue(0.0);
}
}
// create a guitar string whose size and initial values are given by the array
public GuitarString(double[] init) {
// YOUR CODE HERE
buffer = new RingBuffer(init.length);
for (int i = 0; i < init.length; i++){
buffer.enqueue(init[i]);
}
}
// pluck the guitar string by setting the buffer to white noise
public void pluck() {
// YOUR CODE HERE
while(!buffer.isEmpty()) buffer.dequeue();
while(!buffer.isFull()){
buffer.enqueue(Math.random()-0.5);
}
}
// advance the simulation one time step
public void tic() {
// YOUR CODE HERE
double value1, value2;
value1 = buffer.dequeue();
value2 = buffer.peek();
buffer.enqueue(((value1+value2)/2)*0.996);
ticTimes++;
}
// return the current sample
public double sample() {
// YOUR CODE HERE
return buffer.peek();
}
// return number of times tic was called
public int time() {
// YOUR CODE HERE
return ticTimes;
}
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
double[] samples = { .2, .4, .5, .3, -.2, .4, .3, .0, -.1, -.3 };
GuitarString testString = new GuitarString(samples);
for (int i = 0; i < N; i++) {
int t = testString.time();
double sample = testString.sample();
System.out.printf("%6d %8.4f\n", t, sample);
testString.tic();
}
}
}
end of code 2
public class RingBuffer {
private int first; // index of first item in buffer
private int last; // index of last item in buffer
private int size; // current number of items of buffer
private double[] buffer;
// create an empty buffer, with given max capacity
public RingBuffer(int capacity) {
// YOUR CODE HERE
buffer = new double[capacity];
first =0;
last =capacity-1;
size =0;
}
// return number of items currently in the buffer
public int size() {
// YOUR CODE HERE
return size;
}
// is the buffer empty (size equals zero)?
public boolean isEmpty() {
// YOUR CODE HERE
if (size == 0)
return true;
else
return false;
}
// is the buffer full (size equals array capacity)?
public boolean isFull() {
// YOUR CODE HERE
if (size == buffer.length)
return true;
else
return false;
}
// add item x to the end
public void enqueue(double x) {
if (isFull()) { throw new RuntimeException("Ring buffer overflow"); }
// YOUR CODE HERE
last = (last+1)%buffer.length;
buffer[last]=x;
size++;
}
// delete and return item from the front
public double dequeue() {
if (isEmpty()) { throw new RuntimeException("Ring buffer underflow"); }
// YOUR CODE HERE
double temp = buffer[first];
first = (first+1)% buffer.length;
size--;
return temp;
}
// return (but do not delete) item from the front
public double peek() {
if (isEmpty()) { throw new RuntimeException("Ring buffer underflow"); }
// YOUR CODE HERE
return buffer[first];
}
// a simple test of the constructor and methods in RingBuffer
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
RingBuffer buffer = new RingBuffer(N);
for (int i = 1; i <= N; i++) {
buffer.enqueue(i);
}
double t = buffer.dequeue();
buffer.enqueue(t);
System.out.println("Size after wrap-around is " + buffer.size);
while (buffer.size() >= 2) {
double x = buffer.dequeue();
double y = buffer.dequeue();
buffer.enqueue(x + y);
}
System.out.println(buffer.peek());
}
}
Thanks!
Well in the first iteration of that loop the code is trying to allocate an array of 1,681,534,603 doubles (44100 / (110 * 2^-22)), which would require about 3GB of memory. I suggest you find a different solution.