Custom-Checker body section for list of 5 prime numbers under 100
// Start of BODY | |
public class CustomChecker { | |
static boolean is_prime_number(int n) { | |
for(int i=2;i<n;i++) { | |
if(n%i==0) | |
return false; | |
} | |
return true; | |
} | |
static void run_custom_checker(final TestStruct t_obj, ResultStruct r_obj) { | |
// Read contents of the result file | |
String result_data; | |
try { | |
BufferedReader br = new BufferedReader(new FileReader(t_obj.testcase_output_path)); | |
StringBuilder sb = new StringBuilder(); | |
String line = br.readLine(); | |
while (line != null) { | |
sb.append(line); | |
sb.append(System.lineSeparator()); | |
line = br.readLine(); | |
} | |
br.close(); | |
result_data = sb.toString(); | |
} catch(IOException e) { | |
r_obj.result = false; | |
r_obj.score = 0; | |
r_obj.message = "Error reading result file"; | |
return; | |
} | |
String []values = result_data.split(" |\n"); | |
// Make sure all the values are unique | |
Set<String> uniq_values = new HashSet<String>(Arrays.asList(values)); | |
// Count the number of primes | |
int correct_values = 0; | |
for(String value:uniq_values) { | |
if (!value.isEmpty() && is_prime_number( Integer.parseInt(value) ) ) | |
correct_values++; | |
} | |
// Cutoff score to determine success | |
if(correct_values > 3) | |
r_obj.result = true; | |
else | |
r_obj.result = false; | |
r_obj.score = (float)correct_values / 5; | |
r_obj.message = correct_values + " out of 5 values are correct"; | |
} | |
// End of BODY |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment