diff options
Diffstat (limited to 'prolog/problems/lists_advanced/sum_2')
-rw-r--r-- | prolog/problems/lists_advanced/sum_2/common.py | 49 | ||||
-rw-r--r-- | prolog/problems/lists_advanced/sum_2/en.py | 13 | ||||
-rw-r--r-- | prolog/problems/lists_advanced/sum_2/sl.py | 13 |
3 files changed, 75 insertions, 0 deletions
diff --git a/prolog/problems/lists_advanced/sum_2/common.py b/prolog/problems/lists_advanced/sum_2/common.py new file mode 100644 index 0000000..000b4b0 --- /dev/null +++ b/prolog/problems/lists_advanced/sum_2/common.py @@ -0,0 +1,49 @@ +# coding=utf-8 + +from operator import itemgetter +import prolog.engine +import server.problems + +id = 118 +number = 25 +visible = False +facts = None + +solution = '''\ +sum([], 0). +sum([H|T], Sum) :- + sum(T, SumT), + Sum is SumT + H. +''' + +test_cases = [ + ('sum([], X)', + [{'X': '0'}]), + ('sum([8, 3, 5, 1], X)', + [{'X': '17'}]), + ('sum([2, -3, 5, 0], X)', + [{'X': '4'}]), +] + +def test(code, aux_code): + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code+aux_code, timeout=1.0) + if engine_id is not None and 'error' not in map(itemgetter(0), output): + # Engine successfully created, and no syntax error in program. + for query, answers in test_cases: + if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0): + n_correct += 1 + except socket.timeout: + pass + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return n_correct, len(test_cases), hints + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/lists_advanced/sum_2/en.py b/prolog/problems/lists_advanced/sum_2/en.py new file mode 100644 index 0000000..b4dab45 --- /dev/null +++ b/prolog/problems/lists_advanced/sum_2/en.py @@ -0,0 +1,13 @@ +# coding=utf-8 + +name = 'sum/2' +slug = 'find the sum of all elements in list' + +description = '''\ +<p><code>sum(L, Sum)</code>: <code>Sum</code> is the sum of all elements in the list <code>L</code>.</p> +<pre> +?- sum([1,2,3], Sum). + Sum = 6. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/sum_2/sl.py b/prolog/problems/lists_advanced/sum_2/sl.py new file mode 100644 index 0000000..cc0423b --- /dev/null +++ b/prolog/problems/lists_advanced/sum_2/sl.py @@ -0,0 +1,13 @@ +# coding=utf-8 + +name = 'sum/2' +slug = 'Izračunaj vsoto elementov v seznamu' + +description = '''\ +<p><code>sum(L, Sum)</code>: <code>Sum</code> je vsota vseh elementov v seznamu <code>L</code>.</p> +<pre> +?- sum([1,2,3], Sum). + Sum = 6. +</pre>''' + +hint = {} |