I had to make a decrementing carousel [1]: https://i.stack.imgur.com/T7E4d.png. Although it kinda works, I need it to return right results so I can pass the tests. The code:
int position = 0;
int capacity = 3;
public int next() {
if (allNegative(arr)) return -1;
if (position >= capacity) position = 0;
if (arr[position] <= 0) {
if (position == arr.length - 1) {
position = 0;
if (arr[position] == 0) {
return arr[ position]--;
}
else {return arr[position ]--;}
}
else {
return arr[ position]--;
}
}
return arr[position ]--;
}
So it takes elements into the array and next() method should return the current value and decrement it, after that it moves to the next element. It should skip zero elements. When it has nothing to decrement, it returns -1. I've stuck here and I don't understand what doesn't work.
The entire code of a program:
public class DecrementingCarousel {
static int capacity;
int counter = 0;
//static List <Integer> elements = new ArrayList<>();
static int [] arr ;
public DecrementingCarousel(int capacity) {
DecrementingCarousel.capacity = capacity;
arr = new int[capacity];
}
public boolean addElement(int element){
if (counter < capacity && element > 0) {
arr[counter] = element;
counter ;
return true;
}
return false;
}
public CarouselRun run(){
return new CarouselRun();
}
}
public class CarouselRun {
int position = 0;
int number = 0;
/*
if (arr[position] > 0) return arr[position ]--;
if (arr[position] <= 0) {
arr = removeTheElement(arr,position);
capacity--;
if (position >= capacity) position = 0;
}
return arr[position ]--;
*/
public int next() {
if (allNegative(arr)) return -1;
if (position >= capacity) position = 0;
if (arr[position] <= 0) {
if (position == arr.length - 1) {
position = 0;
if (arr[position] == 0) {
return arr[ position]--;
}
else {return arr[position ]--;}
}
else {
return arr[ position]--;
}
}
return arr[position ]--;
}
public static int[] removeTheElement(int[] arr, int index)
{
// If the array is empty
// or the index is not in array range
// return the original array
if (arr == null || index < 0
|| index >= arr.length) {
return arr;
}
// Create another array of size one less
int[] anotherArray = new int[arr.length - 1];
// Copy the elements except the index
// from original array to the other array
for (int i = 0, k = 0; i < arr.length; i ) {
// if the index is
// the removal element index
if (i == index) {
continue;
}
// if the index is not
// the removal element index
anotherArray[k ] = arr[i];
}
// return the resultant array
return anotherArray;
}
public static boolean allNegative (int arr[]) {
for (int i = 0; i < arr.length; i ) {
if (arr[i] > 0) return false;
}
return true;
}
public boolean isFinished() {
for (int var: arr) {
if (var > 0) return false;
}
return true;
}
}
The main method and how the output should look:
public class Main {
public static void main (String args[]) {
DecrementingCarousel carousel = new DecrementingCarousel(7);
carousel.addElement(2);
carousel.addElement(3);
carousel.addElement(1);
CarouselRun run = carousel.run();
System.out.println(run.isFinished()); //false
System.out.println(run.next()); //2
System.out.println(run.next()); //3
System.out.println(run.next()); //1
System.out.println(run.next()); //1
System.out.println(run.next()); //2
System.out.println(run.next()); //1
System.out.println(run.isFinished()); //true
System.out.println(run.next()); //-1
}
}
CodePudding user response:
You need to loop through the array until you find an index that has a value that is greater than 0. You can also use that loop to check that there are any values greater than 0:
public int next() {
int count = 0;
while (count < arr.length && arr[position %= arr.length] <= 0) {
position ;
count ;
}
if (count == arr.length) return -1;
return arr[position ]--;
}