From 898789199e6af91dfa900650c22df6d26f7e635f Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 24 Sep 2015 12:39:32 +0200 Subject: Finished the first version of introduction section. --- .../problems/introduction/fast_fingers_2/common.py | 85 ++++++++++++++++------ 1 file changed, 64 insertions(+), 21 deletions(-) (limited to 'python/problems/introduction/fast_fingers_2/common.py') diff --git a/python/problems/introduction/fast_fingers_2/common.py b/python/problems/introduction/fast_fingers_2/common.py index d562f7b..958940d 100644 --- a/python/problems/introduction/fast_fingers_2/common.py +++ b/python/problems/introduction/fast_fingers_2/common.py @@ -1,11 +1,12 @@ # 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 = 191 group = 'introduction' -number = 2 +number = 6 visible = True solution = '''\ @@ -17,31 +18,78 @@ t = time.time() print('Koliko je ', x, ' krat ', y, '? ') z = float(input()) if x * y == z: - print('Odgovor je pravilen.') + print(True) else: - print('Odgovor ni pravilen.') + print(False) print('Za razmišljanje ste porabili', time.time() ­ t, 's.') ''' +random_code = '''\ +import random +random.randint = lambda x, y: {} +''' + hint_type = { - 'plan': HintSequence('plan', 2), 'random': Hint('random'), - 'name_error': HintSequence('name_error', 4), - 'type_error': HintSequence('type_error', 4), - 'error': HintSequence('error', 2), - 'if_clause': HintSequence('if_clause', 2), + 'name_error': Hint('name_error'), + 'type_error': Hint('type_error'), + 'error': Hint('error'), + 'if_clause': Hint('if_clause'), + 'final_hint': Hint('final_hint') } def test(python, code): - passed = True - hints = [{'id': 'test_results', 'args': {'passed': 0, 'total': 0}}] + # List of inputs: (expression to eval, stdin). + test = [(5,'25\n'), + (6, '29\n'), + (1, '1\n'), + (7, '65\n'), + (9, '81\n')] + + test_out = [ + True, + False, + True, + False, + True + ] + + # List of outputs: (expression result, stdout, stderr, exception). + + n_correct = 0 + tin = None + for ti, t in enumerate(test): + # change code, so that it replaces random values + # hook randint + tcode = random_code.format(t[0]) + code + + answers = python(code=tcode, inputs=[(None, t[1])], timeout=1.0) + output = answers[0][1] + print ("out", output) + print (answers) + + if str(test_out[ti]) in output and str(not test_out[ti]) not in output: + n_correct += 1 + else: + tin = '{t[0]}*{t[0]}={t[1]}'.format(t=t) + tout = test_out[ti] + + passed = n_correct == len(test) + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test)}}] + if tin: + hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}}) + else: + hints.append({'id': 'final_hint'}) 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, '5\n')] + test_in = [(None, '16\n')] answer = python(code=code, inputs=test_in, timeout=1.0) exc = answer[0][3] + exc_hint = get_exception_desc(answer[0][3]) # if have an exception! if exc: if 'NameError' in exc: @@ -49,21 +97,16 @@ def hint(python, code): elif 'TypeError' in exc: return [{'id':'type_error', 'args': {'message': exc}}] else: - return [{'id':'error', 'args': {'message': exc}}] + return exc_hint # First: if student does not import random, tell him about that module - if not has_token_sequence(code, ['random']) or \ - not has_token_sequence(code, ['randint']): + if not has_token_sequence(tokens, ['random']) or \ + not has_token_sequence(tokens, ['randint']): return [{'id': 'random'}] - # show plan if student is lost - # a) empty progam or - # b) there is not input (we can do it here, since we have no input hint) - if len(code.strip()) < 5 or (not has_token_sequence(code, ['input'])): - return [{'id': 'plan'}] # Student will have to test whether result is correct or not - if not has_token_sequence(code, ['if']): + if not has_token_sequence(tokens, ['if']): return [{'id' : 'if_clause'}] return None -- cgit v1.2.1