diff options
author | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2016-03-17 14:19:45 +0100 |
---|---|---|
committer | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2016-03-17 14:19:45 +0100 |
commit | cf4e59c9d6bce337514fc7d77120baced0d6d7bc (patch) | |
tree | 33f7cd7d3ae9595ad548427f3a718f5918935e53 /prolog/problems/lists_advanced | |
parent | a9416de1d2bde1b2327928717d00d262160912af (diff) |
Prolog: split lists exercises into two groups
Diffstat (limited to 'prolog/problems/lists_advanced')
36 files changed, 909 insertions, 0 deletions
diff --git a/prolog/problems/lists_advanced/common.py b/prolog/problems/lists_advanced/common.py new file mode 100644 index 0000000..d4e77fa --- /dev/null +++ b/prolog/problems/lists_advanced/common.py @@ -0,0 +1,2 @@ +id = 1000 +number = 3 diff --git a/prolog/problems/lists_advanced/count_3/common.py b/prolog/problems/lists_advanced/count_3/common.py new file mode 100644 index 0000000..2953f06 --- /dev/null +++ b/prolog/problems/lists_advanced/count_3/common.py @@ -0,0 +1,54 @@ +# coding=utf-8 + +from operator import itemgetter +import prolog.engine +import server.problems + +id = 120 +number = 27 +visible = False +facts = None + +solution = '''\ +count(_, [], 0). +count(X, [X|T], N) :- + count(X, T, NT), + N is NT + 1. +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(code, aux_code): + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code+aux_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: + if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0): + n_correct += 1 + except socket.timeout: + pass + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return n_correct, len(test_cases), hints + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/lists_advanced/count_3/en.py b/prolog/problems/lists_advanced/count_3/en.py new file mode 100644 index 0000000..dd13bc0 --- /dev/null +++ b/prolog/problems/lists_advanced/count_3/en.py @@ -0,0 +1,13 @@ +# coding=utf-8 + +name = 'count/3' +slug = 'find the number of occurrences of an element in list' + +description = '''\ +<p><code>count(X, L, N)</code>: <code>N</code> is the number of times the element <code>X</code> appears in the list <code>L</code>.</p> +<pre> +?- count(1, [1,2,1,3,1], N). + N = 3. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/count_3/sl.py b/prolog/problems/lists_advanced/count_3/sl.py new file mode 100644 index 0000000..98cd6f8 --- /dev/null +++ b/prolog/problems/lists_advanced/count_3/sl.py @@ -0,0 +1,13 @@ +# coding=utf-8 + +name = 'count/3' +slug = 'Preštej kolikokrat se element pojavi v seznamu' + +description = '''\ +<p><code>count(X, L, N)</code>: <code>N</code> je število kolikokrat se element <code>X</code> pojavi v seznamu <code>L</code>.</p> +<pre> +?- count(1, [1,2,1,3,1], N). + N = 3. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/en.py b/prolog/problems/lists_advanced/en.py new file mode 100644 index 0000000..e7ab5b1 --- /dev/null +++ b/prolog/problems/lists_advanced/en.py @@ -0,0 +1,5 @@ +name = 'Lists and arithmetic' +description = '''\ +<p> +Lists and arithmetic. +</p>''' diff --git a/prolog/problems/lists_advanced/evenlen_1_+_oddlen_1/common.py b/prolog/problems/lists_advanced/evenlen_1_+_oddlen_1/common.py new file mode 100644 index 0000000..1236551 --- /dev/null +++ b/prolog/problems/lists_advanced/evenlen_1_+_oddlen_1/common.py @@ -0,0 +1,56 @@ +# coding=utf-8 + +from operator import itemgetter +import prolog.engine +import server.problems + +id = 116 +number = 23 +visible = False +facts = None + +solution = '''\ +evenlen([]). +evenlen([_,_|T]) :- + evenlen(T). + +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(code, aux_code): + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code+aux_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: + if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0): + n_correct += 1 + except socket.timeout: + pass + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return n_correct, len(test_cases), hints + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/lists_advanced/evenlen_1_+_oddlen_1/en.py b/prolog/problems/lists_advanced/evenlen_1_+_oddlen_1/en.py new file mode 100644 index 0000000..a01a41b --- /dev/null +++ b/prolog/problems/lists_advanced/evenlen_1_+_oddlen_1/en.py @@ -0,0 +1,18 @@ +# coding=utf-8 + +name = 'evenlen/1 + oddlen/1' +slug = 'check if the length of a list is even or odd' + +description = '''\ +<p><code>evenlen(L)</code>: the list <code>L</code> has an even number of elements.<br /> +<code>oddlen(L)</code>: the list <code>L</code> has an odd number of elements.</p> +<pre> +?- oddlen([1,2,3,4,5]). + true. +?- oddlen([1,2,3,4]). + false. +?- evenlen([1,2,3,4]). + true. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/evenlen_1_+_oddlen_1/sl.py b/prolog/problems/lists_advanced/evenlen_1_+_oddlen_1/sl.py new file mode 100644 index 0000000..4ca44c2 --- /dev/null +++ b/prolog/problems/lists_advanced/evenlen_1_+_oddlen_1/sl.py @@ -0,0 +1,18 @@ +# coding=utf-8 + +name = 'evenlen/1 + oddlen/1' +slug = 'Brez aritmetike preveri, če je seznam sode ali lihe dolžine' + +description = '''\ +<p><code>evenlen(L)</code>: seznam <code>L</code> ima sodo število elementov.<br /> +<code>oddlen(L)</code>: seznam <code>L</code> ima liho število elementov.</p> +<pre> +?- oddlen([1,2,3,4,5]). + true. +?- oddlen([1,2,3,4]). + false. +?- evenlen([1,2,3,4]). + true. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/len_2/common.py b/prolog/problems/lists_advanced/len_2/common.py new file mode 100644 index 0000000..93a1faa --- /dev/null +++ b/prolog/problems/lists_advanced/len_2/common.py @@ -0,0 +1,49 @@ +# coding=utf-8 + +from operator import itemgetter +import prolog.engine +import server.problems + +id = 119 +number = 26 +visible = False +facts = None + +solution = '''\ +len([], 0). +len([_|T], Len) :- + len(T, LenT), + Len is LenT + 1. +''' + +test_cases = [ + ('len([8, 3, 5, 1], X)', + [{'X': '4'}]), + ('len([2, -3, 5, 0, 3], X)', + [{'X': '5'}]), + ('len([], X)', + [{'X': '0'}]), +] + +def test(code, aux_code): + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code+aux_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: + if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0): + n_correct += 1 + except socket.timeout: + pass + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return n_correct, len(test_cases), hints + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/lists_advanced/len_2/en.py b/prolog/problems/lists_advanced/len_2/en.py new file mode 100644 index 0000000..3ca963e --- /dev/null +++ b/prolog/problems/lists_advanced/len_2/en.py @@ -0,0 +1,13 @@ +# coding=utf-8 + +name = 'len/2' +slug = 'find the length of a list' + +description = '''\ +<p><code>len(L, Len)</code>: <code>Len</code> is the length of the list <code>L</code>.</p> +<pre> +?- len([1,2,3], Len). + Len = 3. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/len_2/sl.py b/prolog/problems/lists_advanced/len_2/sl.py new file mode 100644 index 0000000..0e98272 --- /dev/null +++ b/prolog/problems/lists_advanced/len_2/sl.py @@ -0,0 +1,13 @@ +# coding=utf-8 + +name = 'len/2' +slug = 'Izračunaj dolžino danega seznama' + +description = '''\ +<p><code>len(L, Len)</code>: <code>Len</code> je dolžina seznama <code>L</code>.</p> +<pre> +?- len([1,2,3], Len). + Len = 3. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/max_2/common.py b/prolog/problems/lists_advanced/max_2/common.py new file mode 100644 index 0000000..92cf793 --- /dev/null +++ b/prolog/problems/lists_advanced/max_2/common.py @@ -0,0 +1,53 @@ +# coding=utf-8 + +from operator import itemgetter +import prolog.engine +import server.problems + +id = 109 +number = 16 +visible = False +facts = None + +solution = '''\ +max([X], X). +max([H|T], Max):- + max(T, Max1), + ( H > Max1, Max is H + ; + 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(code, aux_code): + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code+aux_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: + if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0): + n_correct += 1 + except socket.timeout: + pass + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return n_correct, len(test_cases), hints + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/lists_advanced/max_2/en.py b/prolog/problems/lists_advanced/max_2/en.py new file mode 100644 index 0000000..c4bc7e2 --- /dev/null +++ b/prolog/problems/lists_advanced/max_2/en.py @@ -0,0 +1,15 @@ +# coding=utf-8 + +name = 'max/2' +slug = 'find the largest element in list' + +description = '''\ +<p><code>max(L, Max)</code>: <code>Max</code> is the largest value in the list <code>L</code>.</p> +<pre> +?- max([5,4,1,6], M). + M = 6. +?- max([3,2,2], M). + M = 3. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/max_2/sl.py b/prolog/problems/lists_advanced/max_2/sl.py new file mode 100644 index 0000000..7a0d07a --- /dev/null +++ b/prolog/problems/lists_advanced/max_2/sl.py @@ -0,0 +1,15 @@ +# coding=utf-8 + +name = 'max/2' +slug = 'Poišči največji element v danem seznamu' + +description = '''\ +<p><code>max(L, Max)</code>: <code>Max</code> je največji element v seznamu <code>L</code>.</p> +<pre> +?- max([5,4,1,6], M). + M = 6. +?- max([3,2,2], M). + M = 3. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/min_2/common.py b/prolog/problems/lists_advanced/min_2/common.py new file mode 100644 index 0000000..213da5a --- /dev/null +++ b/prolog/problems/lists_advanced/min_2/common.py @@ -0,0 +1,53 @@ +# coding=utf-8 + +from operator import itemgetter +import prolog.engine +import server.problems + +id = 108 +number = 15 +visible = False +facts = None + +solution = '''\ +min([X], X). +min([H|T], Min):- + min(T, Min1), + ( H < Min1, Min is H + ; + 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(code, aux_code): + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code+aux_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: + if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0): + n_correct += 1 + except socket.timeout: + pass + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return n_correct, len(test_cases), hints + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/lists_advanced/min_2/en.py b/prolog/problems/lists_advanced/min_2/en.py new file mode 100644 index 0000000..18f45a6 --- /dev/null +++ b/prolog/problems/lists_advanced/min_2/en.py @@ -0,0 +1,15 @@ +# coding=utf-8 + +name = 'min/2' +slug = 'find the smallest element' + +description = '''\ +<p><code>min(L, Min)</code>: <code>Min</code> is the smallest value in the list <code>L</code>.</p> +<pre> +?- min([5,4,1,6], M). + M = 1. +?- min([3,2,2], M). + M = 2. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/min_2/sl.py b/prolog/problems/lists_advanced/min_2/sl.py new file mode 100644 index 0000000..703a05e --- /dev/null +++ b/prolog/problems/lists_advanced/min_2/sl.py @@ -0,0 +1,15 @@ +# coding=utf-8 + +name = 'min/2' +slug = 'Poišči najmanjši element v danem seznamu' + +description = '''\ +<p><code>min(L, Min)</code>: <code>Min</code> je najmanjši element v seznamu <code>L</code>.</p> +<pre> +?- min([5,4,1,6], M). + M = 1. +?- min([3,2,2], M). + M = 2. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/palindrome_1/common.py b/prolog/problems/lists_advanced/palindrome_1/common.py new file mode 100644 index 0000000..e9960c6 --- /dev/null +++ b/prolog/problems/lists_advanced/palindrome_1/common.py @@ -0,0 +1,54 @@ +# coding=utf-8 + +from operator import itemgetter +import prolog.engine +import server.problems + +id = 112 +number = 19 +visible = False +facts = None + +solution = '''\ +conc112([], L, L). +conc112([H|T], L2, [H|L]) :- + conc112(T, L2, L). +rev112([], []). +rev112([H|T], R):- + rev112(T, R1), + conc112(R1, [H], 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(code, aux_code): + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code+aux_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: + if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0): + n_correct += 1 + except socket.timeout: + pass + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return n_correct, len(test_cases), hints + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/lists_advanced/palindrome_1/en.py b/prolog/problems/lists_advanced/palindrome_1/en.py new file mode 100644 index 0000000..48ee8ac --- /dev/null +++ b/prolog/problems/lists_advanced/palindrome_1/en.py @@ -0,0 +1,15 @@ +# coding=utf-8 + +name = 'palindrome/1' +slug = 'check if list is a palindrome' + +description = '''\ +<p><code>palindrome(L)</code>: the elements of list <code>L</code> are the same when read from the front or back of the list.</p> +<pre> +?- palindrome([1,2,3,2,1]). + true. +?- palindrome([1,2,3]). + false. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/palindrome_1/sl.py b/prolog/problems/lists_advanced/palindrome_1/sl.py new file mode 100644 index 0000000..ad67d3f --- /dev/null +++ b/prolog/problems/lists_advanced/palindrome_1/sl.py @@ -0,0 +1,15 @@ +# coding=utf-8 + +name = 'palindrome/1' +slug = 'Preveri, če elementi seznama tvorijo palindrom' + +description = '''\ +<p><code>palindrome(L)</code>: Elementi seznama <code>L</code> se preberejo v istem vrstnem redu z začetka in s konca seznama.</p> +<pre> +?- palindrome([1,2,3,2,1]). + true. +?- palindrome([1,2,3]). + false. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/rev_2/common.py b/prolog/problems/lists_advanced/rev_2/common.py new file mode 100644 index 0000000..408389a --- /dev/null +++ b/prolog/problems/lists_advanced/rev_2/common.py @@ -0,0 +1,52 @@ +# coding=utf-8 + +from operator import itemgetter +import prolog.engine +import server.problems + +id = 111 +number = 18 +visible = False +facts = None + +solution = '''\ +conc111([], L, L). +conc111([H|T], L2, [H|L]) :- + conc111(T, L2, L). +rev([], []). +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(code, aux_code): + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code+aux_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: + if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0): + n_correct += 1 + except socket.timeout: + pass + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return n_correct, len(test_cases), hints + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/lists_advanced/rev_2/en.py b/prolog/problems/lists_advanced/rev_2/en.py new file mode 100644 index 0000000..3ae126e --- /dev/null +++ b/prolog/problems/lists_advanced/rev_2/en.py @@ -0,0 +1,15 @@ +# coding=utf-8 + +name = 'rev/2' +slug = 'reverse a list' + +description = '''\ +<p><code>rev(L1, L2)</code>: the list <code>L2</code> is obtained from <code>L1</code> by reversing the order of the elements.</p> +<pre> +?- rev([1,2,3], X). + X = [3,2,1]. +?- rev([], X). + X = []. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/rev_2/sl.py b/prolog/problems/lists_advanced/rev_2/sl.py new file mode 100644 index 0000000..ab58a1c --- /dev/null +++ b/prolog/problems/lists_advanced/rev_2/sl.py @@ -0,0 +1,15 @@ +# coding=utf-8 + +name = 'rev/2' +slug = 'Obrni seznam' + +description = '''\ +<p><code>rev(L1, L2)</code>: seznam <code>L2</code> ima elemente v obratnem vrstnem redu kot seznam <code>L1</code>.</p> +<pre> +?- rev([1,2,3], X). + X = [3,2,1]. +?- rev([], X). + X = []. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/shiftleft_2/common.py b/prolog/problems/lists_advanced/shiftleft_2/common.py new file mode 100644 index 0000000..13db28f --- /dev/null +++ b/prolog/problems/lists_advanced/shiftleft_2/common.py @@ -0,0 +1,50 @@ +# coding=utf-8 + +from operator import itemgetter +import prolog.engine +import server.problems + +id = 113 +number = 20 +visible = False +facts = None + +solution = '''\ +conc113([], L, L). +conc113([H|T], L2, [H|L]) :- + conc113(T, L2, 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(code, aux_code): + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code+aux_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: + if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0): + n_correct += 1 + except socket.timeout: + pass + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return n_correct, len(test_cases), hints + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/lists_advanced/shiftleft_2/en.py b/prolog/problems/lists_advanced/shiftleft_2/en.py new file mode 100644 index 0000000..a8dd5f0 --- /dev/null +++ b/prolog/problems/lists_advanced/shiftleft_2/en.py @@ -0,0 +1,13 @@ +# coding=utf-8 + +name = 'shiftleft/2' +slug = 'shift a list left' + +description = '''\ +<p><code>shiftleft(L1, L2)</code>: the list <code>L2</code> is obtained from L1 by shifting elements to the left by one (circular shift).</p> +<pre> +?- shiftleft([1,2,3,4,5], X). + X = [2,3,4,5,1]. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/shiftleft_2/sl.py b/prolog/problems/lists_advanced/shiftleft_2/sl.py new file mode 100644 index 0000000..ae412a6 --- /dev/null +++ b/prolog/problems/lists_advanced/shiftleft_2/sl.py @@ -0,0 +1,13 @@ +# coding=utf-8 + +name = 'shiftleft/2' +slug = 'Premakni elemente seznama za eno mesto v levo' + +description = '''\ +<p><code>shiftleft(L1, L2)</code>: elemente v seznamu <code>L1</code> za eno mesto premaknemo v levo ("circular shift").</p> +<pre> +?- shiftleft([1,2,3,4,5], X). + X = [2,3,4,5,1]. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/shiftright_2/common.py b/prolog/problems/lists_advanced/shiftright_2/common.py new file mode 100644 index 0000000..d8a4c9f --- /dev/null +++ b/prolog/problems/lists_advanced/shiftright_2/common.py @@ -0,0 +1,50 @@ +# coding=utf-8 + +from operator import itemgetter +import prolog.engine +import server.problems + +id = 114 +number = 21 +visible = False +facts = None + +solution = '''\ +conc114([], L, L). +conc114([H|T], L2, [H|L]) :- + conc114(T, L2, 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(code, aux_code): + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code+aux_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: + if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0): + n_correct += 1 + except socket.timeout: + pass + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return n_correct, len(test_cases), hints + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/lists_advanced/shiftright_2/en.py b/prolog/problems/lists_advanced/shiftright_2/en.py new file mode 100644 index 0000000..63e11cf --- /dev/null +++ b/prolog/problems/lists_advanced/shiftright_2/en.py @@ -0,0 +1,13 @@ +# coding=utf-8 + +name = 'shiftright/2' +slug = 'shift a list right' + +description = '''\ +<p><code>shiftright(L1, L2)</code>: the list <code>L2</code> is obtained from L1 by shifting elements to the right by one (circular shift).</p> +<pre> +?- shiftright([1,2,3,4,5], X). + X = [5,1,2,3,4]. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/shiftright_2/sl.py b/prolog/problems/lists_advanced/shiftright_2/sl.py new file mode 100644 index 0000000..62fca8d --- /dev/null +++ b/prolog/problems/lists_advanced/shiftright_2/sl.py @@ -0,0 +1,13 @@ +# coding=utf-8 + +name = 'shiftright/2' +slug = 'Premakni elemente seznama za eno mesto v desno' + +description = '''\ +<p><code>shiftright(L1, L2)</code>: elemente v seznamu <code>L1</code> za eno mesto premaknemo v desno ("circular shift").</p> +<pre> +?- shiftright([1,2,3,4,5], X). + X = [5,1,2,3,4]. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/sl.py b/prolog/problems/lists_advanced/sl.py new file mode 100644 index 0000000..51b4838 --- /dev/null +++ b/prolog/problems/lists_advanced/sl.py @@ -0,0 +1,5 @@ +name = 'Seznami in aritmetika' +description = '''\ +<p> +Seznami in aritmetika. +</p>''' diff --git a/prolog/problems/lists_advanced/sublist_2/common.py b/prolog/problems/lists_advanced/sublist_2/common.py new file mode 100644 index 0000000..6187ebc --- /dev/null +++ b/prolog/problems/lists_advanced/sublist_2/common.py @@ -0,0 +1,53 @@ +# coding=utf-8 + +from operator import itemgetter +import prolog.engine +import server.problems + +id = 117 +number = 24 +visible = False +facts = None + +solution = '''\ +conc117([], L, L). +conc117([H|T], L2, [H|L]) :- + conc117(T, L2, L). +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(code, aux_code): + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code+aux_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: + if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0): + n_correct += 1 + except socket.timeout: + pass + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return n_correct, len(test_cases), hints + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/lists_advanced/sublist_2/en.py b/prolog/problems/lists_advanced/sublist_2/en.py new file mode 100644 index 0000000..2697d9b --- /dev/null +++ b/prolog/problems/lists_advanced/sublist_2/en.py @@ -0,0 +1,19 @@ +# coding=utf-8 + +name = 'sublist/2' +slug = 'generate sublists of a list' + +description = '''\ +<p><code>sublist(L, SL)</code>: <code>SL</code> is a continuous sublist of the list <code>L</code>.</p> +<pre> +?- sublist([1,2,3], X). + X = [] ; + X = [1] ; + X = [1,2] ; + X = [1,2,3] ; + X = [2] ; + X = [2,3] ; + X = [3]. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/sublist_2/sl.py b/prolog/problems/lists_advanced/sublist_2/sl.py new file mode 100644 index 0000000..cc42219 --- /dev/null +++ b/prolog/problems/lists_advanced/sublist_2/sl.py @@ -0,0 +1,19 @@ +# coding=utf-8 + +name = 'sublist/2' +slug = 'Generiraj vse podsezname danega seznama' + +description = '''\ +<p><code>sublist(L, SL)</code>: <code>SL</code> je podseznam seznama <code>L</code>. Predikat naj vrne vse možne podsezname, enega po enega.</p> +<pre> +?- sublist([1,2,3], X). + X = [] ; + X = [1] ; + X = [1,2] ; + X = [1,2,3] ; + X = [2] ; + X = [2,3] ; + X = [3]. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/sum_2/common.py b/prolog/problems/lists_advanced/sum_2/common.py new file mode 100644 index 0000000..000b4b0 --- /dev/null +++ b/prolog/problems/lists_advanced/sum_2/common.py @@ -0,0 +1,49 @@ +# coding=utf-8 + +from operator import itemgetter +import prolog.engine +import server.problems + +id = 118 +number = 25 +visible = False +facts = None + +solution = '''\ +sum([], 0). +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(code, aux_code): + n_correct = 0 + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code+aux_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: + if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0): + n_correct += 1 + except socket.timeout: + pass + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return n_correct, len(test_cases), hints + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/lists_advanced/sum_2/en.py b/prolog/problems/lists_advanced/sum_2/en.py new file mode 100644 index 0000000..b4dab45 --- /dev/null +++ b/prolog/problems/lists_advanced/sum_2/en.py @@ -0,0 +1,13 @@ +# coding=utf-8 + +name = 'sum/2' +slug = 'find the sum of all elements in list' + +description = '''\ +<p><code>sum(L, Sum)</code>: <code>Sum</code> is the sum of all elements in the list <code>L</code>.</p> +<pre> +?- sum([1,2,3], Sum). + Sum = 6. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists_advanced/sum_2/sl.py b/prolog/problems/lists_advanced/sum_2/sl.py new file mode 100644 index 0000000..cc0423b --- /dev/null +++ b/prolog/problems/lists_advanced/sum_2/sl.py @@ -0,0 +1,13 @@ +# coding=utf-8 + +name = 'sum/2' +slug = 'Izračunaj vsoto elementov v seznamu' + +description = '''\ +<p><code>sum(L, Sum)</code>: <code>Sum</code> je vsota vseh elementov v seznamu <code>L</code>.</p> +<pre> +?- sum([1,2,3], Sum). + Sum = 6. +</pre>''' + +hint = {} |