summaryrefslogtreecommitdiff
path: root/prolog/problems/family_relations/mother_2
diff options
context:
space:
mode:
Diffstat (limited to 'prolog/problems/family_relations/mother_2')
-rw-r--r--prolog/problems/family_relations/mother_2/common.py84
-rw-r--r--prolog/problems/family_relations/mother_2/sl.py53
2 files changed, 137 insertions, 0 deletions
diff --git a/prolog/problems/family_relations/mother_2/common.py b/prolog/problems/family_relations/mother_2/common.py
index 65087b4..613b721 100644
--- a/prolog/problems/family_relations/mother_2/common.py
+++ b/prolog/problems/family_relations/mother_2/common.py
@@ -11,3 +11,87 @@ mother(X, Y) :-
parent(X, Y),
female(X).
'''
+
+import prolog.engine
+import prolog.util
+import server.problems
+
+def test(session, program):
+ # TODO: change queries and answers (these are for sister/2)
+ # Test queries and expected answers (values of X).
+ queries = [
+ ('sister(melanie, X)', set(['andrew'])),
+ ('sister(X, andrew)', set(['melanie'])),
+ ('sister(X, _)', set(['michelle', 'daniela', 'patricia', 'luana', 'sally', 'melanie', 'nevia', 'vanessa', 'anna'])),
+ ]
+
+ code = program + '\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:
+ return 0, len(queries)
+
+ n_correct = 0
+ for query, solution in queries:
+ answers, error = prolog.engine.ask_all(engine_id, query=query, timeout=1.0)
+ if answers and answers[-1] != 'timed out':
+ values_x = set([ans.get('X') for ans, constraints in answers])
+ if values_x == solution:
+ n_correct += 1
+ finally:
+ if engine_id:
+ prolog.engine.destroy(engine_id)
+
+ return n_correct, len(queries)
+
+def general_hint():
+ pass
+
+def hint(session, program):
+ tokens = prolog.util.tokenize(program)
+
+ code = program + '\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.')
+
+ # OR (;) instead of AND (,)
+ # this hint has to be before the next two
+ # as otherwise those two would always override it
+ # and not convey the same (amount of) help/information
+ if prolog.util.Token('SEMI', ';') in tokens and prolog.engine.ask_truth(engine_id,
+ 'findall(_, mother(X, Y), L1), length(L1, N), \
+ findall(_, (parent(X, Y) ; female(X)), L2), length(L2, N)'):
+ return [{'id': 'or_instead_of_and'}]
+
+ # X must be female
+ if prolog.engine.ask_truth(engine_id, 'male(X), mother(X, _)'):
+ return [{'id': 'x_must_be_female'}]
+
+ # X must be a parent
+ if prolog.engine.ask_truth(engine_id,
+ 'mother(X, _), \+ parent(X, _)'):
+ return [{'id': 'x_must_be_parent'}]
+
+ # Y can be of any gender, incl. male
+ if prolog.engine.ask_one(engine_id,
+ 'mother(_, Y), male(Y)') == 'false':
+ return [{'id': 'y_can_be_of_any_gender'}]
+
+ # Y does not necessarily need to be a parent
+ if prolog.engine.ask_one(engine_id,
+ 'mother(_, 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/mother_2/sl.py b/prolog/problems/family_relations/mother_2/sl.py
new file mode 100644
index 0000000..8923eb5
--- /dev/null
+++ b/prolog/problems/family_relations/mother_2/sl.py
@@ -0,0 +1,53 @@
+# coding=utf-8
+
+id = 94
+name = 'mother/2'
+slug = 'X je mama od Y'
+
+description = '''\
+<p><code>mother(X, Y)</code>: <code>X</code> je mama od <code>Y</code>.</p>
+<pre>
+ ?- mother(tina, william).
+ true.
+ ?- mother(nevia, X).
+ X = luana ;
+ X = daniela.
+</pre>'''
+
+hint = {
+ 'general_hint_1': '''\
+<p><img src="Mother-1.png" /></p>
+<p>Loni naj zbriše imena relacij (parent, mother) s te slike!</p>
+''',
+
+ 'general_hint_2': '''\
+<p><img src="Mother-1.png" /></p>
+''',
+
+ 'general_hint_3': '''\
+<p>Če je <code>X</code> ženska in je hkrati <code>X</code> starš od <code>Y</code>,
+potem je <code>X</code> mama od <code>Y</code>.</p>
+''',
+
+ 'or_instead_of_and': '''\
+<p>Si morda uporabil podpičje (ki pomeni ALI) namesto vejice (ki pomeni IN)?</p>
+''',
+
+ 'x_must_be_female': '''\
+<p>Mama je navadno ženskega spola.</p>
+<p><img src="Mother-2.png" /></p>
+''',
+
+ 'x_must_be_parent': '''\
+<p>Mama naj bi imela vsaj enega otroka... torej je starš od nekoga.</p>
+<p><img src="Mother-3.png" /></p>
+''',
+
+ 'y_can_be_of_any_gender': '''\
+<p><code>Y</code> je pravzaprav lahko poljubnega spola.</p>
+''',
+
+ 'y_need_not_be_parent': '''\
+<p><code>Y</code> pravzaprav ne rabi imeti otrok, da ima mamo...</p>
+''',
+}