summaryrefslogtreecommitdiff
path: root/python/problems/functions/greatest_absolutist/common.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/problems/functions/greatest_absolutist/common.py')
-rw-r--r--python/problems/functions/greatest_absolutist/common.py98
1 files changed, 98 insertions, 0 deletions
diff --git a/python/problems/functions/greatest_absolutist/common.py b/python/problems/functions/greatest_absolutist/common.py
new file mode 100644
index 0000000..7871243
--- /dev/null
+++ b/python/problems/functions/greatest_absolutist/common.py
@@ -0,0 +1,98 @@
+# coding=utf-8
+
+import re
+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 = 196
+group = 'functions'
+number = 2
+visible = True
+
+solution = '''\
+def naj_abs(xs):
+ naj = xs[0]
+ for x in xs:
+ if abs(x) > abs(naj):
+ naj = x
+ return naj
+'''
+
+hint_type = {
+ 'no_def': Hint('no_def'),
+ 'no_return': Hint('no_return'),
+ 'return_indent': Hint('return_indent'),
+ 'for_loop': Hint('for_loop'),
+ 'not_int': Hint('not_int'),
+ 'return_first': Hint('return_first'),
+ 'return_last': Hint('return_last'),
+ 'return_greatest': Hint('return_greatest'),
+ 'return_positive': Hint('return_positive'),
+}
+
+
+def test(python, code):
+ test_in = [('naj_abs([6, 4, 2, 0])', None),
+ ('naj_abs([4, 6, 2, 0])', None),
+ ('naj_abs([4, 2, 6, 0])', None),
+ ('naj_abs([4, 2, 0, 6])', None),
+ ('naj_abs([6, -8, 2, 0])', None),
+ ('naj_abs([-8, 6, 2, 0])', None),
+ ('naj_abs([8, -6, -2, 0])', None),
+ ('naj_abs([-5, -6, -2, -1])', None),
+ ('naj_abs([-8, -1, -6, -2])', None),
+ ('naj_abs([-1, -8, -6, -2])', None),
+ ('naj_abs([42])', None),
+ ('naj_abs([-42])', None)]
+
+ test_out = [6, 6, 6, 6, -8, -8, 8, -6, -8, -8, 42, -42]
+
+ answers = python(code=code, inputs=test_in, timeout=1.0)
+ n_correct = sum(ans[0] == to for ans, to in zip(answers, test_out))
+ return n_correct, len(test_in)
+
+def hint(python, code):
+ # run one test first to see if there are any exceptions
+ answer = python(code=code, inputs=[('naj_abs([1, 3, 3, -6, 4, 3, 2])', None)], timeout=1.0)
+ exc = get_exception_desc(answer)
+ if exc: return exc
+
+ tokens = get_tokens(code)
+ print(tokens)
+ # if has no def, tell him to define the function
+ if not has_token_sequence(tokens, ['def']):
+ return [{'id' : 'no_def'}]
+
+ # if has no return, tell him to return the value
+ if not has_token_sequence(tokens, ['return']):
+ return [{'id' : 'no_return'}]
+
+ # if has no loop, tell him to use it
+ if not has_token_sequence(tokens, ['while']) and \
+ not has_token_sequence(tokens, ['for']):
+ return [{'id' : 'for_loop'}]
+
+ # if has no condition if, tell him to use it
+ if not has_token_sequence(tokens, ['if']):
+ return [{'id' : 'if_clause'}]
+
+ # test for wrong answers
+ res = answer[0][0]
+ if not isinstance(res, int):
+ return [{'id' : 'not_int'}]
+ cases = {
+ 1: [{'id' : 'return_first'}],
+ 2: [{'id' : 'return_last'}],
+ 4: [{'id' : 'return_greatest'}],
+ 6: [{'id' : 'return_positive'}]
+ }
+ if res in cases:
+ return cases[res]
+
+ # if has no return at the beginning of block,
+ # tell not to return from for
+ if not has_token_sequence(tokens, ['\n', 'return']):
+ return [{'id' : 'return_indent'}]
+
+ return None