From c50452d81b08d54b734f1da73b736bfbc2997215 Mon Sep 17 00:00:00 2001 From: Aleksander Sadikov Date: Tue, 22 Sep 2015 17:57:04 +0200 Subject: New problems with hints for famrel section added. This practically completes famrel section without some details. --- .../problems/family_relations/cousin_2/common.py | 95 ++++++++++++++++++++++ prolog/problems/family_relations/cousin_2/sl.py | 63 ++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 prolog/problems/family_relations/cousin_2/sl.py (limited to 'prolog/problems/family_relations/cousin_2') 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 = '''\ +

cousin(X, Y): X je bratranec ali sestrična od Y.

+
+  ?- cousin(andrew, X).
+    X = vanessa ;
+    X = patricia.
+
''' + +plan = ['''\ +

Verjetno bi se sedaj splačalo uporabiti rešitev kakšne prejšnje naloge?

+

+

Loni naj zbriše imena relacij (parent, sister) s te slike!

+

Seveda se da rešiti tudi brez prejšnjih rešitev... MORE

+

+

Loni naj zbriše imena relacij (parent, sister) s te slike!

+''', '''\ +

+''', '''\ +

Če je PX starš od X in je +PY starš od Y ter sta +PX in PY brat ali sestra, +potem je X bratranec/sestrična od Y.

+'''] + +hint = { + 'gender_is_irrelevant': '''\ +

Je spol res pomemben?

+''', + + 'precedence_fail': '''\ +

Si morda narobe upošteval prioriteto operatorjev IN ter ALI?

+

Operator IN veže močneje od ALI, če želiš spremeniti prioriteto, +lahko uporabiš oklepaje.

+''', + + 'cousin_vs_sibling': '''\ +

Kako je lahko bratranec/sestrična hkrati tudi brat/sestra od Y? +Si morda pozabil, da starša od X in Y ne smeta biti ista oseba?

+

+''', + + 'cousin_to_oneself': '''\ +

Kako je lahko nekdo bratranec/sestrična samemu sebi? +Imata morda X in Y istega starša?

+

+''', + + 'cousin_need_not_be_parent': '''\ +

Bratranec/sestrična pravzaprav ne rabi imeti otrok...

+''', + +# 'interesting_tidbit': '''\ +#

Zanimivost: nalogo bi lahko rešil tudi z uporabo rešitve za relacijo "sister". +# Teta je namreč sestra od starša od Y.

+# ''', +} + -- cgit v1.2.1