summaryrefslogtreecommitdiff
path: root/prolog/problems/lists/del_3/common.py
diff options
context:
space:
mode:
authorAleksander Sadikov <aleksander.sadikov@fri.uni-lj.si>2016-03-09 19:16:26 +0100
committerAleksander Sadikov <aleksander.sadikov@fri.uni-lj.si>2016-03-09 19:16:26 +0100
commit79a91c6a6c4fabf6f3c8949a6735375ceac21989 (patch)
treed6b0447a89fe6354e2945f2073a5485a16ca2c62 /prolog/problems/lists/del_3/common.py
parentc61d06eab2c15928a928fcea106f276013902bb0 (diff)
Hints for del/3 added. And some minor corrections/additions elsewhere.
Diffstat (limited to 'prolog/problems/lists/del_3/common.py')
-rw-r--r--prolog/problems/lists/del_3/common.py64
1 files changed, 62 insertions, 2 deletions
diff --git a/prolog/problems/lists/del_3/common.py b/prolog/problems/lists/del_3/common.py
index 23caf08..f3dfcb2 100644
--- a/prolog/problems/lists/del_3/common.py
+++ b/prolog/problems/lists/del_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 = 105
number = 11
-visible = False
+visible = True
facts = None
solution = '''\
@@ -15,6 +18,17 @@ del(X, [Y|T], [Y|L]) :-
del(X, T, L).
'''
+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'),
+ 'final_hint': Hint('final_hint'),
+ 'del_from_empty_list_success': Hint('del_from_empty_list_success'),
+ 'lost_heads': Hint('lost_heads'),
+}
+
test_cases = [
('del(1, [1], X)',
[{'X': '[]'}]),
@@ -41,8 +55,54 @@ def test(code, aux_code):
prolog.engine.destroy(engine_id)
hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}]
+ if n_correct == len(test_cases):
+ tokens = prolog.util.tokenize(code)
+ if prolog.util.Token('NAME', 'insert') not in tokens:
+ hints += [{'id': 'final_hint'}]
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_truthTO(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'}]
+
+ # 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)
+
+
+ except socket.timeout as ex:
+ return [{'id': 'timeout'}]
+
+ finally:
+ if engine_id:
+ prolog.engine.destroy(engine_id)
+
return []