diff options
Diffstat (limited to 'prolog/problems/sets')
-rw-r--r-- | prolog/problems/sets/diff_3/common.py | 19 | ||||
-rw-r--r-- | prolog/problems/sets/diff_3/en.py | 12 | ||||
-rw-r--r-- | prolog/problems/sets/intersect_3/common.py | 18 | ||||
-rw-r--r-- | prolog/problems/sets/intersect_3/en.py | 12 | ||||
-rw-r--r-- | prolog/problems/sets/is_subset_2/common.py | 16 | ||||
-rw-r--r-- | prolog/problems/sets/is_subset_2/en.py | 14 | ||||
-rw-r--r-- | prolog/problems/sets/is_superset_2/common.py | 16 | ||||
-rw-r--r-- | prolog/problems/sets/is_superset_2/en.py | 14 | ||||
-rw-r--r-- | prolog/problems/sets/powerset_2/common.py | 16 | ||||
-rw-r--r-- | prolog/problems/sets/powerset_2/en.py | 12 | ||||
-rw-r--r-- | prolog/problems/sets/subset_2/common.py | 13 | ||||
-rw-r--r-- | prolog/problems/sets/subset_2/en.py | 19 | ||||
-rw-r--r-- | prolog/problems/sets/union_3/common.py | 19 | ||||
-rw-r--r-- | prolog/problems/sets/union_3/en.py | 12 |
14 files changed, 212 insertions, 0 deletions
diff --git a/prolog/problems/sets/diff_3/common.py b/prolog/problems/sets/diff_3/common.py new file mode 100644 index 0000000..76b88ed --- /dev/null +++ b/prolog/problems/sets/diff_3/common.py @@ -0,0 +1,19 @@ +id = 130 +group = 'sets' +number = 37 +visible = True +facts = None + +solution = '''\ +memb130(X, [X|_]). +memb130(X, [_|T]) :- + memb130(X, T). + +diff([], _, []). +diff([H|T], S2, [H|D]) :- + \+ memb130(H, S2), + diff(T, S2, D). +diff([H|T], S2, D) :- + memb130(H, S2), + diff(T, S2, D). +''' diff --git a/prolog/problems/sets/diff_3/en.py b/prolog/problems/sets/diff_3/en.py new file mode 100644 index 0000000..a2fc55d --- /dev/null +++ b/prolog/problems/sets/diff_3/en.py @@ -0,0 +1,12 @@ +id = 130 +name = 'diff/3' +slug = 'find the difference of two sets' + +description = '''\ +<p><code>diff(S1, S2, D)</code>: the list <code>D</code> contains all elements of <code>S1</code> that don't appear in <code>S2</code>, with no duplicates.</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 new file mode 100644 index 0000000..ffa4f89 --- /dev/null +++ b/prolog/problems/sets/intersect_3/common.py @@ -0,0 +1,18 @@ +id = 129 +group = 'sets' +number = 36 +visible = True +facts = None + +solution = '''\ +memb129(X, [X|_]). +memb129(X, [_|T]) :- + memb129(X, T). + +intersect([], _, []). +intersect([H|T], S2, [H|I]) :- + memb129(H, S2), !, + intersect(T, S2, I). +intersect([_|T], S2, I):- + intersect(T, S2, I). +''' diff --git a/prolog/problems/sets/intersect_3/en.py b/prolog/problems/sets/intersect_3/en.py new file mode 100644 index 0000000..b4296e4 --- /dev/null +++ b/prolog/problems/sets/intersect_3/en.py @@ -0,0 +1,12 @@ +id = 129 +name = 'intersect/3' +slug = 'find the intersection of two sets' + +description = '''\ +<p><code>intersect(S1, S2, I)</code>: the list <code>I</code> contains every element that appears in both <code>S1</code> and <code>S2</code>, with no duplicates.</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 new file mode 100644 index 0000000..5b365ee --- /dev/null +++ b/prolog/problems/sets/is_subset_2/common.py @@ -0,0 +1,16 @@ +id = 132 +group = 'sets' +number = 39 +visible = True +facts = None + +solution = '''\ +memb132(X, [X|_]). +memb132(X, [_|T]) :- + memb132(X, T). + +is_subset([], _). +is_subset([H|T], S2) :- + memb132(H, S2), + is_subset(T, S2). +''' diff --git a/prolog/problems/sets/is_subset_2/en.py b/prolog/problems/sets/is_subset_2/en.py new file mode 100644 index 0000000..ae2c113 --- /dev/null +++ b/prolog/problems/sets/is_subset_2/en.py @@ -0,0 +1,14 @@ +id = 132 +name = 'is_subset/2' +slug = 'check if one set is a subset of another' + +description = '''\ +<p><code>is_subset(S1, S2)</code>: the set <code>S1</code> is a subset of <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 new file mode 100644 index 0000000..364c513 --- /dev/null +++ b/prolog/problems/sets/is_superset_2/common.py @@ -0,0 +1,16 @@ +id = 131 +group = 'sets' +number = 38 +visible = True +facts = None + +solution = '''\ +memb131(X, [X|_]). +memb131(X, [_|T]) :- + memb131(X, T). + +is_superset(_, []). +is_superset(S1, [H|T]) :- + memb131(H, S1), + is_superset(S1, T). +''' diff --git a/prolog/problems/sets/is_superset_2/en.py b/prolog/problems/sets/is_superset_2/en.py new file mode 100644 index 0000000..218c0c8 --- /dev/null +++ b/prolog/problems/sets/is_superset_2/en.py @@ -0,0 +1,14 @@ +id = 131 +name = 'is_superset/2' +slug = 'check if one set is a superset of the other' + +description = '''\ +<p><code>is_superset(S1, S2)</code>: the set <code>S1</code> is a superset (contains all elements) of <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 new file mode 100644 index 0000000..8f3b70e --- /dev/null +++ b/prolog/problems/sets/powerset_2/common.py @@ -0,0 +1,16 @@ +id = 134 +group = 'sets' +number = 41 +visible = True +facts = None + +solution = '''\ +subset134([], []). +subset134([H|T], [H|T1]) :- + subset134(T, T1). +subset134([_|T], T1) :- + subset134(T, T1). + +powerset(Set, PowerSet) :- + findall(S, subset134(Set, S), PowerSet). +''' diff --git a/prolog/problems/sets/powerset_2/en.py b/prolog/problems/sets/powerset_2/en.py new file mode 100644 index 0000000..6471469 --- /dev/null +++ b/prolog/problems/sets/powerset_2/en.py @@ -0,0 +1,12 @@ +id = 134 +name = 'powerset/2' +slug = 'find all subsets of a set' + +description = '''\ +<p><code>powerset(Set, Powerset)</code>: the list <code>Powerset</code> contains all subsets of <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 new file mode 100644 index 0000000..7fbbf49 --- /dev/null +++ b/prolog/problems/sets/subset_2/common.py @@ -0,0 +1,13 @@ +id = 133 +group = 'sets' +number = 40 +visible = True +facts = None + +solution = '''\ +subset([], []). +subset([H|T], [H|T1]) :- + subset(T, T1). +subset([_|T], T1) :- + subset(T, T1). +''' diff --git a/prolog/problems/sets/subset_2/en.py b/prolog/problems/sets/subset_2/en.py new file mode 100644 index 0000000..9d9132b --- /dev/null +++ b/prolog/problems/sets/subset_2/en.py @@ -0,0 +1,19 @@ +id = 133 +name = 'subset/2' +slug = 'generate all subsets of a set' + +description = '''\ +<p><code>subset(Set, Subset)</code>: the set <code>Subset</code> is a subset of <code>Set</code>. This predicate should generate all valid solutions, one by one.</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 new file mode 100644 index 0000000..a219673 --- /dev/null +++ b/prolog/problems/sets/union_3/common.py @@ -0,0 +1,19 @@ +id = 128 +group = 'sets' +number = 35 +visible = True +facts = None + +solution = '''\ +memb128(X, [X|_]). +memb128(X, [_|T]) :- + memb128(X, T). + +union([], S2, S2). +union([H|T], S2, [H|U]) :- + \+ memb128(H, S2), + union(T, S2, U). +union([H|T], S2, U) :- + memb128(H, S2), + union(T, S2, U). +''' diff --git a/prolog/problems/sets/union_3/en.py b/prolog/problems/sets/union_3/en.py new file mode 100644 index 0000000..8257415 --- /dev/null +++ b/prolog/problems/sets/union_3/en.py @@ -0,0 +1,12 @@ +id = 128 +name = 'union/3' +slug = 'find the union of two sets' + +description = '''\ +<p><code>union(S1, S2, U)</code>: the list <code>U</code> contains all elements of <code>S1</code> and <code>S2</code>, with no duplicates.</p> +<pre> + ?- union([1,5,2,3], [3,4,8,2], U). + U = [1,5,3,4,8,2]. +</pre>''' + +hint = {} |