summaryrefslogtreecommitdiff
path: root/prolog/problems/lists
diff options
context:
space:
mode:
authorAleksander Sadikov <aleksander.sadikov@fri.uni-lj.si>2016-03-09 23:03:12 +0100
committerAleksander Sadikov <aleksander.sadikov@fri.uni-lj.si>2016-03-09 23:03:12 +0100
commit8ef87d4610b575c9b1e7e31751663ab7933eefeb (patch)
tree4a9a9d885a10f8ac3bc59e46ddbe9e3f650260d2 /prolog/problems/lists
parentdbbca6aaf0fa33c6ae152cc361a05522776f5a72 (diff)
Reordering the exercises in list chapter #1.
Diffstat (limited to 'prolog/problems/lists')
-rw-r--r--prolog/problems/lists/conc_3/common.py7
-rw-r--r--prolog/problems/lists/del_3/common.py2
-rw-r--r--prolog/problems/lists/dup_2/common.py4
-rw-r--r--prolog/problems/lists/insert_3/common.py76
-rw-r--r--prolog/problems/lists/memb_2/common.py2
5 files changed, 82 insertions, 9 deletions
diff --git a/prolog/problems/lists/conc_3/common.py b/prolog/problems/lists/conc_3/common.py
index 0c5312f..67f9481 100644
--- a/prolog/problems/lists/conc_3/common.py
+++ b/prolog/problems/lists/conc_3/common.py
@@ -2,11 +2,14 @@
from operator import itemgetter
import prolog.engine
+import prolog.util
+import socket
+from server.hints import Hint, HintPopup
import server.problems
id = 104
-number = 12
-visible = False
+number = 5
+visible = True
facts = None
solution = '''\
diff --git a/prolog/problems/lists/del_3/common.py b/prolog/problems/lists/del_3/common.py
index deadbf8..670ea1e 100644
--- a/prolog/problems/lists/del_3/common.py
+++ b/prolog/problems/lists/del_3/common.py
@@ -8,7 +8,7 @@ from server.hints import Hint, HintPopup
import server.problems
id = 105
-number = 11
+number = 3
visible = True
facts = None
diff --git a/prolog/problems/lists/dup_2/common.py b/prolog/problems/lists/dup_2/common.py
index 4153533..d4ebffe 100644
--- a/prolog/problems/lists/dup_2/common.py
+++ b/prolog/problems/lists/dup_2/common.py
@@ -5,8 +5,8 @@ import prolog.engine
import server.problems
id = 110
-number = 17
-visible = False
+number = 4
+visible = True
facts = None
solution = '''\
diff --git a/prolog/problems/lists/insert_3/common.py b/prolog/problems/lists/insert_3/common.py
index be4da78..5b049f7 100644
--- a/prolog/problems/lists/insert_3/common.py
+++ b/prolog/problems/lists/insert_3/common.py
@@ -2,11 +2,14 @@
from operator import itemgetter
import prolog.engine
+import prolog.util
+import socket
+from server.hints import Hint, HintPopup
import server.problems
id = 106
-number = 13
-visible = False
+number = 1
+visible = True
facts = None
solution = '''\
@@ -17,6 +20,18 @@ insert(X, List, BigList) :-
del106(X, BigList, List).
'''
+hint_type = {
+ 'eq_instead_of_equ_markup': HintPopup('eq_instead_of_equ_markup'),
+ 'eq_instead_of_equ': Hint('eq_instead_of_equ'),
+ 'predicate_always_false': Hint('predicate_always_false'),
+ 'base_case': Hint('base_case'),
+ 'timeout': Hint('timeout'),
+ 'del_from_empty_list_success': Hint('del_from_empty_list_success'),
+ 'lost_heads': Hint('lost_heads'),
+ 'recursive_case': Hint('recursive_case'),
+ 'leading_heads_all_x': Hint('leading_heads_all_x'),
+}
+
test_cases = [
('insert(l, [], X)',
[{'X': '[l]'}]),
@@ -46,5 +61,60 @@ def test(code, aux_code):
return n_correct, len(test_cases), hints
def hint(code, aux_code):
- # TODO
+ tokens = prolog.util.tokenize(code)
+
+ try:
+ engine_id, output = prolog.engine.create(code=code+aux_code, timeout=1.0)
+
+ # strict equality testing instead of simple matching
+ # this is usually (but not necessarily) wrong
+ targets = [prolog.util.Token('EQ', '==')]
+ marks = [(t.pos, t.pos + len(t.val)) for t in tokens if t in targets]
+ if marks:
+ return [{'id': 'eq_instead_of_equ_markup', 'start': m[0], 'end': m[1]} for m in marks] + \
+ [{'id': 'eq_instead_of_equ'}]
+
+ # recursion is getting bigger and bigger
+
+
+ # deleting from empty list succeeds with an emtpy list result
+ if prolog.engine.ask_truthTO(engine_id, 'del(_, [], _)'):
+ return [{'id': 'del_from_empty_list_success'}]
+
+ # missing/failed base case
+ if not prolog.engine.ask_one(engine_id, 'del(qQ, [qQ,qa,qb,qc], [qa,qb,qc])'):
+ return [{'id': 'base_case'}]
+
+ # target predicate seems to always be false
+ if not prolog.engine.ask_truth(engine_id, 'del(_, [_,_,_,_,_,_], _)'):
+ return [{'id': 'predicate_always_false'}]
+
+ # forgot to put list head(s) back in the list when deleting an element
+ if prolog.engine.ask_truth(engine_id, 'del(q, [a,b,c,d,q,x,y,z], [x,y,z])'):
+ return [{'id': 'lost_heads'}]
+
+ # used [X|T] instead of more general [H|T] in recursive case
+ if prolog.engine.ask_truth(engine_id, \
+ 'findall(NL, del(q, [q,q,q,a,b,c], NL), [[q,q,a,b,c],[q,q,a,b,c],[q,q,a,b,c]]).') and \
+ not prolog.engine.ask_truth(engine_id, 'del(q, [a,b,c,q,x,y,z], [a,b,c,x,y,z])'):
+ return [{'id': 'leading_heads_all_x'}]
+
+ # L=[H|T] at the end of the rule; this can be detected with test cases and can be wrong (in some sense)
+ # TODO: implement it, and provide a good explanation (might be hard)
+
+ # base case works, the recursive doesn't (but it doesn't timeout)
+ # this may be left as the last, most generic hint
+ if not prolog.engine.ask_truth(engine_id, 'del(qQ, [qa,qb,qQ,qc], [qa,qb,qc])'):
+ return [{'id': 'recursive_case'}]
+
+
+ except socket.timeout as ex:
+ return [{'id': 'timeout'}]
+
+ finally:
+ if engine_id:
+ prolog.engine.destroy(engine_id)
+
return []
+
+# https://xkcd.com/323/ \ No newline at end of file
diff --git a/prolog/problems/lists/memb_2/common.py b/prolog/problems/lists/memb_2/common.py
index 45e070a..c45168d 100644
--- a/prolog/problems/lists/memb_2/common.py
+++ b/prolog/problems/lists/memb_2/common.py
@@ -8,7 +8,7 @@ from server.hints import Hint, HintPopup
import server.problems
id = 103
-number = 10
+number = 2
visible = True
facts = None