Skip to content

Instantly share code, notes, and snippets.

@winniehell
Last active August 29, 2015 14:10
Show Gist options
  • Save winniehell/87f73cb31a8d530581bb to your computer and use it in GitHub Desktop.
Save winniehell/87f73cb31a8d530581bb to your computer and use it in GitHub Desktop.
inline comments
public static int[] process(int[] inp, String oop) {
// initialize a new array with zeroes to use in error case <-- WRONG!
// this will be returned if the operator is not known <-- WRONG!
int[] oue = new int[] { Integer.MAX_VALUE, Integer.MAX_VALUE };
// set placeholder variable for plus operator
// this will be used to compare the input operator in the for loop below
String opp = "+";
// set placeholder variable for minus operator
// this will be used to compare the input operator in the for loop below
String opm = "-";
// set placeholder variable for multiply operator
// this will be used to compare the input operator in the for loop below
String opmu = "*";
// initialize a new array of size two for the calculated result
// this array will be returned at the end of the method
int[] oup = new int[2];
// check if the input array is invalid and return the error placeholder
if (inp == null) {
// return to avoid calculation
return oue;
}
// if multiplication is done, initialize output array with ones instead of zeros
if(opmu.equals(oop)) {
// set first element of output array to one
oup[0] = 1;
// set second element of output array to one
oup[1] = 1;
}
// assign a boolean flag which determines whether the input operator is invalid
// if this flag is set to true the method will set oup to oue and then return oup
boolean inv = false;
// loop over the whole input array by increasing the input array index by one for each step of the loop
// starting with zero
for(int idx = 0; idx < inp.length; ++idx) {
// set the index of the output array to the index of the input array modulo two
// this will retrieve zero for even numbers and one for odd numbers
int idy = idx % 2;
// check whether the input operator is the same as the placeholder for the plus operation
if(opp.equals(oop)) {
// add the value of the input array at the index idx to the output array at position idy
// idy is set to zero for even values of idx and to one for odd values of idx
oup[idy] += inp[idx];
}
// check whether the input operator is the same as the placeholder for the minus operation
else if(opm.equals(oop)) {
// subtract the value of the input array at the index idx from the output array at position idy
// idy is set to zero for even values of idx and to one for odd values of idx
oup[idy] -= inp[idx];
}
// check whether the input operator is the same as the placeholder for the multiplication operation
else if(opmu.equals(oop)) {
// multiply the value of the input array at the index idx from the output array at position idy
// idy is set to zero for even values of idx and to one for odd values of idx
oup[idy] *= inp[idx];
}
// if the input operator did not match the placeholder for the plus operation and did not match the <-- WRONG!
// placeholder for the minus operation, the operation is invalid <-- WRONG!
else {
// set the boolean flag which represents whether the input operator was not plus or minus to true <-- WRONG!
inv = true;
// stop executing the loop which processes the input array
break;
}
}
// if the input operation was not plus and not minus, use the array filled with zeroes as output <-- WRONG!
if(inv) {
// set all values of the output array to zero <-- WRONG!
oup = oue;
}
// return the input array processed by the input operator or output an array with zeros if the input operator <-- WRONG!
// was not plus and not minus <-- WRONG!
return oup;
}
public static int[] process(int[] input, String outputOperation) {
int[] outputError = new int[] { Integer.MAX_VALUE, Integer.MAX_VALUE };
String plusOperation = "+";
String minusOperation = "-";
String multiplyOperation = "*";
int[] output = new int[2];
if (input == null) {
return outputError;
}
if(multiplyOperation.equals(outputOperation)) {
output[0] = 1;
output[1] = 1;
}
boolean invalidOutputOperation = false;
for(int inputIndex = 0; inputIndex < input.length; ++inputIndex) {
int outputIndex = inputIndex % 2;
if(plusOperation.equals(outputOperation)) {
output[outputIndex] += input[inputIndex];
}
else if(minusOperation.equals(outputOperation)) {
output[outputIndex] -= input[inputIndex];
}
else if(multiplyOperation.equals(outputOperation)) {
output[outputIndex] *= input[inputIndex];
}
else {
invalidOutputOperation = true;
break;
}
}
if(invalidOutputOperation) {
output = outputError;
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment