diff options
Diffstat (limited to 'prolog/problems/lists')
36 files changed, 497 insertions, 0 deletions
diff --git a/prolog/problems/lists/conc_3/common.py b/prolog/problems/lists/conc_3/common.py new file mode 100644 index 0000000..29a3919 --- /dev/null +++ b/prolog/problems/lists/conc_3/common.py @@ -0,0 +1,11 @@ +id = 104 +group = 'lists' +number = 12 +visible = True +facts = None + +solution = '''\ +conc([], L, L). +conc([H|T], L2, [H|L]) :- + conc(T, L2, L). +''' diff --git a/prolog/problems/lists/conc_3/en.py b/prolog/problems/lists/conc_3/en.py new file mode 100644 index 0000000..3e127c0 --- /dev/null +++ b/prolog/problems/lists/conc_3/en.py @@ -0,0 +1,14 @@ +id = 104 +name = 'conc/3' +slug = 'concatenate two lists' + +description = '''\ +<p><code>conc(L1, L2, L)</code>: the list <code>L</code> is obtained by appending the elements of <code>L2</code> to <code>L1</code>.</p> +<pre> + ?- conc([1,2], [3,4], X). + X = [1,2,3,4]. + ?- conc(X, [], [1,2,3]). + X = [1,2,3]. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists/count_3/common.py b/prolog/problems/lists/count_3/common.py new file mode 100644 index 0000000..583ad6b --- /dev/null +++ b/prolog/problems/lists/count_3/common.py @@ -0,0 +1,15 @@ +id = 120 +group = 'lists' +number = 27 +visible = True +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). +''' diff --git a/prolog/problems/lists/count_3/en.py b/prolog/problems/lists/count_3/en.py new file mode 100644 index 0000000..799594e --- /dev/null +++ b/prolog/problems/lists/count_3/en.py @@ -0,0 +1,12 @@ +id = 120 +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/del_3/common.py b/prolog/problems/lists/del_3/common.py new file mode 100644 index 0000000..1e99959 --- /dev/null +++ b/prolog/problems/lists/del_3/common.py @@ -0,0 +1,11 @@ +id = 105 +group = 'lists' +number = 11 +visible = True +facts = None + +solution = '''\ +del(X, [X|T], T). +del(X, [Y|T], [Y|L]) :- + del(X, T, L). +''' diff --git a/prolog/problems/lists/del_3/en.py b/prolog/problems/lists/del_3/en.py new file mode 100644 index 0000000..a0f0cd3 --- /dev/null +++ b/prolog/problems/lists/del_3/en.py @@ -0,0 +1,19 @@ +id = 105 +name = 'del/3' +slug = 'delete an element from list' + +description = '''\ +<p><code>del(X, L1, L2)</code>: the list <code>L2</code> is obtained from <code>L1</code> by deleting element <code>X</code>.</p> +<pre> + ?- del(1, [1,2,3], L). + L = [2,3]. + ?- del(2, [1,2,3,2,5], L). + L = [1,3,2,5] ; + L = [1,2,3,5]. + ?- del(X, [1,2,3], L). + X = 1, L = [2,3] ; + X = 2, L = [1,3] ; + X = 3, L = [1,2]. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists/divide_3/common.py b/prolog/problems/lists/divide_3/common.py new file mode 100644 index 0000000..901d2f4 --- /dev/null +++ b/prolog/problems/lists/divide_3/common.py @@ -0,0 +1,12 @@ +id = 115 +group = 'lists' +number = 22 +visible = True +facts = None + +solution = '''\ +divide([], [], []). +divide([X], [X], []). +divide([H1,H2|T], [H1|L1], [H2|L2]) :- + divide(T, L1, L2). +''' diff --git a/prolog/problems/lists/divide_3/en.py b/prolog/problems/lists/divide_3/en.py new file mode 100644 index 0000000..1a8d94e --- /dev/null +++ b/prolog/problems/lists/divide_3/en.py @@ -0,0 +1,14 @@ +id = 115 +name = 'divide/3' +slug = 'split a list into parts of roughly equal length' + +description = '''\ +<p><code>divide(L, L1, L2)</code>: the list <code>L1</code> contains elements at odd positions in <code>L</code>, and the list <code>L2</code> contains the elements at even positions in <code>L</code>.</p> +<pre> + ?- divide([a,b,c,d,e,f,g], X, Y). + X = [a,c,e,g], Y = [b,d,f]. + ?- divide([a,b,c,d,e,f], X, Y). + X = [a,c,e], Y = [b,d,f]. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists/dup_2/common.py b/prolog/problems/lists/dup_2/common.py new file mode 100644 index 0000000..37a0842 --- /dev/null +++ b/prolog/problems/lists/dup_2/common.py @@ -0,0 +1,11 @@ +id = 110 +group = 'lists' +number = 17 +visible = True +facts = None + +solution = '''\ +dup([], []). +dup([H|T], [H,H|TT]) :- + dup(T, TT). +''' diff --git a/prolog/problems/lists/dup_2/en.py b/prolog/problems/lists/dup_2/en.py new file mode 100644 index 0000000..d6aef71 --- /dev/null +++ b/prolog/problems/lists/dup_2/en.py @@ -0,0 +1,14 @@ +id = 110 +name = 'dup/2' +slug = 'duplicate the elements of a list' + +description = '''\ +<p><code>dup(L1, L2)</code>: the list <code>L2</code> is obtained from <code>L1</code> by duplicating every element.</p> +<pre> + ?- dup([1,2], X). + X = [1,1,2,2]. + ?- dup([1,2,3], X). + X = [1,1,2,2,3,3]. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists/evenlen_1_+_oddlen_1/common.py b/prolog/problems/lists/evenlen_1_+_oddlen_1/common.py new file mode 100644 index 0000000..2735965 --- /dev/null +++ b/prolog/problems/lists/evenlen_1_+_oddlen_1/common.py @@ -0,0 +1,15 @@ +id = 116 +group = 'lists' +number = 23 +visible = True +facts = None + +solution = '''\ +evenlen([]). +evenlen([_,_|T]) :- + evenlen(T). + +oddlen([_]). +oddlen([_,_|T]) :- + oddlen(T). +''' diff --git a/prolog/problems/lists/evenlen_1_+_oddlen_1/en.py b/prolog/problems/lists/evenlen_1_+_oddlen_1/en.py new file mode 100644 index 0000000..d1957d1 --- /dev/null +++ b/prolog/problems/lists/evenlen_1_+_oddlen_1/en.py @@ -0,0 +1,17 @@ +id = 116 +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/insert_3/common.py b/prolog/problems/lists/insert_3/common.py new file mode 100644 index 0000000..fa3a937 --- /dev/null +++ b/prolog/problems/lists/insert_3/common.py @@ -0,0 +1,13 @@ +id = 106 +group = 'lists' +number = 13 +visible = True +facts = None + +solution = '''\ +del106(X, [X|T], T). +del106(X, [Y|T], [Y|L]) :- + del106(X, T, L). +insert(X, List, BigList) :- + del106(X, BigList, List). +''' diff --git a/prolog/problems/lists/insert_3/en.py b/prolog/problems/lists/insert_3/en.py new file mode 100644 index 0000000..a4a1128 --- /dev/null +++ b/prolog/problems/lists/insert_3/en.py @@ -0,0 +1,14 @@ +id = 106 +name = 'insert/3' +slug = 'insert an element into list' + +description = '''\ +<p><code>insert(X, L1, L2)</code>: the list <code>L2</code> is obtained from <code>L1</code> by inserting the element <code>X</code> at arbitrary position.</p> +<pre> + ?- insert(1, [2,3], L). + L = [1,2,3] ; + L = [2,1,3] ; + L = [2,3,1]. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists/len_2/common.py b/prolog/problems/lists/len_2/common.py new file mode 100644 index 0000000..62be9cc --- /dev/null +++ b/prolog/problems/lists/len_2/common.py @@ -0,0 +1,12 @@ +id = 119 +group = 'lists' +number = 26 +visible = True +facts = None + +solution = '''\ +len([], 0). +len([_|T], Len) :- + len(T, LenT), + Len is LenT + 1. +''' diff --git a/prolog/problems/lists/len_2/en.py b/prolog/problems/lists/len_2/en.py new file mode 100644 index 0000000..b3e277c --- /dev/null +++ b/prolog/problems/lists/len_2/en.py @@ -0,0 +1,12 @@ +id = 119 +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/max_2/common.py b/prolog/problems/lists/max_2/common.py new file mode 100644 index 0000000..65e5a70 --- /dev/null +++ b/prolog/problems/lists/max_2/common.py @@ -0,0 +1,14 @@ +id = 109 +group = 'lists' +number = 16 +visible = True +facts = None + +solution = '''\ +max([X], X). +max([H|T], Max):- + max(T, Max1), + ( H > Max1, Max is H + ; + H =< Max1, Max is Max1 ). +''' diff --git a/prolog/problems/lists/max_2/en.py b/prolog/problems/lists/max_2/en.py new file mode 100644 index 0000000..dfb4d60 --- /dev/null +++ b/prolog/problems/lists/max_2/en.py @@ -0,0 +1,14 @@ +id = 109 +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/memb_2/common.py b/prolog/problems/lists/memb_2/common.py new file mode 100644 index 0000000..32d9125 --- /dev/null +++ b/prolog/problems/lists/memb_2/common.py @@ -0,0 +1,11 @@ +id = 103 +group = 'lists' +number = 10 +visible = True +facts = None + +solution = '''\ +memb(X, [X|_]). +memb(X, [_|T]) :- + memb(X, T). +''' diff --git a/prolog/problems/lists/memb_2/en.py b/prolog/problems/lists/memb_2/en.py new file mode 100644 index 0000000..fca82b7 --- /dev/null +++ b/prolog/problems/lists/memb_2/en.py @@ -0,0 +1,16 @@ +id = 103 +name = 'memb/2' +slug = 'find elements in list' + +description = '''\ +<p><code>memb(M, L)</code>: <code>M</code> is an element of list <code>L</code>.</p> +<pre> + ?- memb(X, [1,2,3]). + X = 1 ; + X = 2 ; + X = 3. + ?- memb(1, [3,2,X]). + X = 1. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists/min_2/common.py b/prolog/problems/lists/min_2/common.py new file mode 100644 index 0000000..7e9a0bc --- /dev/null +++ b/prolog/problems/lists/min_2/common.py @@ -0,0 +1,14 @@ +id = 108 +group = 'lists' +number = 15 +visible = True +facts = None + +solution = '''\ +min([X], X). +min([H|T], Min):- + min(T, Min1), + ( H < Min1, Min is H + ; + H >= Min1, Min is Min1 ). +''' diff --git a/prolog/problems/lists/min_2/en.py b/prolog/problems/lists/min_2/en.py new file mode 100644 index 0000000..a0a8e74 --- /dev/null +++ b/prolog/problems/lists/min_2/en.py @@ -0,0 +1,14 @@ +id = 108 +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/palindrome_1/common.py b/prolog/problems/lists/palindrome_1/common.py new file mode 100644 index 0000000..8929fd9 --- /dev/null +++ b/prolog/problems/lists/palindrome_1/common.py @@ -0,0 +1,17 @@ +id = 112 +group = 'lists' +number = 19 +visible = True +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). +''' diff --git a/prolog/problems/lists/palindrome_1/en.py b/prolog/problems/lists/palindrome_1/en.py new file mode 100644 index 0000000..c1b8971 --- /dev/null +++ b/prolog/problems/lists/palindrome_1/en.py @@ -0,0 +1,14 @@ +id = 112 +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/permute_2/common.py b/prolog/problems/lists/permute_2/common.py new file mode 100644 index 0000000..8e0386c --- /dev/null +++ b/prolog/problems/lists/permute_2/common.py @@ -0,0 +1,15 @@ +id = 107 +group = 'lists' +number = 14 +visible = True +facts = None + +solution = '''\ +del107(X, [X|T], T). +del107(X, [Y|T], [Y|L]) :- + del107(X, T, L). +permute([], []). +permute(L, [X|P]) :- + del107(X, L, L1), + permute(L1, P). +''' diff --git a/prolog/problems/lists/permute_2/en.py b/prolog/problems/lists/permute_2/en.py new file mode 100644 index 0000000..8898c83 --- /dev/null +++ b/prolog/problems/lists/permute_2/en.py @@ -0,0 +1,17 @@ +id = 107 +name = 'permute/2' +slug = 'generate permutations of a list' + +description = '''\ +<p><code>permute(L1, L2)</code>: the list <code>L2</code> is a permutation of the elements of the list <code>L1</code>.</p> +<pre> + ?- permute([1,2,3], L). + L = [1,2,3] ; + L = [1,3,2] ; + L = [2,1,3] ; + L = [2,3,1] ; + L = [3,1,2] ; + L = [3,2,1]. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists/rev_2/common.py b/prolog/problems/lists/rev_2/common.py new file mode 100644 index 0000000..79c5fca --- /dev/null +++ b/prolog/problems/lists/rev_2/common.py @@ -0,0 +1,15 @@ +id = 111 +group = 'lists' +number = 18 +visible = True +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). +''' diff --git a/prolog/problems/lists/rev_2/en.py b/prolog/problems/lists/rev_2/en.py new file mode 100644 index 0000000..9cacc95 --- /dev/null +++ b/prolog/problems/lists/rev_2/en.py @@ -0,0 +1,14 @@ +id = 111 +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([], X). + X = []. + ?- rev([1,2,3], X). + X = [3,2,1]. +</pre>''' + +hint = {} diff --git a/prolog/problems/lists/shiftleft_2/common.py b/prolog/problems/lists/shiftleft_2/common.py new file mode 100644 index 0000000..a8d56a7 --- /dev/null +++ b/prolog/problems/lists/shiftleft_2/common.py @@ -0,0 +1,13 @@ +id = 113 +group = 'lists' +number = 20 +visible = True +facts = None + +solution = '''\ +conc113([], L, L). +conc113([H|T], L2, [H|L]) :- + conc113(T, L2, L). +shiftleft([H|T], L2) :- + conc113(T, [H], L2). +''' diff --git a/prolog/problems/lists/shiftleft_2/en.py b/prolog/problems/lists/shiftleft_2/en.py new file mode 100644 index 0000000..0f4b807 --- /dev/null +++ b/prolog/problems/lists/shiftleft_2/en.py @@ -0,0 +1,12 @@ +id = 113 +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/shiftright_2/common.py b/prolog/problems/lists/shiftright_2/common.py new file mode 100644 index 0000000..3d5c3a5 --- /dev/null +++ b/prolog/problems/lists/shiftright_2/common.py @@ -0,0 +1,13 @@ +id = 114 +group = 'lists' +number = 21 +visible = True +facts = None + +solution = '''\ +conc114([], L, L). +conc114([H|T], L2, [H|L]) :- + conc114(T, L2, L). +shiftright(L1, [H|T]) :- + conc114(T, [H], L1). +''' diff --git a/prolog/problems/lists/shiftright_2/en.py b/prolog/problems/lists/shiftright_2/en.py new file mode 100644 index 0000000..8e2a57c --- /dev/null +++ b/prolog/problems/lists/shiftright_2/en.py @@ -0,0 +1,12 @@ +id = 114 +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/sublist_2/common.py b/prolog/problems/lists/sublist_2/common.py new file mode 100644 index 0000000..84c0bc8 --- /dev/null +++ b/prolog/problems/lists/sublist_2/common.py @@ -0,0 +1,14 @@ +id = 117 +group = 'lists' +number = 24 +visible = True +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). +''' diff --git a/prolog/problems/lists/sublist_2/en.py b/prolog/problems/lists/sublist_2/en.py new file mode 100644 index 0000000..5b6808b --- /dev/null +++ b/prolog/problems/lists/sublist_2/en.py @@ -0,0 +1,18 @@ +id = 117 +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/sum_2/common.py b/prolog/problems/lists/sum_2/common.py new file mode 100644 index 0000000..14b0614 --- /dev/null +++ b/prolog/problems/lists/sum_2/common.py @@ -0,0 +1,12 @@ +id = 118 +group = 'lists' +number = 25 +visible = True +facts = None + +solution = '''\ +sum([], 0). +sum([H|T], Sum) :- + sum(T, SumT), + Sum is SumT + H. +''' diff --git a/prolog/problems/lists/sum_2/en.py b/prolog/problems/lists/sum_2/en.py new file mode 100644 index 0000000..8db0e67 --- /dev/null +++ b/prolog/problems/lists/sum_2/en.py @@ -0,0 +1,12 @@ +id = 118 +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 = {} |