Adventure Anonymous

Coding Problem 1 | CodeFights - Calculate average score of testers

This is the solution to one of the problems on CodeFight. I had trouble using the IDE on the website because the syntax errors were not clear so I used Eclipse to create test cases. Basically the problem was asking to calculate a score for each member of a company that took a coding test. You would get as input an array like this:

1
2
3

int[][] trainingData = {{5,1},{4,-1},{0,0},{6,1}};

Then you have to feed it into the function to get a score back. In the 2D array trainingData each element which is an array in itself has 2 integers. The first integer gives you the score of the person in terms of how fast they solved the problem and the second integer tells you whether they solved the problem (1 = pass, anything else is fail). The simple solution I came up with was to add up all the scores of people who got it right and then divide. There was only one edge case in this problem where if there were 0 people who passed, you must return 0 as the score.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

public class companyBotStrategy {
double companyBotStrategyFunc(int[][] trainingData) {
double totalScore=0;
int numCandidates=0;


for(int correctIndex=0; correctIndex < trainingData.length; correctIndex++){
if(trainingData[correctIndex][1] == 1) {
numCandidates++;
totalScore+=trainingData[correctIndex][0];
}
}
if (numCandidates==0){
return 0;
}
System.out.println("totalScore = " + totalScore + " numCandidates = " + numCandidates);
return (totalScore / numCandidates);

}
}