diff options
Diffstat (limited to 'prolog/problems/family_relations/brother_2')
-rw-r--r-- | prolog/problems/family_relations/brother_2/common.py | 77 | ||||
-rw-r--r-- | prolog/problems/family_relations/brother_2/sl.py | 51 |
2 files changed, 128 insertions, 0 deletions
diff --git a/prolog/problems/family_relations/brother_2/common.py b/prolog/problems/family_relations/brother_2/common.py index 6038a13..41512e6 100644 --- a/prolog/problems/family_relations/brother_2/common.py +++ b/prolog/problems/family_relations/brother_2/common.py @@ -13,3 +13,80 @@ brother(X, Y) :- male(X), X \== Y. ''' + +import prolog.engine +import prolog.util +import server.problems +import socket + +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.') + + # X must be male + if prolog.engine.ask_truth(engine_id, 'female(X), brother(X, _)'): + return [{'id': 'x_must_be_male'}] + + # X and Y must have a common parent + if prolog.engine.ask_truth(engine_id, + 'brother(X, Y), \+ (parent(P, X), parent(P, Y))'): + return [{'id': 'common_parent_needed'}] + + # Y can be of any gender, incl. female + if prolog.engine.ask_one(engine_id, + 'brother(_, Y), female(Y)') == 'false': + return [{'id': 'Y_can_be_of_any_gender'}] + + # X and Y must be different + ans = prolog.engine.ask_one(engine_id, + 'setof(X, (member(X, [william, alessandro, andrew]), brother(X, X)), L), length(L, N).') + if ans.get('N') == '3': + return [{'id': 'x_y_must_be_different'}] + + except socket.timeout as ex: + pass + + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + return None diff --git a/prolog/problems/family_relations/brother_2/sl.py b/prolog/problems/family_relations/brother_2/sl.py new file mode 100644 index 0000000..f9db389 --- /dev/null +++ b/prolog/problems/family_relations/brother_2/sl.py @@ -0,0 +1,51 @@ +# coding=utf-8 + +id = 97 +name = 'brother/2' +slug = 'X je brat od Y' + +description = '''\ +<p><code>brother(X, Y)</code>: <code>X</code> je brat od <code>Y</code>.</p> +<pre> + ?- brother(jeffrey, X). + X = william ; + X = sally. +</pre>''' + +hint = { + 'general_hint_1': '''\ +<p><img src="Sister-1.png" /></p> +''', + + 'general_hint_2': '''\ +<p><img src="Sister-2.png" /></p> +''', + + 'general_hint_3': '''\ +<p><img src="Sister-2.png" /></p> +<pre> +parent(P, X) +parent(P, Y) +male(X) +</pre> +''', + + 'x_y_must_be_different': '''\ +<p>Si pomislil, da sta <code>X</code> in <code>Y</code> lahko ista oseba? Poskusi naslednjo poizvedbo:</p> +<pre> +?- brother(william, Y). +</pre> +''', + + 'x_must_be_male': '''\ +<p>Brat je navadno moškega spola.</p> +''', + + 'Y_can_be_of_any_gender': '''\ +<p><code>Y</code> je pravzaprav lahko poljubnega spola.</p> +''', + + 'common_parent_needed': '''\ +<p>Dobro bi bilo, da imata <code>X</code> in <code>Y</code> vsaj enega skupnega starša.</p> +''', +} |