From 3146be47a651c357f2a221c39f558d43255cc25c Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Fri, 11 Mar 2016 14:56:55 +0100 Subject: Prolog: add problem lists/last_elem_2 --- prolog/problems/lists/last_elem_2/common.py | 46 +++++++++++++++++++++++++++++ prolog/problems/lists/last_elem_2/en.py | 13 ++++++++ prolog/problems/lists/last_elem_2/sl.py | 21 +++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 prolog/problems/lists/last_elem_2/common.py create mode 100644 prolog/problems/lists/last_elem_2/en.py create mode 100644 prolog/problems/lists/last_elem_2/sl.py (limited to 'prolog/problems/lists/last_elem_2') diff --git a/prolog/problems/lists/last_elem_2/common.py b/prolog/problems/lists/last_elem_2/common.py new file mode 100644 index 0000000..b18243d --- /dev/null +++ b/prolog/problems/lists/last_elem_2/common.py @@ -0,0 +1,46 @@ +from operator import itemgetter +import prolog.engine +import prolog.util +import socket +from server.hints import Hint, HintPopup +import server.problems + +id = 10000 +number = 2 +visible = True +facts = None + +solution = '''\ +''' + +hint_type = { +} + +test_cases = [ + ('last_elem([X], c)', + [{'X': 'c'}]), + ('last_elem([y, l, r, g, a], X)', + [{'X': 'a'}]), +] + +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): + return [] diff --git a/prolog/problems/lists/last_elem_2/en.py b/prolog/problems/lists/last_elem_2/en.py new file mode 100644 index 0000000..93c7c05 --- /dev/null +++ b/prolog/problems/lists/last_elem_2/en.py @@ -0,0 +1,13 @@ +name = 'last_elem/2' +slug = 'find last element in list' + +description = '''\ +

last_elem(L, E): E is the last element of list L.

+
+?- last_elem([1,2,3], X).
+  X = 3.
+?- last_elem([3,2,X], 1).
+  X = 1.
+
''' + +hint = {} diff --git a/prolog/problems/lists/last_elem_2/sl.py b/prolog/problems/lists/last_elem_2/sl.py new file mode 100644 index 0000000..c77ef57 --- /dev/null +++ b/prolog/problems/lists/last_elem_2/sl.py @@ -0,0 +1,21 @@ +name = 'last_elem/2' +slug = 'Vrni zadnji element seznama' + +description = '''\ +

last_elem(L, E): E je zadnji element seznama L.

+
+?- last_elem([1,2,3], X).
+  X = 3.
+?- last_elem([3,2,X], 1).
+  X = 1.
+
+''' + +plan = [] + +hint = { + 'timeout': '''\ +

Je morda na delu potencialno neskončna rekurzija? Kako se bo ustavila?

+

Morda pa je kriv tudi manjkajoč, neustrezen ali preprosto nekompatibilen (s splošnim primerom) robni pogoj?

+''', +} -- cgit v1.2.1