Last active
December 18, 2018 16:34
-
-
Save shashank21j/f2cbf784395dde2b675c to your computer and use it in GitHub Desktop.
HackerRank custom checker
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
Here is a standard template to work with custom checker | |
``` | |
from __future__ import division | |
import json | |
from sys import stdin | |
def check_inputs (inputs, outputs): | |
#inputs is a "list" of all input files. (Read carefully) | |
lis = [] # lis is a 0/1 array which means 1 -> testcase success and 0-> test case failed | |
iterate = -1 | |
for inps, outps in zip(inputs, outputs): | |
#inps and outs is of 1 perticular file. Remember to split on "\n". default split is a common mistake. | |
#Remember to trim the lines. | |
#Custom checker should never throw an error, remember to run it in try: except blocks and verify input to avoid infinite time errors | |
lis.append(0) | |
iterate += 1 | |
result = 0 | |
lis[iterate] = result | |
return True, lis | |
def process(data): | |
score = 0 | |
testcase_input = data["inputs"] | |
testcase_output = data["expected_outputs"] | |
user_output = data["outputs"] | |
a,b = check_inputs(testcase_input,user_output) | |
return score, b | |
if __name__ == '__main__': | |
stdin = stdin.read().split('\n') | |
# Get the run directory for the submission we need to evaluate. I know, | |
# hackish right? | |
run_directory = stdin[-1] | |
# request.json contains the data submitted for the current. | |
request = json.load(open(run_directory + "request.json")) | |
data = { | |
# The submitted source code | |
"source": request["source"], | |
# The list of test cases | |
"testcases": request["testcase_sources"], | |
# The submitted language key (Eg: 5 for python) | |
"lang": request["lang"], | |
"expected_outputs": request["expected_outputs"], | |
"outputs": [], | |
"inputs": [], | |
} | |
for index in range(len(request["testcase_sources"])): | |
output = open(run_directory + "output%05d.out" %(index)).read() | |
data["outputs"].append(output) | |
for index in range(len(request["testcase_sources"])): | |
inputs = open(run_directory + "input%05d.in" %(index)).read() | |
data["inputs"].append(inputs) | |
# Gathered all the data we could, lets process it | |
score, testcase_status = process(data) | |
print score | |
print " ".join(str(i) for i in testcase_status) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! This is really helpful :)