From dce85b3e2ee99ca3a6692ebdd862b8a228c87f19 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Thu, 17 Sep 2015 18:03:55 +0200 Subject: Use check_answers instead of ask_all for testing Simplify the testing function and add correct test cases for Prolog problems mother/2 and brother/2. --- .../problems/family_relations/brother_2/common.py | 35 +++++++++--------- .../problems/family_relations/father_2/common.py | 31 ++++++++-------- .../problems/family_relations/mother_2/common.py | 42 +++++++++++++--------- .../problems/family_relations/sister_2/common.py | 37 ++++++++++--------- 4 files changed, 78 insertions(+), 67 deletions(-) diff --git a/prolog/problems/family_relations/brother_2/common.py b/prolog/problems/family_relations/brother_2/common.py index 48bc519..e725682 100644 --- a/prolog/problems/family_relations/brother_2/common.py +++ b/prolog/problems/family_relations/brother_2/common.py @@ -1,5 +1,6 @@ # coding=utf-8 +from operator import itemgetter import socket import prolog.engine import prolog.util @@ -28,15 +29,15 @@ hint_typs = { 'x_y_must_be_different': Hint('x_y_must_be_different'), } -def test(program, solved_problems): - # 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'])), - ] +test_cases = [ + ('brother(X, _)', + [{'X': 'alessandro'}, {'X': 'andrew'}, {'X': 'jeffrey'}, + {'X': 'kramer'}, {'X': 'michael'}, {'X': 'william'}]), + ('brother(andrew, X)', + [{'X': 'melanie'}]), +] +def test(program, solved_problems): code = (program + '\n' + server.problems.solutions_for_problems('prolog', solved_problems) + '\n' + server.problems.load_facts('prolog', facts).facts) @@ -44,21 +45,21 @@ def test(program, solved_problems): engine_id = None try: engine_id, output = prolog.engine.create(code=code, timeout=1.0) - if not engine_id: - return 0, len(queries) + 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, 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 + 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) - return n_correct, len(queries) + return n_correct, len(test_cases) def hint(program, solved_problems): # tokens = prolog.util.tokenize(program) diff --git a/prolog/problems/family_relations/father_2/common.py b/prolog/problems/family_relations/father_2/common.py index dd799e7..1816d56 100644 --- a/prolog/problems/family_relations/father_2/common.py +++ b/prolog/problems/family_relations/father_2/common.py @@ -1,5 +1,6 @@ # coding=utf-8 +from operator import itemgetter import socket import prolog.engine import prolog.util @@ -27,15 +28,11 @@ hint_type = { 'y_need_not_be_parent': Hint('y_need_not_be_parent'), } -def test(program, solved_problems): - # 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'])), - ] +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) @@ -43,21 +40,21 @@ def test(program, solved_problems): engine_id = None try: engine_id, output = prolog.engine.create(code=code, timeout=1.0) - if not engine_id: - return 0, len(queries) + 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, 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 + 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) - return n_correct, len(queries) + return n_correct, len(test_cases) def hint(program, solved_problems): tokens = prolog.util.tokenize(program) diff --git a/prolog/problems/family_relations/mother_2/common.py b/prolog/problems/family_relations/mother_2/common.py index 2b74f08..3a7e196 100644 --- a/prolog/problems/family_relations/mother_2/common.py +++ b/prolog/problems/family_relations/mother_2/common.py @@ -1,5 +1,6 @@ # coding=utf-8 +from operator import itemgetter import socket import prolog.engine import prolog.util @@ -27,15 +28,22 @@ hint_type = { 'y_need_not_be_parent': Hint('y_need_not_be_parent'), } -def test(program, solved_problems): - # 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'])), - ] +test_cases = [ + ('mother(X, _)', + [{'X': 'ana'}, {'X': 'elaine'}, {'X': 'estelle'}, {'X': 'helen'}, + {'X': 'jill'}, {'X': 'joanne'}, {'X': 'margaret'}, {'X': 'nevia'}, + {'X': 'patricia'}, {'X': 'sally'}, {'X': 'tina'}, {'X': 'vanessa'}]), + ('mother(_, X)', + [{'X': 'aleksander'}, {'X': 'alessandro'}, {'X': 'andrew'}, + {'X': 'anna'}, {'X': 'daniela'}, {'X': 'george'}, {'X': 'jerry'}, + {'X': 'joanne'}, {'X': 'john'}, {'X': 'kramer'}, {'X': 'luana'}, + {'X': 'melanie'}, {'X': 'nevia'}, {'X': 'steve'}, {'X': 'susan'}, + {'X': 'william'}]), + ('mother(joanne, X)', + [{'X': 'steve'}]), +] +def test(program, solved_problems): code = (program + '\n' + server.problems.solutions_for_problems('prolog', solved_problems) + '\n' + server.problems.load_facts('prolog', facts).facts) @@ -43,21 +51,21 @@ def test(program, solved_problems): engine_id = None try: engine_id, output = prolog.engine.create(code=code, timeout=1.0) - if not engine_id: - return 0, len(queries) + 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, 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 + 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) - return n_correct, len(queries) + return n_correct, len(test_cases) def hint(program, solved_problems): tokens = prolog.util.tokenize(program) diff --git a/prolog/problems/family_relations/sister_2/common.py b/prolog/problems/family_relations/sister_2/common.py index 9c63b18..a1375f2 100644 --- a/prolog/problems/family_relations/sister_2/common.py +++ b/prolog/problems/family_relations/sister_2/common.py @@ -1,5 +1,6 @@ # coding=utf-8 +from operator import itemgetter import socket import prolog.engine import prolog.util @@ -28,14 +29,18 @@ hint_type = { 'x_y_must_be_different': Hint('x_y_must_be_different'), } -def test(program, solved_problems): - # 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'])), - ] +test_cases = [ + ('sister(X, _)', + [{'X': 'anna'}, {'X': 'daniela'}, {'X': 'luana'}, {'X': 'melanie'}, + {'X': 'michelle'}, {'X': 'nevia'}, {'X': 'patricia'}, {'X': 'sally'}, + {'X': 'vanessa'}]), + ('sister(melanie, X)', + [{'X': 'andrew'}]), + ('sister(X, andrew)', + [{'X': 'melanie'}]), +] +def test(program, solved_problems): code = (program + '\n' + server.problems.solutions_for_problems('prolog', solved_problems) + '\n' + server.problems.load_facts('prolog', facts).facts) @@ -43,21 +48,21 @@ def test(program, solved_problems): engine_id = None try: engine_id, output = prolog.engine.create(code=code, timeout=1.0) - if not engine_id: - return 0, len(queries) + 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, 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 + 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) - return n_correct, len(queries) + return n_correct, len(test_cases) def hint(program, solved_problems): # how do I know which general hints were already shown? -- cgit v1.2.1 From b9b623bcbcdd6cb215146405d2f0e09ab6826fd0 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Fri, 18 Sep 2015 10:03:33 +0200 Subject: =?UTF-8?q?Use=20"More=E2=80=A6"=20hints=20in=20fahrenheit=5Fto=5F?= =?UTF-8?q?celsius?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../introduction/fahrenheit_to_celsius/common.py | 6 +- .../introduction/fahrenheit_to_celsius/sl.py | 90 ++++++++++++---------- 2 files changed, 53 insertions(+), 43 deletions(-) diff --git a/python/problems/introduction/fahrenheit_to_celsius/common.py b/python/problems/introduction/fahrenheit_to_celsius/common.py index 0bb6468..1ba0e15 100644 --- a/python/problems/introduction/fahrenheit_to_celsius/common.py +++ b/python/problems/introduction/fahrenheit_to_celsius/common.py @@ -19,9 +19,9 @@ hint_type = { 'no_input_call': Hint('no_input_call'), 'expressions_python': Hint('expressions_python'), 'printing': Hint('printing'), - 'name_error': HintSequence('name_error', 4), - 'unsupported_operand': HintSequence('unsupported_operand', 4), - 'not_callable': HintSequence('not_callable', 4), + 'name_error': Hint('name_error'), + 'unsupported_operand': Hint('unsupported_operand'), + 'not_callable': Hint('not_callable'), } def test(python, code): diff --git a/python/problems/introduction/fahrenheit_to_celsius/sl.py b/python/problems/introduction/fahrenheit_to_celsius/sl.py index 5cce6dc..d040adb 100644 --- a/python/problems/introduction/fahrenheit_to_celsius/sl.py +++ b/python/problems/introduction/fahrenheit_to_celsius/sl.py @@ -14,23 +14,22 @@ general_exception = {

Napaka:

 [%=message%]
-
-''', +''', - 'general': ''' -

Pri razumevanju napake sta pomembni dve vrstici. V predzadnji vrstici je napisana lokacija - napake (line ...), v zadnji vrstici pa izvemo za kakšno napako gre.

- ''', + 'general': '''\ +

Pri razumevanju napake sta pomembni dve vrstici. V predzadnji vrstici je +napisana lokacija napake (line ...), v zadnji vrstici pa izvemo za kakšno +napako gre.

''', - 'name_error' : ''' -

Napaka NameError pomeni, da uporabljate nedefinirano vrednost: - ali vrednost spremenljivke ni določena ali uporabljate funkcijo, ki ni uvožena.

- ''', + 'name_error' : '''\ +

Napaka NameError pomeni, da uporabljate nedefinirano vrednost: +ali vrednost spremenljivke ni določena ali uporabljate funkcijo, ki ni +uvožena.

''', - 'type_error': ''' -

TypeError napaka pomeni, da želite izvesti operacijo na nedovoljenih tipih. - Npr., če želite sešteti niz in število ali klicati funkcijo, čeprav tisto ni funkcija, itd. .

- ''', + 'type_error': '''\ +

TypeError napaka pomeni, da želite izvesti operacijo na nedovoljenih tipih. +Npr., če želite sešteti niz in število ali klicati funkcijo, čeprav tisto ni +funkcija, itd.

''', } hint = { @@ -54,38 +53,46 @@ ime = input("Kako ti je ime?")

pokliče funkcijo input, ki povpraša uporabnika po imenu in si shrani uporabnikov odgovor v spremenljivko ime.

''', - - 'expressions_python': ''' -

Računanje: če v Pythonu napišemo izraz, se bo ta izračunal. Npr., če napišemo

+ 'expressions_python': '''\ +

Računanje: če v Pythonu napišemo izraz, se bo ta izračunal. Npr., če +napišemo

 3 + 6
-
-bo Python seštel vrednosti 3 in 6 in ... rezultat pozabil. Kadar pa želimo 
-rezultat shraniti, za to uporabimo prireditveni stavek, kjer na levo 
+
+

bo Python seštel vrednosti 3 in 6 in ... rezultat pozabil. Kadar pa želimo +rezultat shraniti, za to uporabimo prireditveni stavek, kjer na levo napišemo ime spremenljivke, na desno pa izraz:

 c = 2 * a * (3 + b)
 
-

Kot vidite, izraz lahko uporablja tudi spremenljivke.

''', +

Kot vidite, izraz lahko uporablja tudi spremenljivke.

''', - 'printing': ''' -

V Pythonu izpisujemo s funkcijo print. Če želimo izpisati več elementov, -jih ločimo z vejico. Recimo, da imamo spremenljivko ime, -ki vsebuje naše ime, potem lahko napišemo: + 'printing': '''\ +

V Pythonu izpisujemo s funkcijo print. Če želimo izpisati več +elementov, jih ločimo z vejico. Recimo, da imamo spremenljivko +ime, ki vsebuje naše ime, potem lahko napišemo:

 print("Ime mi je", ime, ".")
 
''', - 'name_error' : [general_exception['error_head'], general_exception['general'], - general_exception['name_error'], ''' -

Verjetno uporabljate spremenljivko, ki nima vrednosti. Ali v izrazu za izračun - uporabljate napačno spremenljivko? Ali pri izpisu morda poskušate - izpisati napačno spremenljivko?

'''], + 'name_error' : [ + general_exception['error_head'], + general_exception['general'], + general_exception['name_error'], + '''\ +

Verjetno uporabljate spremenljivko, ki nima vrednosti. Ali v izrazu za +izračun uporabljate napačno spremenljivko? Ali pri izpisu morda poskušate +izpisati napačno spremenljivko?

''' + ], - 'unsupported_operand' : [general_exception['error_head'], general_exception['general'], - general_exception['type_error'], ''' -

Verjetni razlog: funkcija input vrača vrednost tipa niz, -ki jo moramo najprej pretvoriti v tip float, če želimo z njo računati:

+ 'unsupported_operand' : [ + general_exception['error_head'], + general_exception['general'], + general_exception['type_error'], + '''\ +

Verjetni razlog: funkcija input vrača vrednost tipa niz, ki jo +moramo najprej pretvoriti v tip float, če želimo z njo +računati:

 v = float(input(" ...
 
@@ -100,11 +107,14 @@ f = float(fniz) f = float(input("Temperatura [F]: "))

Funkcijo float lahko kličemo le, če je v nizu zapisano število, -npr. "10". Če imamo v nizu tudi kakšno črko, bo Python javil napako.

'''], - +npr. "10". Če imamo v nizu tudi kakšno črko, bo Python javil napako.

''' + ], - 'not_callable' : [general_exception['error_head'], general_exception['general'], - general_exception['type_error'], ''' + 'not_callable' : [ + general_exception['error_head'], + general_exception['general'], + general_exception['type_error'], + '''\

V programu poskušate uporabiti število kot funkcijo, a to ne gre. Verjetni razlog: Ali ste v izrazu pozabili na znak *. Pri pretvorbi temperatur bi namesto:

@@ -114,6 +124,6 @@ C = 5/9(F – 32)
 
 C = 5/9 * (F – 32).
 
-

Python ne zna izpuščati znaka za množenje, kot to delamo pri matematiki.

'''], - +

Python ne zna izpuščati znaka za množenje, kot to delamo pri matematiki.

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