I have been tasked to solve a question concerning the creation of a triple-ended queue with efficient random access, as outlined in this: https://open.kattis.com/problems/teque. I created a program based around using 2 very large arrays, one containing the front half of all stored integers so far and the other the back half, with both being of the same size or the front half containing at most 1 more element than the back half after every insertion operation. This should allow all insertion and retrieval operations to be of O(1) time complexity, but the code just keeps exceeding the given time limit. Can anyone tell me what is wrong with my code? Here it is:
import java.util.*;
import java.io.*;
public class Teque3 {
static int[] front = new int[1_000_000];
static int[] back = new int[1_000_000];
static int frontHead = 499_999;
static int backHead = 499_999;
static int frontSize = 0;
static int backSize = 0;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
for (int i = 0; i < n; i++) {
String[] line = br.readLine().split(" ");
if (line[0].equals("get")) {
int index = Integer.parseInt(line[1]);
if (index >= frontSize) System.out.println(back[backHead + index - frontSize]);
else System.out.println(front[frontHead + index]);
continue;
}
if (frontSize == backSize) {
if (line[0].equals("push_front")) {
frontHead--;
front[frontHead] = Integer.parseInt(line[1]);
frontSize++;
} else if (line[0].equals("push_back")) {
back[backHead + backSize] = Integer.parseInt(line[1]);
front[frontHead + frontSize] = back[backHead];
frontSize++;
backHead++;
} else if (line[0].equals("push_middle")) {
front[frontHead + frontSize] = Integer.parseInt(line[1]);
frontSize++;
}
} else {
if (line[0].equals("push_front")) {
frontHead--;
front[frontHead] = Integer.parseInt(line[1]);
backHead--;
back[backHead] = front[frontHead + frontSize];
backSize++;
} else if (line[0].equals("push_back")) {
back[backHead + backSize] = Integer.parseInt(line[1]);
backSize++;
} else if (line[0].equals("push_middle")) {
backHead--;
back[backHead] = Integer.parseInt(line[1]);
backSize++;
}
}
}
}
}
You could try to minimze IO-Operations: Collect your programm output. Instead of writing System.out.println better create a new StringBuilder to collect everything. In the end write all at once.
static StringBuilder result = new StringBuilder();
...
private static void result(int value) {
result.append(value).append("\n");
}
...
if (index >= frontSize) result(back[backHead + index - frontSize]);
else result(front[frontHead + index]);
...
System.out.println(result);
Decouple read from parse and process: Create one thread for reading the operations. But the operations in a Queue. Start another thread for the process.
I 'm looking for a way to read a range of elements in an array of unknown dimension ( not length).
The client can send a read request for an object and specify the range to read. The input String could be like this : "1:2:3:2,2:3:1:4" for example. This would mean he wants to read the elements in the range from [1][2][3][2] to [2][3][1][4] of an array.
To read a concrete element I created this function:
public Object readValue(Object obj,int[] positions ) {
Object value = null; //Result
int objDimension = getDimension(obj); //Dimesion of the array
System.out.println("Dimension: " + objDimension );
try {
Object[] aux = (Object[]) obj;
for (int i = 0; i < objDimension - 1; i++) {
int pos = positions[i];
aux = (Object[]) aux[pos];
}
value = aux[positions[objDimension - 1]];
System.out.println("Result: " + value);
} catch (ArrayIndexOutOfBoundsException e) {
// TODO: Send a fault to the client.
System.out.println("Error: "+e.getMessage());
}
return value;
}
public static int getDimension(Object value) {
Class<?> clazz = value.getClass();
String className = clazz.getName();
int dimension = 0;
for (int i = 0; i < className.length(); i++) {
if (className.charAt(i) != '[') {
dimension = i;
break;
}
}
return dimension;
}
//Example.
public static void main(String[] args) {
// TODO code application logic here
TestMultiDimensioNRead test = new TestMultiDimensioNRead();
Integer[][][][] testSubject = new Integer[5][2][4][];
testSubject[0][0][2] = new Integer[8];
testSubject[0][0][0] = new Integer[15];
testSubject[0][0][1] = new Integer[20];
testSubject[0][0][3] = new Integer[2];
testSubject[1][1][2] = new Integer[7];
testSubject[1][1][2][0] = 80;
test.readValue(testSubject,new int[]{1, 1, 2, 0});
}
I was thinking a good way may be to calculate the differens between each dimension length.
If anyone can come with a good idea, I would really appreciatee.
Thanks in advance.
EDIT 1: The code posted in this question does read the value of a given position in an array of unknown dimension. My problem is to read all the elements that are between to given points. This might not have been clear in the initial question.
You could use a recursive solution:
public class Test {
private class TestMultiDimensioNRead {
public Integer readValue(Object testSubject, int[] coordinates) {
return readValue(testSubject, coordinates, 0);
}
private Integer readValue(Object testSubject, int[] coordinates, int which) {
if (testSubject instanceof Object[]) {
Object[] subject = (Object[]) testSubject;
if (coordinates.length > which + 1) {
return readValue(subject[coordinates[which]], coordinates, which + 1);
} else {
return (Integer) subject[coordinates[which]];
}
} else {
// Throw some sort of exception?
return -1;
}
}
public Iterator<Integer> readValues(Object testSubject, int[] coordinates, int count) {
return readValues(testSubject, coordinates, count, 0);
}
private Iterator<Integer> readValues(Object testSubject, int[] coordinates, int count, int level) {
if (testSubject instanceof Object[]) {
Object[] subject = (Object[]) testSubject;
if (coordinates.length > level + 1) {
return readValues(subject[coordinates[level]], coordinates, count, level + 1);
} else {
return new Iterator<Integer>() {
int i = 0;
Integer[] intSubject = (Integer[]) subject;
#Override
public boolean hasNext() {
return i <= count;
}
#Override
public Integer next() {
return intSubject[coordinates[level] + (i++)];
}
};
}
} else {
// Throw some sort of exception?
return null;
}
}
}
public void test() {
TestMultiDimensioNRead test = new TestMultiDimensioNRead();
Integer[][][][] testSubject = new Integer[5][2][4][];
testSubject[0][0][2] = new Integer[8];
testSubject[0][0][0] = new Integer[15];
testSubject[0][0][1] = new Integer[20];
testSubject[0][0][3] = new Integer[2];
testSubject[1][1][2] = new Integer[7];
testSubject[1][1][2][0] = 80;
testSubject[1][1][2][1] = 79;
testSubject[1][1][2][2] = 78;
Iterator<Integer> them = test.readValues(testSubject, new int[]{1, 1, 2, 0}, 3);
for (Integer x = them.next(); them.hasNext(); x = them.next()) {
System.out.println(x);
}
System.out.println();
}
public static void main(String args[]) {
try {
new Test().test();
} catch (Throwable t) {
t.printStackTrace(System.err);
}
}
}
Prints 80 as expected.
There's probably more to do in terms of sanity checks but this seems to work.
Found the way to do it, maybe it's helpfull at somepoint for someone.
I didn't include any checks, this is more a test case to see that is works.
public class TestReadMultiDimensionArray {
private int[] startPosition; //Start position.
private int[] endPosition; //End position.
private boolean inRange = false; //If the current position is in range.
private List<Object> result; //List to store the values we find.
public TestReadMultiDimensionArray() {
result = new ArrayList<>();
}
public static void main(String[] args) {
TestReadMultiDimensionArray test = new TestReadMultiDimensionArray();
Integer[][][][] testSubject = new Integer[2][2][4][];
//(0,0,y,z)
testSubject[0][0][0] = new Integer[]{1}; //(0,0,0,0)
testSubject[0][0][1] = new Integer[]{2}; //(0,0,1,0)
testSubject[0][0][2] = new Integer[]{3}; //(0,0,2,0)
testSubject[0][0][3] = new Integer[]{4}; //(0,0,3,0)
//(0,1,y,z)
testSubject[0][1][0] = new Integer[]{5}; //(0,1,0,0)
testSubject[0][1][1] = new Integer[]{6}; //(0,1,1,0)
testSubject[0][1][2] = new Integer[]{7, 8, 9}; //(0,1,2,0) (0,1,2,1) (0,1,2,2)
testSubject[0][1][3] = new Integer[]{10}; //(0,1,3,0)
//(1,0,y,z)
testSubject[1][0][0] = new Integer[]{11, 12}; //(1,0,0,0)..
testSubject[1][0][1] = new Integer[]{13, 14, 15};
testSubject[1][0][2] = new Integer[]{16, 17, 18};
testSubject[1][0][3] = new Integer[]{19, 20, 21}; //..(1,0,3,2)
//(1,1,y,z)
testSubject[1][1][0] = new Integer[]{22, 23}; //(1,1,0,0)..
testSubject[1][1][1] = new Integer[]{24, 25, 26};
testSubject[1][1][2] = new Integer[]{27, 28, 29, 30, 31, 32, 33, 34};
testSubject[1][1][3] = new Integer[]{35, 36}; //..(1,1,3,1)
//Launch the test.
test.readValue(testSubject);
}
/**
*
* #param obj The Array from where we want to get the data.
*/
public void readValue(Object obj) {
//Where should it start.
startPosition = new int[]{0, 1, 0, 0};
//Where should it stop.
endPosition = new int[]{1, 1, 1, 2};
System.out.println("Start Position:" + Arrays.toString(startPosition) + " End Position:" + Arrays.toString(endPosition));
int[] currentPosition = new int[]{-1, -1, -1, -1};
//Call to the method.
testRead((Object[]) obj, 0, currentPosition);
//Result to array.
Object[] arrayToReturn = result.toArray(new Object[0]);
System.out.println("Result: " + Arrays.toString(arrayToReturn));
}
/**
* Recursive method that looks for the values in a multi-dimensional array, in a given range. /!\ No checks are implemented here, wrong input can end in a
* StackOverFlow.
*
* #param obj The array in Object[] form.
* #param currentDimension The dimension we are currently in.
* #param result The reference to the list that will store all the values we found.
* #param currentPosition The current position we are in.
*/
private void testRead(Object[] obj, int currentDimension, int[] currentPosition) {
for (int i = 0; i < obj.length; i++) {
currentPosition[currentDimension] = i;
if (Arrays.equals(startPosition, currentPosition) && currentDimension == (currentPosition.length - 1)) {
//Found the start position.
System.out.println("############ START ############");
inRange = true;
}
if ((i >= startPosition[currentDimension] && i <= endPosition[currentDimension]) || inRange == true) {
//We are in the write track to get to the values we are looking for.
if (obj[i] instanceof Object[]) {
//The data contained in the cell is an array.
testRead((Object[]) obj[i], currentDimension + 1, currentPosition);
} else {
//The data contained in the cell is a scalar. This is what we where looking for.
System.out.println(Arrays.toString(currentPosition) + " Data: " + obj[i]);
result.add(obj[i]);
}
}
if (Arrays.equals(endPosition, currentPosition) && currentDimension == (currentPosition.length - 1)) {
//Found the end position.
System.out.println("############ END ############");
inRange = false;
}
}
}
}
Any question or idea to better the code is welcome.
When I attempt to compile my code, this error occurs:
Note: GoFishEdited.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
When I tried to run it, this error message occurs:
Exception in thread "main" java.lang.NullPointerException
at Habitat.stockUp(Habitat.java:20)
at GoFishEdited.main(GoFishEdited.java:14)
I'm pretty sure the error is in the stoockUp method, but I don't understand what could be wrong with it.
Here is my code:
public class GoFishEdited {
public static void main(String[] args) {
System.out.println("\nProject 1, Stage 3\n");
int[] fishArray;
ArrayList<Fish> catchF = new ArrayList<Fish>();
Habitat h1 = new Habitat();
Habitat h2 = new Habitat();
fishArray = h1.stockUp();
System.out.println("Start with some weights:");
for (int i : fishArray) {
System.out.print(i + " ");
}
System.out.println("\n\nMake fish of those weights.\n");
Fish[] fishGroup = new Fish[fishArray.length]; // array of Fish
for (int i=0; i < fishArray.length; i++) {
fishGroup[i] = new Fish(fishArray[i]); // make fish
}
System.out.println("Fish are made. Now put them in a habitat:\n");
for (Fish f : fishGroup) {
h1.addFish(f);
}
System.out.println("\nAll in. The habitat displays them:\n");
h1.printFish();
System.out.println("\nMove some fish to the second habitat.\n");
for(Fish f : fishGroup){
h2.addFish(f);
}
System.out.println("\nPrint both habitats:\n");
h1.printFish();
h2.printFish();
System.out.println("\nCatch some fish.\n");
for(Fish f : fishGroup){
catchF = h1.catchFish(f);
}
}
}
And:
public class Habitat {
ArrayList<Fish> stringer = new ArrayList<Fish>();
int[] fishArr;
public int maxCount=25;
public int minCount=9;
public int maxWeight=10;
public int minWeight=1;
public int catchProbability=30; //0.3
public ArrayList<Fish> catches = new ArrayList<Fish>();
public int[] stockUp(){
int numofF = minCount + (int)(Math.random() * ((maxCount - minCount) + 1));
for(int i = 0; i<numofF; i++){
fishArr[i] = minWeight + (int)(Math.random() * ((maxWeight - minWeight) + 1));
}
return fishArr;
}
public Habitat(){
int[] hab;
}
public void addFish(Fish f) {
stringer.add(f);
}
public void removeFish(Fish f){
stringer.remove(f);
}
public void printFish(){
System.out.println(stringer);
}
public ArrayList catchFish(Fish f){
int caught = 0 + (int)(Math.random() * ((100 - 0) + 1));
if(caught < catchProbability){
catches.add(f);
stringer.remove(f);
}
return catches;
}
public void printGrid(int[][] twoD){
int i, j;
for ( i = 0 ; i < twoD.length ; i++ ){
for ( j = 0 ; j < twoD[i].length ; j++ ){
System.out.print( twoD[i][j] + " " );
}
System.out.println();
}
}
public void toGrid(String[] oneD){
int cols = (int) Math.floor(Math.sqrt(oneD.length));
int currentCol = 0;
for(String element : oneD) {
System.out.print(element + "\t");
if(currentCol >= cols) {
System.out.println("");
currentCol = 0;
}
else {
currentCol++;
}
}
}
}
I would appreciate an explanation, because I am thoroughly confused, and just want to see if it'll work properly.
The problem is that you never initialize fishArr in Habitat
And the unchecked operation warning might come from public ArrayList catchFish(Fish f){, you should parameterize your return type
So I have this script I've written:
public class Primes {
public static void main(String[] args) {
}
//Part 4: Question 1
public static int Binary2int(String b){
int size = b.length();
double count, sum=0;
boolean binary = true;
for (int i=0; i<b.length() ; i++){
int digit = b.charAt(i)-'0';
if (digit>1){
binary = false;
i=b.length();
}
count = digit*Math.pow(2, size-i-1);
sum+=count;
}
if (!binary){
System.out.println("Error - "+b+" is not a binary number.");
}
int sum1 = (int)sum;
return sum1;
}
//Part 4: Question 2
public static boolean isBinaryString(String b){
boolean binary = true;
for (int i=0; i<b.length() ; i++){
int digit = b.charAt(i)-'0';
if (digit>1){
binary = false;
i=b.length();
}
}
return binary;
}
//Part 4: Question 3
public static String int2Binary(int n){
int count=0;
if (n<=0){
count = 1;
}
for (int i=n; i>0 ;){
i=i/2;
count++;
}
int arr[] = new int [count];
for (int i=n, t=0; i>0 ;t++){
arr[arr.length-1-t] = i%2;
i = i/2;
}
if (n<=0){
arr[0] = 0;
}
String s = Arrays.toString(arr);
return s;
}
}
4.1 is meant to take a string (which stands for a binary number) and then return an integer(which stands for the integer the binary number in the input represents).
4.2 takes a binary number in the form of a string and then returns a Boolean variable (true if it’s a binary number and false if it isn’t).
4.3 is doing the opposite of 4.1, it takes an integer in the form of an integer and returns the binary number that represents it in the form of a string.
Oh and It all works fine when I'm testing it.
Now the problem is that the one checking it will be using the following (type of)script:
/**
* This class represents a tester - to be used by students to check Ex3: <br>
* 1. call all the public functions, check compilation. <br>
* 2. test some of function of their results. <br> <br>
* note: for debug change the printFlag to true.
*/
public class BynaryTest {
/**
* if set to true - will print a trace of all the checks.
*/
public static boolean printFlag = true;//false;
/**
* number of errors the test program found
*/
public static int error = 0;
/**
* this main function runs the test of the EX3Tester class
*/
public static void main(String[] a) {
System.out.println("******* Testing Ex3 - print mode = " + printFlag + " ********");
checkEx34();
System.out.println();
System.out.println("******* U have got " + error + " errors ********");
}
public static void checkEx34() {
if (printFlag) {
System.out.println("**** Cheking Ex21 ****");
}
int[] nums = {0, 1, 12345};
for (int i = 0; i < nums.length; i = i + 1) {
String s = Primes.int2Binary(nums[i]);
int num2 = Primes.Binary2int(s);
if (nums[i] != num2) {
error++;
System.out.println("** Error in EX34:" + num2 + "!=" + nums[i] + " **");
} else {
if (printFlag) {
System.out.println("num[" + i + "]=" + nums[i] + " binary: " + s + " .. ok");
}
}
}
}
}
The interaction between the two scripts does happen and nothing crashes, but then, for some reason, the only outcome I get from using it is:
** Error in EX34:21!=0 **
Error - [1] is not a binary number.
** Error in EX34:21!=1 **
Error - [1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1] is not a binary number.
** Error in EX34:21!=12345 **
******* U have got 3 errors ********
I've tried to fix it myself or finding out what causes that sort of unwanted outcome but I couldn't come up with anything :(
Have you tested it? The mistake is quite obvious if you do.
For example for 9 your Int2Binary method returns the following binary representation: [1, 0, 0, 1] as String.
Then in your Binary2Int you check if you only have 0s and 1s like so:
int digit = b.charAt(i)-'0'; // b is [1, 0, 0, 1]
if (digit>1){
binary = false;
//... more code ...
}
See the mistake now?
In your method intToBinary, you have the following line:
String s = Arrays.toString(arr);
I think you are expecting this to take an array and concat all the values in it into a single string. Given this example:
int[] array = {1, 0, 0};
System.out.println(Arrays.toString(array));
You expect the output to be:
100
When in fact it is:
[1,0,0]
Ok, I need to have a class that re-creates the Pascal Triangle. We are using BlueJ and I can't get my arrays to access each other.
Here's the code:
public class PascalTriangle {
private int currentLineNumber;
private int[] previousLineArray;
private int[] nextLineArray;
public void firstLine()
{
currentLineNumber = 1;
System.out.println("1");
}
public void nextLine()
{
if (currentLineNumber == 1) {
int [] previousLineArray = new int [(currentLineNumber+1)];
previousLineArray[0] = 1;
previousLineArray[1] = 1;
System.out.println("1 1");
currentLineNumber = 2;
}
else if(currentLineNumber >= 2) {
for (int lineCount = currentLineNumber; lineCount <= currentLineNumber; lineCount++) {
int [] nextLineArray = new int [(lineCount+1)];
nextLineArray[0] = 1;
System.out.print(nextLineArray[0] + " ");
for (int nextLineCount = 1; nextLineCount < lineCount; nextLineCount++) {
// The next line is the line with the NullPointerException
nextLineArray[(nextLineCount)] = (previousLineArray[(nextLineCount-1)
+ previousLineArray[(nextLineCount)]]);
System.out.print(nextLineArray[(nextLineCount)] + " ");
}
nextLineArray[(lineCount)] = 1;
System.out.print(nextLineArray[(lineCount)] + "\n");
previousLineArray = nextLineArray;
}
currentLineNumber = currentLineNumber+1;
}
}
}
The class will compile but as I get to the third line, which should read 1 2 1, I get a java.lang.NullPointerException at PascalTriangle.nextLine(PascalTriangle.java:29) it highlists the nextLineArray[(nextLineCount)] = (previousLineArray[(nextLineCount-1) line. Why will the nextLineArray take the information from previousLineArray which is set when the nextLine() method is called for the first time?
Any help would be appreciated :) Thanks.
the problem is the following:
in the line where int [] previousLineArray = new int [(currentLineNumber+1)]; - you create a local array that shadows your member and only visible inside of if statement. Then when you come to this line: nextLineArray[(nextLineCount)] = (previousLineArray[(nextLineCount-1) it uses your member array that was not init.