summaryrefslogtreecommitdiff
path: root/prolog/problems/sorting
diff options
context:
space:
mode:
authorTimotej Lazar <timotej.lazar@fri.uni-lj.si>2016-03-29 19:19:13 +0200
committerTimotej Lazar <timotej.lazar@fri.uni-lj.si>2016-03-29 19:19:13 +0200
commitb795582bb9920ed20638ded3a13110ab9622ac89 (patch)
tree4426e4f257159e192a15d1e4a0adfb1dfee59987 /prolog/problems/sorting
parent0d32abe688f0636988090f653f75bc33616ad610 (diff)
Prolog: add triggers for slowest_sort_ever/2 hints
Diffstat (limited to 'prolog/problems/sorting')
-rw-r--r--prolog/problems/sorting/slowest_sort_ever_2/common.py51
-rw-r--r--prolog/problems/sorting/slowest_sort_ever_2/sl.py34
2 files changed, 79 insertions, 6 deletions
diff --git a/prolog/problems/sorting/slowest_sort_ever_2/common.py b/prolog/problems/sorting/slowest_sort_ever_2/common.py
index 38f2d1d..1689f4a 100644
--- a/prolog/problems/sorting/slowest_sort_ever_2/common.py
+++ b/prolog/problems/sorting/slowest_sort_ever_2/common.py
@@ -1,11 +1,11 @@
-# coding=utf-8
-
from operator import itemgetter
+import socket
+
import prolog.engine
-import server.problems
+from server.hints import Hint, HintPopup
id = 126
-number = 33
+number = 20
visible = False
facts = None
@@ -29,6 +29,15 @@ slowest_sort_ever(L, S) :-
is_sorted126(S).
'''
+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'),
+ 'timeout': Hint('timeout'),
+ 'no_permute': Hint('no_permute'),
+ 'no_isSorted': Hint('no_isSorted'),
+}
+
test_cases = [
('slowest_sort_ever([], X)',
[{'X': '[]'}]),
@@ -60,5 +69,37 @@ 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'}]
+
+ # does not call permute
+ if not any(t.val == 'permute' for t in tokens):
+ return [{'id': 'no_permute'}]
+
+ # calls permute but not is_sorted
+ if (any(t.val == 'permute' for t in tokens) and
+ not any(t.val == 'is_sorted' for t in tokens)):
+ return [{'id': 'no_isSorted'}]
+
+ # target predicate seems to always be false
+ if not prolog.engine.ask_truthTO(engine_id, 'slowest_sort_ever(_, _)'):
+ return [{'id': 'predicate_always_false'}]
+
+ except socket.timeout as ex:
+ return [{'id': 'timeout'}]
+
+ finally:
+ if engine_id:
+ prolog.engine.destroy(engine_id)
+
return []
diff --git a/prolog/problems/sorting/slowest_sort_ever_2/sl.py b/prolog/problems/sorting/slowest_sort_ever_2/sl.py
index d6cf66b..03a77b1 100644
--- a/prolog/problems/sorting/slowest_sort_ever_2/sl.py
+++ b/prolog/problems/sorting/slowest_sort_ever_2/sl.py
@@ -10,4 +10,36 @@ description = '''\
L = [1,2,3,4,5].
</pre>'''
-hint = {}
+hint = {
+ 'eq_instead_of_equ': '''\
+<p>Operator <code>==</code> je strožji od operatorja <code>=</code> v smislu, da je za slednjega dovolj,
+da elementa lahko naredi enaka (unifikacija).</p>
+<p>Seveda pa lahko nalogo rešiš brez obeh omenjenih operatorjev, spomni se, da lahko unifikacijo narediš
+implicitno že kar v argumentih predikata (glavi stavka).</p>
+''',
+
+ 'eq_instead_of_equ_markup': '''\
+<p>Morda bi bil bolj primeren operator za unifikacijo (<code>=</code>)?</p>
+''',
+
+ 'predicate_always_false': '''\
+<p>Vse kaže, da tvoj predikat vedno vrne <code>false</code>. Si mu dal pravilno ime, si se morda pri imenu zatipkal?</p>
+<p>Če je ime pravilno, se morda splača preveriti tudi, če se nisi zatipkal kje drugje,
+je morda kakšna pika namesto vejice ali obratno, morda kakšna spremenljivka z malo začetnico?</p>
+<p>Možno je seveda tudi, da so tvoji pogoji prestrogi ali celo nemogoči (kot bi bila npr. zahteva,
+da je <code>X</code> hkrati starš in sestra od <code>Y</code> ali kaj podobno zlobnega).</p>
+''',
+
+ 'timeout': '''\
+<p>Je morda na delu potencialno neskončna rekurzija? Kako se bo ustavila?</p>
+<p>Morda pa je kriv tudi manjkajoč, neustrezen ali preprosto nekompatibilen (s splošnim primerom) robni pogoj?</p>
+''',
+
+ 'no_permute': '''\
+<p>no_permute</p>
+''',
+
+ 'no_isSorted': '''\
+<p>no_isSorted</p>
+''',
+}