From ead9a8ac6d99b3cd79223e995cb952eb3c477d19 Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 23 Sep 2015 12:28:34 +0200 Subject: Small changes too fahrnheit and pythagorean problems. --- .../introduction/pythagorean_theorem/common.py | 38 ++++++++++++---------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'python/problems/introduction/pythagorean_theorem/common.py') diff --git a/python/problems/introduction/pythagorean_theorem/common.py b/python/problems/introduction/pythagorean_theorem/common.py index 405a39c..193527e 100644 --- a/python/problems/introduction/pythagorean_theorem/common.py +++ b/python/problems/introduction/pythagorean_theorem/common.py @@ -1,6 +1,7 @@ # coding=utf-8 -from python.util import has_token_sequence, string_almost_equal +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc from server.hints import Hint, HintSequence id = 188 @@ -52,47 +53,50 @@ def test(python, code): outputs = [ans[1] for ans in answers] n_correct = 0 - for output, correct in zip(outputs, test_out): + tin = None + for i, (output, correct) in enumerate(zip(outputs, test_out)): if string_almost_equal(output, float(correct)): n_correct += 1 + else: + tin = test_in[i][1] + tout = correct passed = n_correct == len(test_in) hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}] + if tin: + hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}}) return passed, hints def hint(python, code): + tokens = get_tokens(code) + # run one test first to see if there are any exceptions test_in = [(None, '3\n4\n')] answer = python(code=code, inputs=test_in, timeout=1.0) exc = answer[0][3] - # if have an exception! + exc_hint = get_exception_desc(answer[0][3]) if exc: if 'NameError' in exc: return [{'id':'name_error', 'args': {'message': exc}}] - if 'unsupported operand' in exc or 'TypeError' in exc: + elif 'unsupported operand' in exc or 'TypeError' in exc: return [{'id':'unsupported_operand', 'args': {'message': exc}}] - - # show plan if student is lost - if len(code.strip()) < 5 or (not has_token_sequence(code, ['input']) and - (has_token_sequence(code, ['pi']) or - has_token_sequence(code, ['sin']) or - has_token_sequence(code, ['print']))): - return [{'id': 'plan'}] - + else: + return exc_hint # if input is not present in code, student needs to learn about input - if not has_token_sequence(code, ['input']) or \ - not has_token_sequence(code, ['float']): + if not has_token_sequence(tokens, ['input']) or \ + not has_token_sequence(tokens, ['float']) or \ + not has_token_sequence(tokens, ['=']): return [{'id': 'no_input_call'}] # if tokens sqrt or ** are not in code, we have to teach them how to # use math functions. - if (not has_token_sequence(code, ['sqrt']) or - not has_token_sequence(code, ['**'])): + if (not has_token_sequence(tokens, ['sqrt']) or + not has_token_sequence(tokens, ['**'])): return [{'id' : 'math_functions'}] # student is not using print function - if not has_token_sequence(code, ['print']): + if not has_token_sequence(tokens, ['print']): return [{'id' : 'printing'}] return None -- cgit v1.2.1