Skip to content

Instantly share code, notes, and snippets.

@Shafaet
Created February 9, 2016 11:16
Show Gist options
  • Save Shafaet/f846057ece83ec7c8611 to your computer and use it in GitHub Desktop.
Save Shafaet/f846057ece83ec7c8611 to your computer and use it in GitHub Desktop.
Checker
1. Input to custom checker will be a directory. There are some unwanted white spaces (including line feeds) at the beginning and/or end. Make sure that you trim them off.
2. In the given directory, there exists a `request.json` file [sample file is attached]. One of the fields will be `expected_outputs` which contains a array of strings. There will be `n` elements in this array, representing the expected output of `n` test cases.
3. There will be `n` input files in the same directory. Suppose there are `n = 20` test cases, then input files will be named as `input00000.in`, `input00001.in`, `input00002.in`,..., `input00009.in`, `input00010.in`, `input00011.in` ,..., `input00019.in`.
4. Similarly, there will be `n` output files, named same as above. Eg. `ouptput00000.in`, `ouptput00001.in`, `ouptput00002.in`,..., `ouptput00009.in`, `ouptput00010.in`, `ouptput00011.in` ,..., `ouptput00019.in`. They are placed in the same directory.
5. You have to read respective input, output (and expected output, if required) and process them. You can do them separately.
6. You have to print scores to stdout, in three different lines.
a. In first line, you will have to print total score in the scale of [0, 1] (It can be a floating point value).
b. For each of the `n` test cases, you have to print `n` space separated 0/1. A 1 at i'th place represents that i'th test case is passed, otherwise failed.
c. Again you have to print `n` space separated floating point numbers. It is the score attained for each test case on the scale of [0, 1].
NOTE: Expected output can be used for reference purpose. Even if you don't require them, don't leave output files empty while adding test cases.You can add something like '-' to them.
---
###Sample Checkers
```java
//Author: Abhiranjan Kumar//{{{
//Problem Curator, Developer @ HackerRank
import java.io.*;
import java.util.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
//}}}
public class Checker
{
// If a test case score is greater than `thresholdPerTest` then it is marked as "Success", otherwise as "Wrong Answer"
static double thresholdPerTest = 0.5;
/*
* Arguments represent input file path, output file path, and expected output (no file path here).
* It must return a value between 0.0 to 1.0.
*/
double evaluateScore(String inputFilePath, String outputFilePath, String expOutput) {
// Compelte this function
Random rnd = new Random();
return rnd.nextDouble();
}
public static void main(String [] args) throws Exception
{
String baseDir = new Scanner(System.in).next();
String json_file = baseDir + "request.json";
String raw_json = new Scanner(new File(json_file)).useDelimiter("\\Z").next();
JSONObject jObj = (JSONObject)JSONValue.parse(raw_json);
JSONArray jArr = (JSONArray)jObj.get("expected_outputs");
int numTestCases = jArr.size();
Checker checker = new Checker();
double totalScore = 0.0;
double []scores = new double [numTestCases];
for(int testIdx = 0; testIdx < numTestCases; testIdx++) {
String expOutput = (String)jArr.get(testIdx);
String inputFilePath = String.format("%sinput%05d.in" , baseDir, testIdx);
String outputFilePath = String.format("%soutput%05d.out", baseDir, testIdx);
double testScore = checker.evaluateScore(inputFilePath, outputFilePath, expOutput);
if(Double.isNaN(testScore))
testScore = 0;
if(Double.isInfinite(testScore))
testScore = 0;
testScore = Math.min(1.0, testScore);
testScore = Math.max(0.0, testScore);
totalScore += testScore;
scores[testIdx] = testScore;
}
// Line 1: Total score
totalScore /= (double)Math.max(1, numTestCases);
System.out.println(totalScore);
// Line 2: `N` space separated integers (1 or 0) rerpesenting success of test case.
for(int i = 0; i < numTestCases; i++) {
int s = scores[i] >= thresholdPerTest ? 1 : 0;
System.out.print(s + " ");
}
System.out.println("");
// Score per test case, scaled in range [0.0, 1.0]
for(int i = 0; i < numTestCases; i++) {
System.out.print(scores[i] + " ");
}
System.out.println("");
}
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment