From ee50df2225a974857e74d5ecb7c5f503e1c6b18a Mon Sep 17 00:00:00 2001 From: Aleksander Sadikov Date: Thu, 10 Sep 2015 20:39:02 +0200 Subject: Hints for mother, father, brother, and sister added. --- .../problems/family_relations/father_2/common.py | 97 ++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 prolog/problems/family_relations/father_2/common.py (limited to 'prolog/problems/family_relations/father_2/common.py') diff --git a/prolog/problems/family_relations/father_2/common.py b/prolog/problems/family_relations/father_2/common.py new file mode 100644 index 0000000..e1b46fd --- /dev/null +++ b/prolog/problems/family_relations/father_2/common.py @@ -0,0 +1,97 @@ +# coding=utf-8 + +id = 999 # TODO: new id has to be defined for this problem +group = 'family_relations' +number = 999 # TODO: this should be problem #2 +visible = True +facts = 'family_relations' + +solution = '''\ +father(X, Y) :- + parent(X, Y), + male(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(_, father(X, Y), L1), length(L1, N), \ + findall(_, (parent(X, Y) ; male(X)), L2), length(L2, N)'): + return [{'id': 'or_instead_of_and'}] + + # X must be male + if prolog.engine.ask_truth(engine_id, 'female(X), father(X, _)'): + return [{'id': 'x_must_be_male'}] + + # X must be a parent + if prolog.engine.ask_truth(engine_id, + 'father(X, _), \+ parent(X, _)'): + return [{'id': 'x_must_be_parent'}] + + # Y can be of any gender, incl. female + if prolog.engine.ask_one(engine_id, + 'father(_, Y), female(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, + 'father(_, 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 -- cgit v1.2.1