summaryrefslogtreecommitdiff
path: root/yaksh/compare_stdio.py
diff options
context:
space:
mode:
authorPrabhu Ramachandran2017-05-25 18:58:31 +0530
committerGitHub2017-05-25 18:58:31 +0530
commiteae6ee7ceb25f78b216a5e2c9d6165513272e4cb (patch)
tree2758e1ec86c533b58d0fa3e7f4f5304bcd2431ec /yaksh/compare_stdio.py
parent80b67d07ceaf4c73705a27ee0bfc905e30b19ac4 (diff)
parent77f05d3df90a70ff97285deb5bda2d91d99e65d5 (diff)
downloadonline_test-eae6ee7ceb25f78b216a5e2c9d6165513272e4cb.tar.gz
online_test-eae6ee7ceb25f78b216a5e2c9d6165513272e4cb.tar.bz2
online_test-eae6ee7ceb25f78b216a5e2c9d6165513272e4cb.zip
Merge pull request #297 from maheshgudi/difflib
StdIO error output simplification
Diffstat (limited to 'yaksh/compare_stdio.py')
-rw-r--r--yaksh/compare_stdio.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/yaksh/compare_stdio.py b/yaksh/compare_stdio.py
new file mode 100644
index 0000000..c4076de
--- /dev/null
+++ b/yaksh/compare_stdio.py
@@ -0,0 +1,43 @@
+try:
+ from itertools import zip_longest
+except ImportError:
+ from itertools import izip_longest as zip_longest
+
+
+def _get_incorrect_user_lines(exp_lines, user_lines):
+ err_line_numbers = []
+ for line_no, (expected_line, user_line) in \
+ enumerate(zip_longest(exp_lines, user_lines)):
+ if not user_line or not expected_line or \
+ user_line.strip() != expected_line.strip():
+ err_line_numbers.append(line_no)
+ return err_line_numbers
+
+def compare_outputs(expected_output, user_output, given_input=None):
+ given_lines = user_output.splitlines()
+ exp_lines = expected_output.splitlines()
+ msg = {"given_input":given_input,
+ "expected_output": exp_lines,
+ "user_output":given_lines
+ }
+ ng = len(given_lines)
+ ne = len(exp_lines)
+ err_line_numbers = _get_incorrect_user_lines(exp_lines, given_lines)
+ msg["error_line_numbers"] = err_line_numbers
+ if ng != ne:
+ msg["error_msg"] = ("Incorrect Answer: "
+ + "We had expected {} number of lines. ".format(ne)
+ + "We got {} number of lines.".format(ng)
+ )
+ return False, msg
+ else:
+ if err_line_numbers:
+ msg["error_msg"] = ("Incorrect Answer: "
+ + "Line number(s) {0} did not match."
+ .format(", ".join(map(
+ str,[x+1 for x in err_line_numbers]
+ ))))
+ return False, msg
+ else:
+ msg["error_msg"] = "Correct Answer"
+ return True, msg