Last active
January 21, 2023 09:04
-
-
Save patilarpith/7b17b5afb9218f770ebc528747017d25 to your computer and use it in GitHub Desktop.
Custom-Checker body section for list of 5 prime numbers under 100
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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