summaryrefslogtreecommitdiff
path: root/python/problems/introduction/ballistics/common.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/problems/introduction/ballistics/common.py')
-rw-r--r--python/problems/introduction/ballistics/common.py61
1 files changed, 25 insertions, 36 deletions
diff --git a/python/problems/introduction/ballistics/common.py b/python/problems/introduction/ballistics/common.py
index f7f271e..10ef692 100644
--- a/python/problems/introduction/ballistics/common.py
+++ b/python/problems/introduction/ballistics/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 = 187
group = 'introduction'
-number = 2
+number = 3
visible = True
solution = '''\
@@ -21,34 +22,19 @@ print("Kroglo bo odneslo", razdalja, "metrov.")
hint_type = {
'plan': Hint('plan'),
'eval_expression': Hint('eval_expression'),
- 'sin_error': HintSequence('sin_error', 4),
- 'name_error': HintSequence('name_error', 4),
- 'unsupported_operand': HintSequence('unsupported_operand', 4),
- 'error': HintSequence('error', 2),
- 'radians': HintSequence('radians', 3),
+ 'sin_error': Hint('sin_error'),
+ 'name_error': Hint('name_error'),
+ 'unsupported_operand': Hint('unsupported_operand'),
+ 'error': Hint('error'),
+ 'radians': Hint('radians'),
'printing': Hint('printing'),
'betterg': Hint('betterg'),
}
-import re
-numeric_const_pattern = r"""
- [-+]? # optional sign
- (?:
- (?: \d* \. \d+ ) # .1 .12 .123 etc 9.1 etc 98.1 etc
- |
- (?: \d+ \.? ) # 1. 12. 123. etc 1 12 123 etc
- )
- # followed by optional exponent part if desired
- (?: [Ee] [+-]? \d+ ) ?
- """
-rx = re.compile(numeric_const_pattern, re.VERBOSE)
-
def contains_negative(s):
- """ Returns whether the string contains negative value or not.
- """
- vals = rx.findall(s)
- for v in vals:
- if float(v) < 0:
+ nums = get_numbers(s)
+ for n in nums:
+ if n < 0:
return True
return False
@@ -89,20 +75,29 @@ 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)):
+ print (output, float(correct))
if string_almost_equal(output, float(correct)):
n_correct += 1
+ else:
+ tin = test_in[i][1].replace('\n', ' ')
+ 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, '5\n10\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 'sin' in exc and 'NameError' in exc:
return [{'id':'sin_error', 'args': {'message': exc}}]
@@ -113,21 +108,15 @@ def hint(python, code):
elif 'TypeError' in exc:
return [{'id':'type_error', 'args': {'message': exc}}]
else:
- return [{'id':'error', 'args': {'message': exc}}]
-
- # show plan if student is lost
- # a) empty progam
- # b) there is not input (we can do it here, since we have no input hint)
- if not code or (not has_token_sequence(code, ['input'])):
- return [{'id': 'plan'}]
+ return exc_hint
# if sinus is not in code, we need to teach students where they can get it
# use math functions.
- if (not has_token_sequence(code, ['sin'])):
+ if (not has_token_sequence(tokens, ['sin'])):
return [{'id' : 'eval_expression'}]
# student is not using print function
- if not has_token_sequence(code, ['print']):
+ if not has_token_sequence(tokens, ['print']):
return [{'id' : 'printing'}]
# if result is negative, student did not translate to radians