From a37c3602e0deaa6da902b1202325b80624e21724 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sun, 20 Mar 2016 17:13:12 +0100 Subject: Prolog: add triggers for palindrome/min/max hints --- .../problems/lists_advanced/palindrome_1/common.py | 31 +++++++++++++++++++--- prolog/problems/lists_advanced/palindrome_1/sl.py | 20 ++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) (limited to 'prolog/problems/lists_advanced/palindrome_1') diff --git a/prolog/problems/lists_advanced/palindrome_1/common.py b/prolog/problems/lists_advanced/palindrome_1/common.py index 3d202b9..f1baf4e 100644 --- a/prolog/problems/lists_advanced/palindrome_1/common.py +++ b/prolog/problems/lists_advanced/palindrome_1/common.py @@ -28,6 +28,11 @@ hint_type = { 'predicate_always_false': Hint('predicate_always_false'), 'base_case': Hint('base_case'), 'timeout': Hint('timeout'), + '[X,X]_instead_of_[]_base_case': Hint('[X,X]_instead_of_[]_base_case'), + 'one_base_case_missing': Hint('one_base_case_missing'), + 'arbitrary_base_case': Hint('arbitrary_base_case'), + 'last_used': Hint('last_used'), + 'final_hint': Hint('final_hint'), } test_cases = [ @@ -56,6 +61,8 @@ def test(code, aux_code): prolog.engine.destroy(engine_id) hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + if n_correct == len(test_cases): + hints += [{'id': 'final_hint'}] return n_correct, len(test_cases), hints def hint(code, aux_code): @@ -72,14 +79,32 @@ def hint(code, aux_code): return [{'id': 'eq_instead_of_equ_markup', 'start': m[0], 'end': m[1]} for m in marks] + \ [{'id': 'eq_instead_of_equ'}] - # missing/failed base case - if not prolog.engine.ask_truthTO(engine_id, 'palindrome([])'): - return [{'id': 'base_case'}] + if prolog.engine.ask_truthTO(engine_id, + 'palindrome([q, q]), \+ palindrome([]), \+ palindrome([q, a])'): + return [{'id': '[X,X]_instead_of_[]_base_case'}] + + if prolog.engine.ask_truthTO(engine_id, '''\ + palindrome([a, b, b, a]), + \+ palindrome([c, e, p, e, c]) + ; + \+ palindrome([a, b, b, a]), + palindrome([c, e, p, e, c])'''): + return [{'id': 'one_base_case_missing'}] + + if prolog.engine.ask_truthTO(engine_id, 'palindrome(kokos(1))'): + return [{'id': 'arbitrary_base_case'}] + + if any(t.val == 'last' for t in tokens): + return [{'id': 'last_used'}] # target predicate seems to always be false if not prolog.engine.ask_truthTO(engine_id, 'palindrome(_)'): return [{'id': 'predicate_always_false'}] + # missing/failed base case + if not prolog.engine.ask_truthTO(engine_id, 'palindrome([])'): + return [{'id': 'base_case'}] + except socket.timeout as ex: return [{'id': 'timeout'}] diff --git a/prolog/problems/lists_advanced/palindrome_1/sl.py b/prolog/problems/lists_advanced/palindrome_1/sl.py index 400ac6b..6f4a715 100644 --- a/prolog/problems/lists_advanced/palindrome_1/sl.py +++ b/prolog/problems/lists_advanced/palindrome_1/sl.py @@ -39,5 +39,25 @@ da je X hkrati starš in sestra od Y ali kaj podobno z 'timeout': '''\

Je morda na delu potencialno neskončna rekurzija? Kako se bo ustavila?

Morda pa je kriv tudi manjkajoč, neustrezen ali preprosto nekompatibilen (s splošnim primerom) robni pogoj?

+''', + + '[X,X]_instead_of_[]_base_case': '''\ +

[X,X]_instead_of_[]_base_case')

+''', + + 'one_base_case_missing': '''\ +

one_base_case_missing

+''', + + 'arbitrary_base_case': '''\ +

arbitrary_base_case

+''', + + 'last_used': '''\ +

last_used

+''', + + 'final_hint': '''\ +

final_hint

''', } -- cgit v1.2.1