Last active
February 18, 2020 20:17
-
-
Save patilarpith/584ff7b714fc1940070d326031af2efd to your computer and use it in GitHub Desktop.
Custom checker in scala
This file contains hidden or 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 HEAD | |
import org.json.simple.JSONArray | |
import org.json.simple.JSONObject | |
import org.json.simple.parser.JSONParser | |
import collection.JavaConversions._ | |
// End of HEAD | |
// Start of Body | |
/** | |
* TestStruct:: | |
* testcase_id [long] ID of the test-case | |
* testcase_input_path [String] File path to test-case input | |
* testcase_output_path [String] File path to test-case output generated by the problem solver | |
* testcase_expected_output_path [String] File path to test-case expected output to be matched with | |
* metadata_file_paths [ArrayList<String>] File paths to Question metadata (Extra files usually used for defining traning sets) | |
* submission_code_path [String] File path to submission source code | |
* testcase_result [boolean] Set to true if test-case output matches test-case expected output. Matching is done line by line | |
* testcase_signal [long] Exit code of the test-case process | |
* testcase_time [double] Time taken by the test-case process in seconds | |
* testcase_memory [long] Peak memory of the test-case process determined in bytes | |
* data [String] <Future use> | |
* | |
* | |
* ResultStruct:: | |
* result [boolean] Assign test-case result. true determines success. false determines failure | |
* score [double] Assign test-case score. Normalized between 0 to 1 | |
* message [String] Assign test-case message. This message is visible to the problem solver | |
**/ | |
object Solution { | |
def run_custom_checker(t_obj: TestStruct, r_obj: ResultStruct) { | |
println("testcase_id: " + t_obj.testcase_id) | |
println("testcase_input_path: " + t_obj.testcase_input_path) | |
println("testcase_output_path: " + t_obj.testcase_output_path) | |
println("testcase_expected_output_path: " + t_obj.testcase_expected_output_path) | |
t_obj.metadata_file_paths.foreach((path: String) => { | |
println("metadata_file_paths: " + path) | |
}) | |
println("submission_code_path: " + t_obj.submission_code_path) | |
println("testcase_result: " + t_obj.testcase_result) | |
println("testcase_signal: " + t_obj.testcase_signal) | |
println("testcase_time: " + t_obj.testcase_time) | |
println("testcase_memory: " + t_obj.testcase_memory) | |
println("data: " + t_obj.data) | |
r_obj.score = 1.0f | |
r_obj.result = true | |
r_obj.message = "Success" | |
} | |
// End of Body | |
// Start of Tail | |
def write_result_json(r_obj: ResultStruct) { | |
var map = scala.collection.mutable.Map[String, Object]() | |
map += ("custom_result" -> new Integer(if (r_obj.result) 1 else 0)) | |
map += ("custom_score" -> new java.lang.Double(if (r_obj.score > 1.0f) 1.0f else if (r_obj.score < 0.0f) 0.0f else r_obj.score)) | |
map += ("custom_message" -> new String(if (r_obj.message.length > 4096) r_obj.message.substring(0, 4095) else r_obj.message)) | |
val json_obj = new JSONObject(map) | |
println(json_obj) | |
} | |
def main(args: Array[String]) { | |
val r_obj = new ResultStruct() | |
if (args.length < 1) { | |
write_result_json(r_obj) | |
System.exit(1) | |
} | |
val json_file_path = args(0).toString() | |
var json_obj: JSONObject = null | |
var t_obj: TestStruct = null | |
try { | |
json_obj = new JSONParser().parse(new java.io.FileReader(json_file_path)).asInstanceOf[JSONObject] | |
t_obj = new TestStruct(json_obj) | |
} catch { | |
case _: Throwable => { | |
r_obj.message = "Unable to read input json" | |
write_result_json(r_obj) | |
System.exit(1) | |
} | |
} | |
run_custom_checker(t_obj, r_obj) | |
write_result_json(r_obj) | |
} | |
} | |
class TestStruct(json_obj: JSONObject) { | |
val testcase_id = json_obj.get("testcase_id").asInstanceOf[Long] | |
val testcase_input_path = json_obj.get("input_file_path").asInstanceOf[String] | |
val testcase_output_path = json_obj.get("output_file_path").asInstanceOf[String] | |
val testcase_expected_output_path = json_obj.get("expected_output_file_path").asInstanceOf[String] | |
val metadata_file_paths = if (json_obj.containsKey("metadata_file_paths") && json_obj.get("metadata_file_paths") != null) json_obj.get("metadata_file_paths").asInstanceOf[JSONArray].toArray().map(_.toString()) else Array[String]() | |
val submission_code_path = json_obj.get("submission_code_path").asInstanceOf[String] | |
val testcase_result = json_obj.get("testcase_result").asInstanceOf[Boolean] | |
val testcase_signal = json_obj.get("testcase_signal").asInstanceOf[Long] | |
val testcase_time = json_obj.get("testcase_time") | |
val testcase_memory = json_obj.get("testcase_memory").asInstanceOf[Long] | |
val data = json_obj.get("data").asInstanceOf[String] | |
} | |
class ResultStruct { | |
var result: Boolean = false | |
var score: Double = 0.0f | |
var message: String = "Uninitialized" | |
} | |
// End of Tail |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Testing:
Input file data. Path to be passed as command line argument
Expected output: