summaryrefslogtreecommitdiff
path: root/prolog/problems/lists/insert_3
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/insert_3
parentdbbca6aaf0fa33c6ae152cc361a05522776f5a72 (diff)
Reordering the exercises in list chapter #1.
Diffstat (limited to 'prolog/problems/lists/insert_3')
-rw-r--r--prolog/problems/lists/insert_3/common.py76
1 files changed, 73 insertions, 3 deletions
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