summaryrefslogtreecommitdiff
path: root/prolog/problems/family_relations/cousin_2
diff options
context:
space:
mode:
authorAleksander Sadikov <sasha@fri.uni-lj.si>2015-09-22 17:57:04 +0200
committerAleksander Sadikov <sasha@fri.uni-lj.si>2015-09-22 17:57:04 +0200
commitc50452d81b08d54b734f1da73b736bfbc2997215 (patch)
tree03c64486fb259b97fa7330849990dac76e14c74c /prolog/problems/family_relations/cousin_2
parentcb71b2083b2f87b2f0466577dbe3e11efe22410c (diff)
New problems with hints for famrel section added.
This practically completes famrel section without some details.
Diffstat (limited to 'prolog/problems/family_relations/cousin_2')
-rw-r--r--prolog/problems/family_relations/cousin_2/common.py95
-rw-r--r--prolog/problems/family_relations/cousin_2/sl.py63
2 files changed, 158 insertions, 0 deletions
diff --git a/prolog/problems/family_relations/cousin_2/common.py b/prolog/problems/family_relations/cousin_2/common.py
index 7cd6392..d95babd 100644
--- a/prolog/problems/family_relations/cousin_2/common.py
+++ b/prolog/problems/family_relations/cousin_2/common.py
@@ -26,3 +26,98 @@ cousin(X, Y) :-
;
sister99(PX, PY) ).
'''
+
+hint_type = {
+ 'gender_is_irrelevant': Hint('gender_is_irrelevant'),
+ 'precedence_fail': Hint('precedence_fail'),
+ 'cousin_vs_sibling': Hint('cousin_vs_sibling'),
+ 'cousin_to_oneself': Hint('cousin_to_oneself'),
+ 'cousin_need_not_be_parent': Hint('cousin_need_not_be_parent'),
+}
+
+test_cases = [
+ # TODO
+]
+
+def test(program, solved_problems):
+ code = (program + '\n' +
+ server.problems.solutions_for_problems('prolog', solved_problems) + '\n' +
+ server.problems.load_facts('prolog', facts).facts)
+
+ engine_id = None
+ try:
+ engine_id, output = prolog.engine.create(code=code, timeout=1.0)
+ if not engine_id or 'error' in map(itemgetter(0), output):
+ # Engine creation failed, or syntax error in code.
+ return 0, len(test_cases)
+
+ n_correct = 0
+ for query, answers in test_cases:
+ # Limit inferences for each solution to curb unbounded recursion.
+ limited = 'call_with_inference_limit(({}), 100000, _)'.format(query)
+ if prolog.engine.check_answers(engine_id, query=limited, answers=answers, timeout=1.0):
+ n_correct += 1
+ finally:
+ if engine_id:
+ prolog.engine.destroy(engine_id)
+
+ passed = n_correct == len(test_cases)
+ hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}]
+ return passed, hints
+
+def hint(program, solved_problems):
+ tokens = prolog.util.tokenize(program)
+
+ code = (program + '\n' +
+ server.problems.solutions_for_problems('prolog', solved_problems) + '\n' +
+ server.problems.load_facts('prolog', facts).facts)
+
+ engine_id = None
+ try:
+ engine_id, output = prolog.engine.create(code=code, timeout=1.0)
+ if not engine_id:
+ raise Exception('Prolog engine failed to create.')
+
+ # gender testing is redundant
+ # this is not necessarily wrong, but worth mentioning anyway
+ targets = [prolog.util.Token('NAME', 'male'), prolog.util.Token('NAME', 'female')]
+ pos = [(t.pos, t.pos + len(t.val)) for t in tokens if t in targets]
+ if pos:
+ # TODO: resolve how highlighting info is communicated
+ return [{'id': 'gender_is_irrelevant', 'highlight': pos}]
+
+ # precedence fail (AND block vs OR block)
+ # case in point: parent(PX, X), parent(PY, Y), brother(PX, PY) ; sister(PX, PY)
+ # or this case: parent(PX, X), parent(PY, Y), sister(PX, PY) ; brother(PX, PY)
+ # warning: knowledge base dependent
+ if prolog.util.Token('SEMI', ';') in tokens and prolog.engine.ask_truth(engine_id,
+ 'findall(_, cousin(X, Y), L), (length(L, 14) ; length(L, 16))'):
+ return [{'id', 'precedence_fail'}]
+
+ # cousin should not be brother or sister
+ # common parent, probably solved via grandparent
+ if prolog.engine.ask_truth(engine_id,
+ 'cousin(X, Y), (brother(X, Y) ; sister(X, Y))'):
+ return [{'id', 'cousin_vs_sibling'}]
+
+ # cousin to him/herself
+ # just common grandparent is not enough
+ if prolog.engine.ask_truth(engine_id, 'cousin(X, X)'):
+ return [{'id', 'cousin_to_oneself'}]
+
+
+ # X (or Y) does not necessarily need to be a parent
+ # cousin is a symmetrical relation, so X and Y are covered by this
+ if prolog.engine.ask_one(engine_id,
+ 'cousin(X, _), \+ parent(X, _)') == 'false':
+ return [{'id': 'cousin_need_not_be_parent'}]
+
+ except socket.timeout as ex:
+ pass
+
+ finally:
+ if engine_id:
+ prolog.engine.destroy(engine_id)
+
+ return None
+
diff --git a/prolog/problems/family_relations/cousin_2/sl.py b/prolog/problems/family_relations/cousin_2/sl.py
new file mode 100644
index 0000000..ebecf8d
--- /dev/null
+++ b/prolog/problems/family_relations/cousin_2/sl.py
@@ -0,0 +1,63 @@
+# coding=utf-8
+
+id = 99
+name = 'cousin/2'
+slug = 'X je bratranec ali sestrična od Y'
+
+description = '''\
+<p><code>cousin(X, Y)</code>: <code>X</code> je bratranec ali sestrična od <code>Y</code>.</p>
+<pre>
+ ?- cousin(andrew, X).
+ X = vanessa ;
+ X = patricia.
+</pre>'''
+
+plan = ['''\
+<p>Verjetno bi se sedaj splačalo uporabiti rešitev kakšne prejšnje naloge?</p>
+<p><img src="Cousin-1.png" /></p>
+<p>Loni naj zbriše imena relacij (parent, sister) s te slike!</p>
+<p>Seveda se da rešiti tudi brez prejšnjih rešitev... MORE</p>
+<p><img src="Cousin-3.png" /></p>
+<p>Loni naj zbriše imena relacij (parent, sister) s te slike!</p>
+''', '''\
+<p><img src="Cousin-1.png" /></p>
+''', '''\
+<p>Če je <code>PX</code> starš od <code>X</code> in je
+<code>PY</code> starš od <code>Y</code> ter sta
+<code>PX</code> in <code>PY</code> brat ali sestra,
+potem je <code>X</code> bratranec/sestrična od <code>Y</code>.</p>
+''']
+
+hint = {
+ 'gender_is_irrelevant': '''\
+<p>Je spol res pomemben?</p>
+''',
+
+ 'precedence_fail': '''\
+<p>Si morda narobe upošteval prioriteto operatorjev IN ter ALI?</p>
+<p>Operator IN veže močneje od ALI, če želiš spremeniti prioriteto,
+lahko uporabiš oklepaje.</p>
+''',
+
+ 'cousin_vs_sibling': '''\
+<p>Kako je lahko bratranec/sestrična hkrati tudi brat/sestra od <code>Y</code>?
+Si morda pozabil, da starša od <code>X</code> in <code>Y</code> ne smeta biti ista oseba?</p>
+<p><img src="Cousin-4.png" /></p>
+''',
+
+ 'cousin_to_oneself': '''\
+<p>Kako je lahko nekdo bratranec/sestrična samemu sebi?
+Imata morda <code>X</code> in <code>Y</code> istega starša?</p>
+<p><img src="Cousin-2.png" /></p>
+''',
+
+ 'cousin_need_not_be_parent': '''\
+<p>Bratranec/sestrična pravzaprav ne rabi imeti otrok...</p>
+''',
+
+# 'interesting_tidbit': '''\
+# <p>Zanimivost: nalogo bi lahko rešil tudi z uporabo rešitve za relacijo "sister".
+# Teta je namreč sestra od starša od <code>Y</code>.</p>
+# ''',
+}
+