From 3bbaa1e17778fb3790a9546d365444c38df31e1a Mon Sep 17 00:00:00 2001 From: mahesh Date: Sat, 13 May 2017 02:14:13 +0530 Subject: added pretty stdio functionality for all languages. --- yaksh/compare_stdio.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 yaksh/compare_stdio.py (limited to 'yaksh/compare_stdio.py') diff --git a/yaksh/compare_stdio.py b/yaksh/compare_stdio.py new file mode 100644 index 0000000..ba258c3 --- /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 + +class CompareOutputs(object): + + def _incorrect_user_lines(self, exp_lines, user_lines): + err_line_no = [] + for i, (expected_line, user_line) in enumerate(zip_longest(exp_lines, user_lines)): + if not user_line or not expected_line: + err_line_no.append(i) + else: + if user_line.strip() != expected_line.strip(): + err_line_no.append(i) + return err_line_no + + def compare_outputs(self, expected_output, user_output,given_input=None): + given_lines = user_output.splitlines() + exp_lines = expected_output.splitlines() + # if given_input: + # given_input = given_input.splitlines() + msg = {"given_input":given_input, + "expected_output": exp_lines, + "user_output":given_lines + } + ng = len(given_lines) + ne = len(exp_lines) + if ng != ne: + err_line_no = self._incorrect_user_lines(exp_lines, given_lines) + msg["error_no"] = err_line_no + msg["error"] = "We had expected {0} number of lines. We got {1} number of lines.".format(ne, ng) + return False, msg + else: + err_line_no = self._incorrect_user_lines(exp_lines, given_lines) + if err_line_no: + msg["error_no"] = err_line_no + msg["error"] = "Line number(s) {0} did not match."\ + .format(", ".join(map(str,[x+1 for x in err_line_no]))) + return False, msg + else: + msg["error"] = "Correct answer" + return True, msg -- cgit From 94f51e9c4286224057a404e48fad5069a4ed332c Mon Sep 17 00:00:00 2001 From: mahesh Date: Wed, 17 May 2017 12:26:06 +0530 Subject: changed assertions for stdio test cases --- yaksh/compare_stdio.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'yaksh/compare_stdio.py') diff --git a/yaksh/compare_stdio.py b/yaksh/compare_stdio.py index ba258c3..3b1b998 100644 --- a/yaksh/compare_stdio.py +++ b/yaksh/compare_stdio.py @@ -29,13 +29,13 @@ class CompareOutputs(object): if ng != ne: err_line_no = self._incorrect_user_lines(exp_lines, given_lines) msg["error_no"] = err_line_no - msg["error"] = "We had expected {0} number of lines. We got {1} number of lines.".format(ne, ng) + msg["error"] = "Incorrect Answer: We had expected {0} number of lines. We got {1} number of lines.".format(ne, ng) return False, msg else: err_line_no = self._incorrect_user_lines(exp_lines, given_lines) if err_line_no: msg["error_no"] = err_line_no - msg["error"] = "Line number(s) {0} did not match."\ + msg["error"] = "Incorrect Answer: Line number(s) {0} did not match."\ .format(", ".join(map(str,[x+1 for x in err_line_no]))) return False, msg else: -- cgit From b591928d868ecf92660e43be72348334073ed502 Mon Sep 17 00:00:00 2001 From: mahesh Date: Wed, 17 May 2017 18:51:58 +0530 Subject: adds pep8 changes --- yaksh/compare_stdio.py | 76 ++++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 37 deletions(-) (limited to 'yaksh/compare_stdio.py') diff --git a/yaksh/compare_stdio.py b/yaksh/compare_stdio.py index 3b1b998..c9c631f 100644 --- a/yaksh/compare_stdio.py +++ b/yaksh/compare_stdio.py @@ -4,40 +4,42 @@ except ImportError: from itertools import izip_longest as zip_longest class CompareOutputs(object): - - def _incorrect_user_lines(self, exp_lines, user_lines): - err_line_no = [] - for i, (expected_line, user_line) in enumerate(zip_longest(exp_lines, user_lines)): - if not user_line or not expected_line: - err_line_no.append(i) - else: - if user_line.strip() != expected_line.strip(): - err_line_no.append(i) - return err_line_no - - def compare_outputs(self, expected_output, user_output,given_input=None): - given_lines = user_output.splitlines() - exp_lines = expected_output.splitlines() - # if given_input: - # given_input = given_input.splitlines() - msg = {"given_input":given_input, - "expected_output": exp_lines, - "user_output":given_lines - } - ng = len(given_lines) - ne = len(exp_lines) - if ng != ne: - err_line_no = self._incorrect_user_lines(exp_lines, given_lines) - msg["error_no"] = err_line_no - msg["error"] = "Incorrect Answer: We had expected {0} number of lines. We got {1} number of lines.".format(ne, ng) - return False, msg - else: - err_line_no = self._incorrect_user_lines(exp_lines, given_lines) - if err_line_no: - msg["error_no"] = err_line_no - msg["error"] = "Incorrect Answer: Line number(s) {0} did not match."\ - .format(", ".join(map(str,[x+1 for x in err_line_no]))) - return False, msg - else: - msg["error"] = "Correct answer" - return True, msg + + def _incorrect_user_lines(self, exp_lines, user_lines): + err_line_no = [] + for i, (expected_line, user_line) in \ + enumerate(zip_longest(exp_lines, user_lines)): + if not user_line or not expected_line: + err_line_no.append(i) + else: + if user_line.strip() != expected_line.strip(): + err_line_no.append(i) + return err_line_no + + def compare_outputs(self, 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) + if ng != ne: + err_line_no = self._incorrect_user_lines(exp_lines, given_lines) + msg["error_no"] = err_line_no + msg["error"] = "Incorrect Answer: \ + We had expected {0} number of lines. \ + We got {1} number of lines. ".format(ne, ng) + return False, msg + else: + err_line_no = self._incorrect_user_lines(exp_lines, given_lines) + if err_line_no: + msg["error_no"] = err_line_no + msg["error"] = "Incorrect Answer: "\ + "Line number(s) {0} did not match."\ + .format(", ".join(map(str,[x+1 for x in err_line_no]))) + return False, msg + else: + msg["error"] = "Correct answer" + return True, msg -- cgit From 77f05d3df90a70ff97285deb5bda2d91d99e65d5 Mon Sep 17 00:00:00 2001 From: mahesh Date: Mon, 22 May 2017 14:32:47 +0530 Subject: changes variable and dict key names along with other minor changes in answerpaper. --- yaksh/compare_stdio.py | 70 ++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 36 deletions(-) (limited to 'yaksh/compare_stdio.py') diff --git a/yaksh/compare_stdio.py b/yaksh/compare_stdio.py index c9c631f..c4076de 100644 --- a/yaksh/compare_stdio.py +++ b/yaksh/compare_stdio.py @@ -3,43 +3,41 @@ try: except ImportError: from itertools import izip_longest as zip_longest -class CompareOutputs(object): - def _incorrect_user_lines(self, exp_lines, user_lines): - err_line_no = [] - for i, (expected_line, user_line) in \ - enumerate(zip_longest(exp_lines, user_lines)): - if not user_line or not expected_line: - err_line_no.append(i) - else: - if user_line.strip() != expected_line.strip(): - err_line_no.append(i) - return err_line_no +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(self, 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) - if ng != ne: - err_line_no = self._incorrect_user_lines(exp_lines, given_lines) - msg["error_no"] = err_line_no - msg["error"] = "Incorrect Answer: \ - We had expected {0} number of lines. \ - We got {1} number of lines. ".format(ne, ng) +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: - err_line_no = self._incorrect_user_lines(exp_lines, given_lines) - if err_line_no: - msg["error_no"] = err_line_no - msg["error"] = "Incorrect Answer: "\ - "Line number(s) {0} did not match."\ - .format(", ".join(map(str,[x+1 for x in err_line_no]))) - return False, msg - else: - msg["error"] = "Correct answer" - return True, msg + msg["error_msg"] = "Correct Answer" + return True, msg -- cgit