summaryrefslogtreecommitdiff
path: root/prolog/problems/sets
diff options
context:
space:
mode:
Diffstat (limited to 'prolog/problems/sets')
-rw-r--r--prolog/problems/sets/common.py2
-rw-r--r--prolog/problems/sets/diff_3/common.py41
-rw-r--r--prolog/problems/sets/diff_3/sl.py13
-rw-r--r--prolog/problems/sets/intersect_3/common.py41
-rw-r--r--prolog/problems/sets/intersect_3/sl.py13
-rw-r--r--prolog/problems/sets/is_subset_2/common.py37
-rw-r--r--prolog/problems/sets/is_subset_2/sl.py15
-rw-r--r--prolog/problems/sets/is_superset_2/common.py37
-rw-r--r--prolog/problems/sets/is_superset_2/sl.py15
-rw-r--r--prolog/problems/sets/powerset_2/common.py35
-rw-r--r--prolog/problems/sets/powerset_2/sl.py13
-rw-r--r--prolog/problems/sets/subset_2/common.py51
-rw-r--r--prolog/problems/sets/subset_2/sl.py20
-rw-r--r--prolog/problems/sets/union_3/common.py11
-rw-r--r--prolog/problems/sets/union_3/sl.py13
15 files changed, 349 insertions, 8 deletions
diff --git a/prolog/problems/sets/common.py b/prolog/problems/sets/common.py
index 104aa9d..9318010 100644
--- a/prolog/problems/sets/common.py
+++ b/prolog/problems/sets/common.py
@@ -1,2 +1,4 @@
id = 5
number = 4
+
+allowed_groups = ['sets', 'lists']
diff --git a/prolog/problems/sets/diff_3/common.py b/prolog/problems/sets/diff_3/common.py
index fee93b2..bfa3ce6 100644
--- a/prolog/problems/sets/diff_3/common.py
+++ b/prolog/problems/sets/diff_3/common.py
@@ -1,5 +1,9 @@
# coding=utf-8
+from operator import itemgetter
+import prolog.engine
+import server.problems
+
id = 130
number = 37
visible = True
@@ -18,3 +22,40 @@ diff([H|T], S2, D) :-
memb130(H, S2),
diff(T, S2, D).
'''
+
+test_cases = [
+ ('diff([], [t, e, q], X)',
+ [{'X': '[]'}]),
+ ('diff([q, r, t], [], A), msort(A, X)',
+ [{'X': '[q, r, t]'}]),
+ ('diff([1, 2, 3, 8, 4, 6], [6, 0, 3, 4], A), msort(A, X)',
+ [{'X': '[1, 2, 8]'}]),
+ ('diff([1, 2, 3, 8, 7, 4, 6], [7, 8, 6], A), msort(A, X)',
+ [{'X': '[1, 2, 3, 4]'}]),
+ ('diff([1, 8, 2, 3, 7, 5], [7], A), msort(A, X)',
+ [{'X': '[1, 2, 3, 5, 8]'}]),
+ ('diff([t, w, q], [t, e, q, w, b], X)',
+ [{'X': '[]'}]),
+]
+
+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
+ 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/sets/diff_3/sl.py b/prolog/problems/sets/diff_3/sl.py
new file mode 100644
index 0000000..d9b89e7
--- /dev/null
+++ b/prolog/problems/sets/diff_3/sl.py
@@ -0,0 +1,13 @@
+# coding=utf-8
+
+name = 'diff/3'
+slug = 'Poišči razliko dveh množic'
+
+description = '''\
+<p><code>diff(S1, S2, D)</code>: seznam <code>D</code> vsebuje elemente iz <code>S1</code>, ki niso v <code>S2</code>.</p>
+<pre>
+ ?- diff([2,3,5,1,7,9], [3,7,4,5,6], D).
+ D = [2,1,9].
+</pre>'''
+
+hint = {}
diff --git a/prolog/problems/sets/intersect_3/common.py b/prolog/problems/sets/intersect_3/common.py
index 501cdf3..55d2df5 100644
--- a/prolog/problems/sets/intersect_3/common.py
+++ b/prolog/problems/sets/intersect_3/common.py
@@ -1,5 +1,9 @@
# coding=utf-8
+from operator import itemgetter
+import prolog.engine
+import server.problems
+
id = 129
number = 36
visible = True
@@ -17,3 +21,40 @@ intersect([H|T], S2, [H|I]) :-
intersect([_|T], S2, I):-
intersect(T, S2, I).
'''
+
+test_cases = [
+ ('intersect([], [a, b, d], X)',
+ [{'X': '[]'}]),
+ ('intersect([g, h, r], [], X)',
+ [{'X': '[]'}]),
+ ('intersect([2, 5, 4, 7, 1, 9, 8], [0, 2, 5, 6, 7, 9, 1], A), msort(A, X)',
+ [{'X': '[1, 2, 5, 7, 9]'}]),
+ ('intersect([4, 7, 1, 9, 8], [4, 2, 5, 6, 7, 9, 1], A), msort(A, X)',
+ [{'X': '[1, 4, 7, 9]'}]),
+ ('intersect([9, 3, 4, 2, 1, 7], [a, b, d], X)',
+ [{'X': '[]'}]),
+ ('intersect([9, 3, 4, 2, 1, 7], [4, 2, 5, 6, 7, 9, 1], A), msort(A, X)',
+ [{'X': '[1, 2, 4, 7, 9]'}]),
+]
+
+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
+ 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/sets/intersect_3/sl.py b/prolog/problems/sets/intersect_3/sl.py
new file mode 100644
index 0000000..a6af2d5
--- /dev/null
+++ b/prolog/problems/sets/intersect_3/sl.py
@@ -0,0 +1,13 @@
+# coding=utf-8
+
+name = 'intersect/3'
+slug = 'Poišči presek dveh množic'
+
+description = '''\
+<p><code>intersect(S1, S2, I)</code>: seznam <code>I</code> vsebuje presek elementov v <code>S1</code> in <code>S2</code>.</p>
+<pre>
+ ?- intersect([1,5,6,3,4,2], [8,1,5,9,4,3], I).
+ I = [1,5,3,4].
+</pre>'''
+
+hint = {}
diff --git a/prolog/problems/sets/is_subset_2/common.py b/prolog/problems/sets/is_subset_2/common.py
index 4047146..0d78cd6 100644
--- a/prolog/problems/sets/is_subset_2/common.py
+++ b/prolog/problems/sets/is_subset_2/common.py
@@ -1,5 +1,9 @@
# coding=utf-8
+from operator import itemgetter
+import prolog.engine
+import server.problems
+
id = 132
number = 39
visible = True
@@ -15,3 +19,36 @@ is_subset([H|T], S2) :-
memb132(H, S2),
is_subset(T, S2).
'''
+
+test_cases = [
+ ('is_subset(X, [])',
+ [{'X': '[]'}]),
+ ('is_subset([2, 1, 3], [3, 2, 1, 4, 6, 7])',
+ [{}]),
+ ('\+ is_subset([2, 1, 0, 3], [3, 2, 1, 4, 6, 7])',
+ [{}]),
+ ('is_subset([3, 2, 1, 4, 6, 7], [3, 2, 1, 4, 6, 7])',
+ [{}]),
+]
+
+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
+ 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/sets/is_subset_2/sl.py b/prolog/problems/sets/is_subset_2/sl.py
new file mode 100644
index 0000000..ccec5b0
--- /dev/null
+++ b/prolog/problems/sets/is_subset_2/sl.py
@@ -0,0 +1,15 @@
+# coding=utf-8
+
+name = 'is_subset/2'
+slug = 'Preveri, če je prva množica podmnožica od druge'
+
+description = '''\
+<p><code>is_subset(S1, S2)</code>: množica <code>S1</code> je podmnožica od <code>S2</code>.</p>
+<pre>
+ ?- is_subset([2,1,3,5,0], [3,2,1,4,5,9]).
+ false.
+ ?- is_subset([2,1,3,5], [3,2,1,4,5,9]).
+ true.
+</pre>'''
+
+hint = {}
diff --git a/prolog/problems/sets/is_superset_2/common.py b/prolog/problems/sets/is_superset_2/common.py
index 86e5dc7..59f2fda 100644
--- a/prolog/problems/sets/is_superset_2/common.py
+++ b/prolog/problems/sets/is_superset_2/common.py
@@ -1,5 +1,9 @@
# coding=utf-8
+from operator import itemgetter
+import prolog.engine
+import server.problems
+
id = 131
number = 38
visible = True
@@ -15,3 +19,36 @@ is_superset(S1, [H|T]) :-
memb131(H, S1),
is_superset(S1, T).
'''
+
+test_cases = [
+ ('is_superset([], X)',
+ [{'X': '[]'}]),
+ ('is_superset([3, 2, 1, 4, 6, 7], [2, 1, 3])',
+ [{}]),
+ ('\+ is_superset([3, 2, 1, 4, 6, 7], [2, 1, 0, 3])',
+ [{}]),
+ ('is_superset([3, 2, 1, 4, 6, 7], [3, 2, 1, 4, 6, 7])',
+ [{}]),
+]
+
+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
+ 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/sets/is_superset_2/sl.py b/prolog/problems/sets/is_superset_2/sl.py
new file mode 100644
index 0000000..77d0d9d
--- /dev/null
+++ b/prolog/problems/sets/is_superset_2/sl.py
@@ -0,0 +1,15 @@
+# coding=utf-8
+
+name = 'is_superset/2'
+slug = 'Preveri, če je prva množica nadmnožica od druge'
+
+description = '''\
+<p><code>is_superset(S1, S2)</code>: množica <code>S1</code> je nadmnožica (vsebuje vse elemente) od <code>S2</code>.</p>
+<pre>
+ ?- is_superset([3,2,1,4,5,9], [2,1,3,5]).
+ true.
+ ?- is_superset([3,2,1,4,5,9], [2,1,3,5,0]).
+ false.
+</pre>'''
+
+hint = {}
diff --git a/prolog/problems/sets/powerset_2/common.py b/prolog/problems/sets/powerset_2/common.py
index c825430..e97603c 100644
--- a/prolog/problems/sets/powerset_2/common.py
+++ b/prolog/problems/sets/powerset_2/common.py
@@ -1,5 +1,9 @@
# coding=utf-8
+from operator import itemgetter
+import prolog.engine
+import server.problems
+
id = 134
number = 41
visible = True
@@ -15,3 +19,34 @@ subset134([_|T], T1) :-
powerset(Set, PowerSet) :-
findall(S, subset134(Set, S), PowerSet).
'''
+
+test_cases = [
+ ('powerset([], X)',
+ [{'X': '[[]]'}]),
+ ('powerset([1, 2], A), length(A, 4), lists:flatten(A, Y), msort(Y, X)',
+ [{'X': '[1, 1, 2, 2]'}]),
+ ('powerset([1, 2, 3], A), length(A, 8), lists:flatten(A, Y), msort(Y, X)',
+ [{'X': '[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]'}]),
+]
+
+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
+ 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/sets/powerset_2/sl.py b/prolog/problems/sets/powerset_2/sl.py
new file mode 100644
index 0000000..a782dee
--- /dev/null
+++ b/prolog/problems/sets/powerset_2/sl.py
@@ -0,0 +1,13 @@
+# coding=utf-8
+
+name = 'powerset/2'
+slug = 'Poišči potenčno množico od dane množice'
+
+description = '''\
+<p><code>powerset(Set, Powerset)</code>: množica <code>Powerset</code> vsebuje vse podmnožice od množice <code>Set</code>.</p>
+<pre>
+ ?- powerset([1,2,3], L).
+ L = [[1,2,3],[1,2],[1,3],[1],[2,3],[2],[3],[]].
+</pre>'''
+
+hint = {}
diff --git a/prolog/problems/sets/subset_2/common.py b/prolog/problems/sets/subset_2/common.py
index 130d87b..f7d87d2 100644
--- a/prolog/problems/sets/subset_2/common.py
+++ b/prolog/problems/sets/subset_2/common.py
@@ -1,5 +1,9 @@
# coding=utf-8
+from operator import itemgetter
+import prolog.engine
+import server.problems
+
id = 133
number = 40
visible = True
@@ -12,3 +16,50 @@ subset([H|T], [H|T1]) :-
subset([_|T], T1) :-
subset(T, T1).
'''
+
+test_cases = [
+ ('subset([], X)',
+ [{'X': '[]'}]),
+ ('subset([1, 3, 5, 7], A), msort(A, X)',
+ [{'X': '[]'}, {'X': '[7]'}, {'X': '[5]'}, {'X': '[5, 7]'}, {'X': '[3]'},
+ {'X': '[3, 7]'}, {'X': '[3, 5]'}, {'X': '[3, 5, 7]'}, {'X': '[1]'},
+ {'X': '[1, 7]'}, {'X': '[1, 5]'}, {'X': '[1, 5, 7]'}, {'X': '[1, 3]'},
+ {'X': '[1, 3, 7]'}, {'X': '[1, 3, 5]'}, {'X': '[1, 3, 5, 7]'}]),
+ ('subset([8, 4, 6, 2], A), msort(A, X)',
+ [{'X': '[]'}, {'X': '[2]'}, {'X': '[6]'}, {'X': '[2, 6]'}, {'X': '[4]'},
+ {'X': '[2, 4]'}, {'X': '[4, 6]'}, {'X': '[2, 4, 6]'}, {'X': '[8]'},
+ {'X': '[2, 8]'}, {'X': '[6, 8]'}, {'X': '[2, 6, 8]'}, {'X': '[4, 8]'},
+ {'X': '[2, 4, 8]'}, {'X': '[4, 6, 8]'}, {'X': '[2, 4, 6, 8]'}]),
+ ('subset([5, 6, 7, 0, 2], A), msort(A, X)',
+ [{'X': '[]'}, {'X': '[2]'}, {'X': '[0]'}, {'X': '[0, 2]'}, {'X': '[7]'},
+ {'X': '[2, 7]'}, {'X': '[0, 7]'}, {'X': '[0, 2, 7]'}, {'X': '[6]'},
+ {'X': '[2, 6]'}, {'X': '[0, 6]'}, {'X': '[0, 2, 6]'}, {'X': '[6, 7]'},
+ {'X': '[2, 6, 7]'}, {'X': '[0, 6, 7]'}, {'X': '[0, 2, 6, 7]'},
+ {'X': '[5]'}, {'X': '[2, 5]'}, {'X': '[0, 5]'}, {'X': '[0, 2, 5]'},
+ {'X': '[5, 7]'}, {'X': '[2, 5, 7]'}, {'X': '[0, 5, 7]'},
+ {'X': '[0, 2, 5, 7]'}, {'X': '[5, 6]'}, {'X': '[2, 5, 6]'},
+ {'X': '[0, 5, 6]'}, {'X': '[0, 2, 5, 6]'}, {'X': '[5, 6, 7]'},
+ {'X': '[2, 5, 6, 7]'}, {'X': '[0, 5, 6, 7]'}, {'X': '[0, 2, 5, 6, 7]'}]),
+]
+
+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
+ 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/sets/subset_2/sl.py b/prolog/problems/sets/subset_2/sl.py
new file mode 100644
index 0000000..eae4fcf
--- /dev/null
+++ b/prolog/problems/sets/subset_2/sl.py
@@ -0,0 +1,20 @@
+# coding=utf-8
+
+name = 'subset/2'
+slug = 'Generiraj vse podmnožice dane množice'
+
+description = '''\
+<p><code>subset(Set, Subset)</code>: množica <code>Subset</code> je podmnožica od <code>Set</code>. Ta predikat naj, eno po eno, generira vse veljavne podmnožice.</p>
+<pre>
+ ?- subset([1,2,3], SS).
+ SS = [1,2,3] ;
+ SS = [1,2] ;
+ SS = [1,3] ;
+ SS = [1] ;
+ SS = [2,3] ;
+ SS = [2] ;
+ SS = [3] ;
+ SS = [].
+</pre>'''
+
+hint = {}
diff --git a/prolog/problems/sets/union_3/common.py b/prolog/problems/sets/union_3/common.py
index 506913a..ae24943 100644
--- a/prolog/problems/sets/union_3/common.py
+++ b/prolog/problems/sets/union_3/common.py
@@ -34,20 +34,15 @@ test_cases = [
[{'X': '[0, 1, 2, 3, 5, 6, 9]'}]),
]
-def test(program, solved_problems):
- code = (program + '\n' +
- server.problems.solutions_for_problems('prolog', solved_problems))
-
+def test(code, aux_code):
n_correct = 0
engine_id = None
try:
- engine_id, output = prolog.engine.create(code=code, timeout=1.0)
+ 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:
- # 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):
+ if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0):
n_correct += 1
finally:
if engine_id:
diff --git a/prolog/problems/sets/union_3/sl.py b/prolog/problems/sets/union_3/sl.py
new file mode 100644
index 0000000..27d3088
--- /dev/null
+++ b/prolog/problems/sets/union_3/sl.py
@@ -0,0 +1,13 @@
+# coding=utf-8
+
+name = 'union/3'
+slug = 'Poišči unijo dveh množic'
+
+description = '''\
+<p><code>union(S1, S2, U)</code>: seznam <code>U</code> predstavlja unijo elementov v seznamih <code>S1</code> in <code>S2</code>, duplikatov (kot se za množice spodobi) ni.</p>
+<pre>
+ ?- union([1,5,2,3], [3,4,8,2], U).
+ U = [1,5,3,4,8,2].
+</pre>'''
+
+hint = {}