diff options
Diffstat (limited to 'prolog/problems')
-rw-r--r-- | prolog/problems/lists/count_3/common.py | 42 | ||||
-rw-r--r-- | prolog/problems/lists/del_3/common.py | 40 | ||||
-rw-r--r-- | prolog/problems/lists/divide_3/common.py | 44 | ||||
-rw-r--r-- | prolog/problems/lists/dup_2/common.py | 42 | ||||
-rw-r--r-- | prolog/problems/lists/evenlen_1_+_oddlen_1/common.py | 44 | ||||
-rw-r--r-- | prolog/problems/lists/insert_3/common.py | 40 | ||||
-rw-r--r-- | prolog/problems/lists/len_2/common.py | 4 | ||||
-rw-r--r-- | prolog/problems/lists/max_2/common.py | 42 | ||||
-rw-r--r-- | prolog/problems/lists/memb_2/common.py | 38 | ||||
-rw-r--r-- | prolog/problems/lists/min_2/common.py | 42 | ||||
-rw-r--r-- | prolog/problems/lists/palindrome_1/common.py | 40 | ||||
-rw-r--r-- | prolog/problems/lists/permute_2/common.py | 40 | ||||
-rw-r--r-- | prolog/problems/lists/rev_2/common.py | 40 | ||||
-rw-r--r-- | prolog/problems/lists/shiftleft_2/common.py | 40 | ||||
-rw-r--r-- | prolog/problems/lists/shiftright_2/common.py | 40 | ||||
-rw-r--r-- | prolog/problems/lists/sublist_2/common.py | 42 | ||||
-rw-r--r-- | prolog/problems/lists/sum_2/common.py | 40 |
17 files changed, 658 insertions, 2 deletions
diff --git a/prolog/problems/lists/count_3/common.py b/prolog/problems/lists/count_3/common.py index 8a85079..e28ad5c 100644 --- a/prolog/problems/lists/count_3/common.py +++ b/prolog/problems/lists/count_3/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 120 number = 27 visible = True @@ -14,3 +18,41 @@ count(X, [Y|T], NT) :- X \== Y, count(X, T, NT). ''' + +test_cases = [ + ('count(a, [], X)', + [{'X': '0'}]), + ('count(r, [a,r,b,c,r], X)', + [{'X': '2'}]), + ('count(l, [l,l,l,1,2,3], X)', + [{'X': '3'}]), + ('count(z, [a,b,c,z,z], X)', + [{'X': '2'}]), +] + +def test(program, solved_problems): + code = (program + '\n' + + server.problems.solutions_for_problems('prolog', solved_problems)) + + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code, timeout=1.0) + if engine_id is not None and 'error' not in map(itemgetter(0), output): + # Engine successfully created, and no syntax error in program. + 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): + # TODO + return [] diff --git a/prolog/problems/lists/del_3/common.py b/prolog/problems/lists/del_3/common.py index 755193c..1b37475 100644 --- a/prolog/problems/lists/del_3/common.py +++ b/prolog/problems/lists/del_3/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 105 number = 11 visible = True @@ -10,3 +14,39 @@ del(X, [X|T], T). del(X, [Y|T], [Y|L]) :- del(X, T, L). ''' + +test_cases = [ + ('del(1, [1], X)', + [{'X': '[]'}]), + ('del(X, [t,e,g], [t,g])', + [{'X': 'e'}]), + ('del(X, [Y,g,Z], [u,f])', + [{'X': 'g', 'Y': 'u', 'Z': 'f'}]), +] + +def test(program, solved_problems): + code = (program + '\n' + + server.problems.solutions_for_problems('prolog', solved_problems)) + + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code, timeout=1.0) + if engine_id is not None and 'error' not in map(itemgetter(0), output): + # Engine successfully created, and no syntax error in program. + 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): + # TODO + return [] diff --git a/prolog/problems/lists/divide_3/common.py b/prolog/problems/lists/divide_3/common.py index c5744cc..8cc1073 100644 --- a/prolog/problems/lists/divide_3/common.py +++ b/prolog/problems/lists/divide_3/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 115 number = 22 visible = True @@ -11,3 +15,43 @@ divide([X], [X], []). divide([H1,H2|T], [H1|L1], [H2|L2]) :- divide(T, L1, L2). ''' + +test_cases = [ + ('divide([], A, B)', + [{'A': '[]', 'B': '[]'}]), + ('divide([q], A, B)', + [{'A': '[q]', 'B': '[]'}]), + ('divide([h, e, t, y, q, w], A, B)', + [{'A': '[h, t, q]', 'B': '[e, y, w]'}]), + ('divide([j, e, y, u, i], A, B)', + [{'A': '[j, y, i]', 'B': '[e, u]'}]), + ('divide(A, [t, q, y], [l, e, r])', + [{'A': '[t, l, q, e, y, r]'}]), +] + +def test(program, solved_problems): + code = (program + '\n' + + server.problems.solutions_for_problems('prolog', solved_problems)) + + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code, timeout=1.0) + if engine_id is not None and 'error' not in map(itemgetter(0), output): + # Engine successfully created, and no syntax error in program. + 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): + # TODO + return [] diff --git a/prolog/problems/lists/dup_2/common.py b/prolog/problems/lists/dup_2/common.py index e0a1001..9758b64 100644 --- a/prolog/problems/lists/dup_2/common.py +++ b/prolog/problems/lists/dup_2/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 110 number = 17 visible = True @@ -10,3 +14,41 @@ dup([], []). dup([H|T], [H,H|TT]) :- dup(T, TT). ''' + +test_cases = [ + ('dup([], X)', + [{'X': '[]'}]), + ('dup([y], X)', + [{'X': '[y, y]'}]), + ('dup([k, f, f, g, a], X)', + [{'X': '[k, k, f, f, f, f, g, g, a, a]'}]), + ('dup(X, [k, k, f, f, f, f, g, g, a, a])', + [{'X': '[k, f, f, g, a]'}]), +] + +def test(program, solved_problems): + code = (program + '\n' + + server.problems.solutions_for_problems('prolog', solved_problems)) + + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code, timeout=1.0) + if engine_id is not None and 'error' not in map(itemgetter(0), output): + # Engine successfully created, and no syntax error in program. + 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): + # TODO + return [] diff --git a/prolog/problems/lists/evenlen_1_+_oddlen_1/common.py b/prolog/problems/lists/evenlen_1_+_oddlen_1/common.py index 601cd33..4df2726 100644 --- a/prolog/problems/lists/evenlen_1_+_oddlen_1/common.py +++ b/prolog/problems/lists/evenlen_1_+_oddlen_1/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 116 number = 23 visible = True @@ -14,3 +18,43 @@ oddlen([_]). oddlen([_,_|T]) :- oddlen(T). ''' + +test_cases = [ + ('evenlen([])', + [{}]), + ('evenlen([r, w, t, q])', + [{}]), + ('\+ evenlen([r, w, t, q, l])', + [{}]), + ('\+ oddlen([r, w, t, q])', + [{}]), + ('oddlen([r, w, t, q, l])', + [{}]), +] + +def test(program, solved_problems): + code = (program + '\n' + + server.problems.solutions_for_problems('prolog', solved_problems)) + + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code, timeout=1.0) + if engine_id is not None and 'error' not in map(itemgetter(0), output): + # Engine successfully created, and no syntax error in program. + 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): + # TODO + return [] diff --git a/prolog/problems/lists/insert_3/common.py b/prolog/problems/lists/insert_3/common.py index 4892f4d..a4fe40c 100644 --- a/prolog/problems/lists/insert_3/common.py +++ b/prolog/problems/lists/insert_3/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 106 number = 13 visible = True @@ -12,3 +16,39 @@ del106(X, [Y|T], [Y|L]) :- insert(X, List, BigList) :- del106(X, BigList, List). ''' + +test_cases = [ + ('insert(l, [], X)', + [{'X': '[l]'}]), + ('insert(X, [h, t, w, j], [A, B, C, D, z])', + [{'A': 'h', 'B': 't', 'C': 'w', 'D': 'j', 'X': 'z'}]), + ('insert(i, [A, j], [c, X, j])', + [{'A': 'c', 'X': 'i'}]), +] + +def test(program, solved_problems): + code = (program + '\n' + + server.problems.solutions_for_problems('prolog', solved_problems)) + + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code, timeout=1.0) + if engine_id is not None and 'error' not in map(itemgetter(0), output): + # Engine successfully created, and no syntax error in program. + 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): + # TODO + return [] diff --git a/prolog/problems/lists/len_2/common.py b/prolog/problems/lists/len_2/common.py index d30588c..61ea064 100644 --- a/prolog/problems/lists/len_2/common.py +++ b/prolog/problems/lists/len_2/common.py @@ -17,9 +17,9 @@ len([_|T], Len) :- ''' test_cases = [ - ('len([8,3,5,1], X)', + ('len([8, 3, 5, 1], X)', [{'X': '4'}]), - ('len([2,-3,5,0,3], X)', + ('len([2, -3, 5, 0, 3], X)', [{'X': '5'}]), ('len([], X)', [{'X': '0'}]), diff --git a/prolog/problems/lists/max_2/common.py b/prolog/problems/lists/max_2/common.py index 0e8f031..d055ab2 100644 --- a/prolog/problems/lists/max_2/common.py +++ b/prolog/problems/lists/max_2/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 109 number = 16 visible = True @@ -13,3 +17,41 @@ max([H|T], Max):- ; H =< Max1, Max is Max1 ). ''' + +test_cases = [ + ('max([15], X)', + [{'X': '15'}]), + ('max([22, 13, 81], X)', + [{'X': '81'}]), + ('max([42, 42, 42, 42], X)', + [{'X': '42'}]), + ('max([-22, -113, -41], X)', + [{'X': '-22'}]), +] + +def test(program, solved_problems): + code = (program + '\n' + + server.problems.solutions_for_problems('prolog', solved_problems)) + + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code, timeout=1.0) + if engine_id is not None and 'error' not in map(itemgetter(0), output): + # Engine successfully created, and no syntax error in program. + 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): + # TODO + return [] diff --git a/prolog/problems/lists/memb_2/common.py b/prolog/problems/lists/memb_2/common.py index 3acbc15..b9c0584 100644 --- a/prolog/problems/lists/memb_2/common.py +++ b/prolog/problems/lists/memb_2/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 103 number = 10 visible = True @@ -10,3 +14,37 @@ memb(X, [X|_]). memb(X, [_|T]) :- memb(X, T). ''' + +test_cases = [ + ('memb(c, [X])', + [{'X': 'c'}]), + ('memb(X, [y,l,r,g,a])', + [{'X': 'y'}, {'X': 'l'}, {'X': 'r'}, {'X': 'g'}, {'X': 'a'}]), +] + +def test(program, solved_problems): + code = (program + '\n' + + server.problems.solutions_for_problems('prolog', solved_problems)) + + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code, timeout=1.0) + if engine_id is not None and 'error' not in map(itemgetter(0), output): + # Engine successfully created, and no syntax error in program. + 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): + # TODO + return [] diff --git a/prolog/problems/lists/min_2/common.py b/prolog/problems/lists/min_2/common.py index cc6ad9e..632e473 100644 --- a/prolog/problems/lists/min_2/common.py +++ b/prolog/problems/lists/min_2/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 108 number = 15 visible = True @@ -13,3 +17,41 @@ min([H|T], Min):- ; H >= Min1, Min is Min1 ). ''' + +test_cases = [ + ('min([15], X)', + [{'X': '15'}]), + ('min([22, 13, 81], X)', + [{'X': '13'}]), + ('min([42, 42, 42, 42], X)', + [{'X': '42'}]), + ('min([-22, 113, 1], X)', + [{'X': '-22'}]), +] + +def test(program, solved_problems): + code = (program + '\n' + + server.problems.solutions_for_problems('prolog', solved_problems)) + + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code, timeout=1.0) + if engine_id is not None and 'error' not in map(itemgetter(0), output): + # Engine successfully created, and no syntax error in program. + 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): + # TODO + return [] diff --git a/prolog/problems/lists/palindrome_1/common.py b/prolog/problems/lists/palindrome_1/common.py index 5df4b87..ae82c9b 100644 --- a/prolog/problems/lists/palindrome_1/common.py +++ b/prolog/problems/lists/palindrome_1/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 112 number = 19 visible = True @@ -16,3 +20,39 @@ rev112([H|T], R):- palindrome(L) :- rev112(L, L). ''' + +test_cases = [ + ('palindrome([])', + [{}]), + ('palindrome([a, b, e, A, B, C])', + [{'A': 'e', 'B': 'b', 'C': 'a'}]), + ('palindrome([b, e, X])', + [{'X': 'b'}]), +] + +def test(program, solved_problems): + code = (program + '\n' + + server.problems.solutions_for_problems('prolog', solved_problems)) + + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code, timeout=1.0) + if engine_id is not None and 'error' not in map(itemgetter(0), output): + # Engine successfully created, and no syntax error in program. + 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): + # TODO + return [] diff --git a/prolog/problems/lists/permute_2/common.py b/prolog/problems/lists/permute_2/common.py index 2f37a3b..3662421 100644 --- a/prolog/problems/lists/permute_2/common.py +++ b/prolog/problems/lists/permute_2/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 107 number = 14 visible = True @@ -14,3 +18,39 @@ permute(L, [X|P]) :- del107(X, L, L1), permute(L1, P). ''' + +test_cases = [ + ('permute([], X)', + [{'X': '[]'}]), + ('permute([a, i, o, e], [i, X, o, e])', + [{'X': 'a'}]), + ('setof(L, permute([e, b, l], L), X)', + [{'X': '[[b, e, l], [b, l, e], [e, b, l], [e, l, b], [l, b, e], [l, e, b]]'}]), +] + +def test(program, solved_problems): + code = (program + '\n' + + server.problems.solutions_for_problems('prolog', solved_problems)) + + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code, timeout=1.0) + if engine_id is not None and 'error' not in map(itemgetter(0), output): + # Engine successfully created, and no syntax error in program. + 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): + # TODO + return [] diff --git a/prolog/problems/lists/rev_2/common.py b/prolog/problems/lists/rev_2/common.py index ba72492..117049a 100644 --- a/prolog/problems/lists/rev_2/common.py +++ b/prolog/problems/lists/rev_2/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 111 number = 18 visible = True @@ -14,3 +18,39 @@ rev([H|T], R):- rev(T, R1), conc111(R1, [H], R). ''' + +test_cases = [ + ('rev([], X)', + [{'X': '[]'}]), + ('rev([s, k, c, d, d], X)', + [{'X': '[d, d, c, k, s]'}]), + ('rev(X, [s, k, c, d, d])', + [{'X': '[d, d, c, k, s]'}]), +] + +def test(program, solved_problems): + code = (program + '\n' + + server.problems.solutions_for_problems('prolog', solved_problems)) + + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code, timeout=1.0) + if engine_id is not None and 'error' not in map(itemgetter(0), output): + # Engine successfully created, and no syntax error in program. + 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): + # TODO + return [] diff --git a/prolog/problems/lists/shiftleft_2/common.py b/prolog/problems/lists/shiftleft_2/common.py index 9a2fe0e..4d68265 100644 --- a/prolog/problems/lists/shiftleft_2/common.py +++ b/prolog/problems/lists/shiftleft_2/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 113 number = 20 visible = True @@ -12,3 +16,39 @@ conc113([H|T], L2, [H|L]) :- shiftleft([H|T], L2) :- conc113(T, [H], L2). ''' + +test_cases = [ + ('shiftleft([i], X)', + [{'X': '[i]'}]), + ('shiftleft([h, e, u, l], X)', + [{'X': '[e, u, l, h]'}]), + ('shiftleft([h, A, B, l, c], [x, y, C, D, E])', + [{'A': 'x', 'B': 'y', 'C': 'l', 'D': 'c', 'E': 'h'}]), +] + +def test(program, solved_problems): + code = (program + '\n' + + server.problems.solutions_for_problems('prolog', solved_problems)) + + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code, timeout=1.0) + if engine_id is not None and 'error' not in map(itemgetter(0), output): + # Engine successfully created, and no syntax error in program. + 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): + # TODO + return [] diff --git a/prolog/problems/lists/shiftright_2/common.py b/prolog/problems/lists/shiftright_2/common.py index 5358935..e9b7a7a 100644 --- a/prolog/problems/lists/shiftright_2/common.py +++ b/prolog/problems/lists/shiftright_2/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 114 number = 21 visible = True @@ -12,3 +16,39 @@ conc114([H|T], L2, [H|L]) :- shiftright(L1, [H|T]) :- conc114(T, [H], L1). ''' + +test_cases = [ + ('shiftright([i], X)', + [{'X': '[i]'}]), + ('shiftright([h, e, u, l], X)', + [{'X': '[l, h, e, u]'}]), + ('shiftright([h, A, B, l, c], [C, D, x, y, E])', + [{'A': 'x', 'B': 'y', 'C': 'c', 'D': 'h', 'E': 'l'}]), +] + +def test(program, solved_problems): + code = (program + '\n' + + server.problems.solutions_for_problems('prolog', solved_problems)) + + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code, timeout=1.0) + if engine_id is not None and 'error' not in map(itemgetter(0), output): + # Engine successfully created, and no syntax error in program. + 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): + # TODO + return [] diff --git a/prolog/problems/lists/sublist_2/common.py b/prolog/problems/lists/sublist_2/common.py index 694bfcf..9e8b987 100644 --- a/prolog/problems/lists/sublist_2/common.py +++ b/prolog/problems/lists/sublist_2/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 117 number = 24 visible = True @@ -13,3 +17,41 @@ sublist(L, S) :- conc117(_, T, L), conc117(S, _, T). ''' + +test_cases = [ + ('sublist([], X)', + [{'X': '[]'}]), + ('sublist([e, h, A, B, C], [f, a, e])', + [{'A': 'f', 'B': 'a', 'C': 'e'}]), + ('sublist([e, A, l, B], [r, C, k])', + [{'A': 'r', 'B': 'k', 'C': 'l'}]), + ('setof(L, sublist([a, b, c], L), X)', + [{'X': '[[], [a], [a, b], [a, b, c], [b], [b, c], [c]]'}]), +] + +def test(program, solved_problems): + code = (program + '\n' + + server.problems.solutions_for_problems('prolog', solved_problems)) + + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code, timeout=1.0) + if engine_id is not None and 'error' not in map(itemgetter(0), output): + # Engine successfully created, and no syntax error in program. + 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): + # TODO + return [] diff --git a/prolog/problems/lists/sum_2/common.py b/prolog/problems/lists/sum_2/common.py index 9c17e9c..2a17157 100644 --- a/prolog/problems/lists/sum_2/common.py +++ b/prolog/problems/lists/sum_2/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 118 number = 25 visible = True @@ -11,3 +15,39 @@ sum([H|T], Sum) :- sum(T, SumT), Sum is SumT + H. ''' + +test_cases = [ + ('sum([], X)', + [{'X': '0'}]), + ('sum([8, 3, 5, 1], X)', + [{'X': '17'}]), + ('sum([2, -3, 5, 0], X)', + [{'X': '4'}]), +] + +def test(program, solved_problems): + code = (program + '\n' + + server.problems.solutions_for_problems('prolog', solved_problems)) + + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code, timeout=1.0) + if engine_id is not None and 'error' not in map(itemgetter(0), output): + # Engine successfully created, and no syntax error in program. + 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): + # TODO + return [] |