fork download
  1.  
  2. // Start of HEAD
  3. #include <map>
  4. #include <cmath>
  5. #include <cstdio>
  6. #include <vector>
  7. #include <fstream>
  8. #include <iostream>
  9. #include <algorithm>
  10. #ifdef __APPLE__
  11. #include <json/json.h>
  12. #else
  13. #include <json/json.h>
  14. #endif
  15.  
  16. using namespace std;
  17. using namespace Json;
  18.  
  19. class TestStruct {
  20. public:
  21. size_t testcase_id;
  22. string testcase_input_path;
  23. string testcase_output_path;
  24. string testcase_expected_output_path;
  25. vector<string> metadata_file_paths;
  26. string submission_code_path;
  27. bool testcase_result;
  28. size_t testcase_signal;
  29. float testcase_time;
  30. size_t testcase_memory;
  31. string data;
  32. };
  33.  
  34. class ResultStruct {
  35. public:
  36. bool result;
  37. float score;
  38. string message;
  39. };
  40. // End of HEAD
  41.  
  42. // Start of BODY
  43. /**
  44.  * TestStruct members::
  45.  * testcase_id [size_t] ID of the test-case
  46.  * testcase_input_path [string] File path to test-case input
  47.  * testcase_output_path [string] File path to test-case output generated by the problem solver
  48.  * testcase_expected_output_path [string] File path to test-case expected output to be matched with
  49.  * metadata_file_paths [vector<string>] File paths to Question metadata (Extra files usually used for defining traning sets)
  50.  * submission_code_path [string] File path to submission source code
  51.  * testcase_result [bool] Set to true if test-case output matches test-case expected output. Matching is done line by line
  52.  * testcase_signal [size_t] Exit code of the test-case process
  53.  * testcase_time [float] Time taken by the test-case process in seconds
  54.  * testcase_memory [size_t] Peak memory of the test-case process determined in bytes
  55.  * data [string] <Future use>
  56.  *
  57.  *
  58.  * ResultStruct::
  59.  * result [bool] Assign test-case result. true determines success. false determines failure
  60.  * score [float] Assign test-case score. Normalized between 0 to 1
  61.  * message [string] Assign test-case message. This message is visible to the problem solver
  62. **/
  63.  
  64.  
  65. void run_custom_checker(const TestStruct t_obj,
  66. ResultStruct &r_obj) {
  67. //Don't print anything to STDOUT in this function
  68. //Enter your custom checker scoring logic here
  69.  
  70. r_obj.result = true;
  71. r_obj.score = 1.0f;
  72. r_obj.message = "Success";
  73. }
  74. // End of BODY
  75.  
  76. // Start of TAIL
  77. int read_input_json(const string json_file_path,
  78. TestStruct &t_obj) {
  79. ifstream stream(json_file_path);
  80. string json_file_contents((std::istreambuf_iterator<char>(stream)),
  81. std::istreambuf_iterator<char>());
  82.  
  83. Value root;
  84. Reader reader;
  85. if(!reader.parse(json_file_contents, root, false))
  86. return 1;
  87.  
  88. try {
  89. // Read values
  90. if(root.isMember("testcase_id"))
  91. t_obj.testcase_id = root["testcase_id"].asInt();
  92. if(root.isMember("input_file_path"))
  93. t_obj.testcase_input_path = root["input_file_path"].asString();
  94. if(root.isMember("output_file_path"))
  95. t_obj.testcase_output_path = root["output_file_path"].asString();
  96. if(root.isMember("expected_output_file_path"))
  97. t_obj.testcase_expected_output_path = root["expected_output_file_path"].asString();
  98. if(root.isMember("metadata_file_paths")) {
  99. Value metadata_file_path_node = root["metadata_file_paths"];
  100. if(metadata_file_path_node.isArray()) {
  101. for(int i = 0; i < (int)metadata_file_path_node.size(); i++) {
  102. string metadata_file = metadata_file_path_node[i].asString();
  103. t_obj.metadata_file_paths.push_back(metadata_file);
  104. }
  105. }
  106. }
  107. if(root.isMember("submission_code_path"))
  108. t_obj.submission_code_path = root["submission_code_path"].asString();
  109. if(root.isMember("testcase_result"))
  110. t_obj.testcase_result = root["testcase_result"].asBool();
  111. if(root.isMember("testcase_signal"))
  112. t_obj.testcase_signal = root["testcase_signal"].asInt();
  113. if(root.isMember("testcase_time"))
  114. t_obj.testcase_time = root["testcase_time"].asFloat();
  115. if(root.isMember("testcase_memory"))
  116. t_obj.testcase_memory = root["testcase_memory"].asInt();
  117. if(root.isMember("data"))
  118. t_obj.data = root["data"].asString();
  119. }
  120. catch(const runtime_error& error) {
  121. return 1;
  122. }
  123.  
  124. return 0;
  125. }
  126.  
  127. void write_result_json(const ResultStruct r_obj) {
  128. Value root;
  129. root["custom_result"] = (int)r_obj.result;
  130. root["custom_score"] = max( ((r_obj.score > 1.0f)? 1.0f : r_obj.score), 0.0f);
  131. root["custom_message"] = (r_obj.message.size() > 4096)? r_obj.message.substr(0, 4095) : r_obj.message;
  132. cout << root.toStyledString() << endl;
  133. }
  134.  
  135. int main(int argc, char** argv) {
  136. // Input parameters
  137. TestStruct t_obj;
  138. t_obj.testcase_id = 0;
  139. t_obj.testcase_signal = 0;
  140. t_obj.testcase_memory = 0;
  141. t_obj.testcase_time = 0.0f;
  142. t_obj.testcase_result = true;
  143.  
  144. // Out parameters
  145. ResultStruct r_obj;
  146. r_obj.result = false;
  147. r_obj.score = 0.0f;
  148. r_obj.message = "Uninitialized";
  149.  
  150. if(argc < 2) {
  151. write_result_json(r_obj);
  152. return 1;
  153. }
  154.  
  155. // Decode input JSON
  156. int failure = read_input_json((string)argv[1],
  157. t_obj);
  158. // Incase input JSON was malformed or not existent
  159. if(failure) {
  160. r_obj.message = "Unable to read input json";
  161. write_result_json(r_obj);
  162. return 2;
  163. }
  164.  
  165. // Run the custom checker evaluator
  166. run_custom_checker(t_obj, r_obj);
  167.  
  168. // Encode result JSON
  169. write_result_json(r_obj);
  170. return 0;
  171. }
  172. // End of TAIL
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:13:27: fatal error: json/json.h: No such file or directory
compilation terminated.
stdout
Standard output is empty