summaryrefslogtreecommitdiff
path: root/prolog/problems/family_relations/aunt_2
diff options
context:
space:
mode:
authorMartin <martin@leo.fri1.uni-lj.si>2015-09-23 10:21:51 +0200
committerMartin <martin@leo.fri1.uni-lj.si>2015-09-23 10:21:51 +0200
commitbb366684ada98e140b1911c54d6c40fd911904cf (patch)
treee3b4f50ffd033d179c0c38d290ce33b7f99c800a /prolog/problems/family_relations/aunt_2
parente0a5dd801c8382848b28503a81c141d0d15f5833 (diff)
parent76639241146ac5655ae04ea773ada420adb15295 (diff)
Merge branch 'master' of 192.168.15.97:codeq-problems
Diffstat (limited to 'prolog/problems/family_relations/aunt_2')
-rw-r--r--prolog/problems/family_relations/aunt_2/common.py100
-rw-r--r--prolog/problems/family_relations/aunt_2/sl.py60
2 files changed, 160 insertions, 0 deletions
diff --git a/prolog/problems/family_relations/aunt_2/common.py b/prolog/problems/family_relations/aunt_2/common.py
index f0af2dc..ed291fc 100644
--- a/prolog/problems/family_relations/aunt_2/common.py
+++ b/prolog/problems/family_relations/aunt_2/common.py
@@ -17,3 +17,103 @@ aunt(X, Y) :-
sister98(X, Z),
parent(Z, Y).
'''
+
+hint_type = {
+ 'x_and_y_mixed_up': Hint('x_and_y_mixed_up'),
+ 'precedence_fail': Hint('precedence_fail'),
+ 'x_must_be_female': Hint('x_must_be_female'),
+ 'aunt_vs_mother': Hint('aunt_vs_mother'),
+ 'x_need_not_be_parent': Hint('x_need_not_be_parent'),
+ 'y_need_not_be_parent': Hint('y_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.')
+
+ # X and Y mixed up
+ # warning: knowledge base dependent
+ # independent: match on findall(X/Y, (sister(Y, P), parent(P, X)), L)
+ if prolog.engine.ask_truth(engine_id,
+ 'findall(X/Y, aunt(X, Y), \
+ [vanessa/sally, patricia/sally, joanne/melanie, john/vanessa, susan/patricia])'):
+ return [{'id', 'x_and_y_mixed_up'}]
+
+ # precedence fail (AND block vs OR block)
+ # case in point: female(X), parent(P, Y), brother(P, X) ; sister(P, X)
+ # warning: knowledge base dependent
+ if prolog.util.Token('SEMI', ';') in tokens and prolog.engine.ask_truth(engine_id,
+ 'findall(_, aunt(X, Y), L), length(L, 15)'):
+ return [{'id', 'precedence_fail'}]
+
+ # X must be female
+ if prolog.engine.ask_truth(engine_id,
+ 'aunt(X, _), male(X)'):
+ return [{'id', 'x_must_be_female'}]
+
+ # X and P can be the same person
+ # this can occur if the problem is not solved using sister/2
+ if prolog.engine.ask_truth(engine_id,
+ 'aunt(X, Y), mother(X, Y)'):
+ return [{'id', 'aunt_vs_mother'}]
+
+ # X does not necessarily need to be a parent
+ # perhaps risky as only one aunt that is not a parent exists in DB (melanie)
+ if prolog.engine.ask_one(engine_id,
+ 'aunt(X, _), \+ parent(X, _)') == 'false':
+ return [{'id': 'x_need_not_be_parent'}]
+
+ # Y does not necessarily need to be a parent
+ # perhaps risky as only one such nephew exists in DB (susan)
+ if prolog.engine.ask_one(engine_id,
+ 'aunt(_, Y), \+ parent(Y, _)') == 'false':
+ return [{'id': 'y_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/aunt_2/sl.py b/prolog/problems/family_relations/aunt_2/sl.py
new file mode 100644
index 0000000..95fc6b2
--- /dev/null
+++ b/prolog/problems/family_relations/aunt_2/sl.py
@@ -0,0 +1,60 @@
+# coding=utf-8
+
+id = 98
+name = 'aunt/2'
+slug = 'X je teta od Y'
+
+description = '''\
+<p><code>aunt(X, Y)</code>: <code>X</code> je teta od <code>Y</code>.</p>
+<pre>
+ ?- aunt(sally, X).
+ X = vanessa ;
+ X = patricia.
+</pre>'''
+
+plan = ['''\
+<p>Morda lahko uporabiš rešitev kakšne prejšnje naloge?</p>
+<p><img src="Aunt-5.png" /></p>
+<p>Loni naj zbriše imena relacij (parent, sister) s te slike!</p>
+''', '''\
+<p><img src="Aunt-5.png" /></p>
+''', '''\
+<p>Če je <code>X</code> sestra od starša od <code>Y</code>,
+potem je <code>X</code> teta od <code>Y</code>.</p>
+''']
+
+hint = {
+ 'x_and_y_mixed_up': '''\
+<p><code>X</code> mora biti teta od <code>Y</code>, ne obratno.</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>
+''',
+
+ 'x_must_be_female': '''\
+<p>Teta je navadno ženskega spola.</p>
+''', # TODO: Loni should mark female gender on some aunt hints!
+
+ 'aunt_vs_mother': '''\
+<p>Kako je teta lahko hkrati še mama od <code>Y</code>?
+Si morda pozabil, da teta in starš od <code>Y</code> ne smeta biti ista oseba?</p>
+<p><img src="Aunt-4.png" /></p>
+''',
+
+ 'x_need_not_be_parent': '''\
+<p><code>X</code> pravzaprav ne rabi imeti otrok, da je lahko teta od nekoga...</p>
+''',
+
+ 'y_need_not_be_parent': '''\
+<p>Nečak ali nečakinja <code>Y</code> pravzaprav ne rabi imeti otrok, da ima teto...</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>
+''', # TODO: only trigger if sister is not part of correct solution
+}
+