I am doing the find the parity kata in Java. I am not sure why but my junit test fails as it returns -10 instead of 3 for part of the first test. If someone could please let me know as to why that is? The test fails at example test1 as it returns -10 instead of 3.
Update I amended my code(please see below) to do the following. So now it passes all the tests in eclipse but for some reason still fails in the code wars website. The error message is expected:<2> but was:<7>
import java.util.ArrayList;
public class FindOutlier {
private ArrayList<Integer> odds = new ArrayList<Integer>();
private ArrayList<Integer> evens = new ArrayList<Integer>();
public static void main(String[] args) {
}
public int find(int[] integers) {
int finalResult = 0;
for (int i = 0; i < integers.length; i++) {
if (integers[i] % 2 != 0) {
odds.add(integers[i]);
} else {
evens.add(integers[i]);
}
}
if (evens.size() > odds.size()) {
finalResult += odds.get(odds.size()-1);
} else {
finalResult += evens.get(evens.size()-1);
}
return finalResult;
}
}
and here is the jnuit test
package Tests;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import Supermarket_Pricing.FindOutlier;
public class OutlierTest{
private FindOutlier foo;
@Before
public void setup(){
foo = new FindOutlier();
}
@Test
public void testExample() {
int[] exampleTest1 = {2,6,8,-10,3};
int[] exampleTest2 = {206847684,1056521,7,17,1901,21104421,7,1,35521,1,7781};
int[] exampleTest3 = {Integer.MAX_VALUE, 0, 1};
assertEquals(3, foo.find(exampleTest1));
assertEquals(206847684, foo.find(exampleTest2));
assertEquals(0, foo.find(exampleTest3));
}}