summaryrefslogtreecommitdiff
path: root/prolog/problems/license_plates/genexp_2
diff options
context:
space:
mode:
Diffstat (limited to 'prolog/problems/license_plates/genexp_2')
-rw-r--r--prolog/problems/license_plates/genexp_2/common.py23
-rw-r--r--prolog/problems/license_plates/genexp_2/en.py18
2 files changed, 41 insertions, 0 deletions
diff --git a/prolog/problems/license_plates/genexp_2/common.py b/prolog/problems/license_plates/genexp_2/common.py
new file mode 100644
index 0000000..a91c392
--- /dev/null
+++ b/prolog/problems/license_plates/genexp_2/common.py
@@ -0,0 +1,23 @@
+id = 146
+group = 'license_plates'
+number = 53
+visible = True
+facts = None
+
+solution = '''\
+memb146(X, [X|_]).
+memb146(X, [_|T]) :-
+ memb146(X, T).
+
+conc146([], L, L).
+conc146([H|T], L2, [H|L]) :-
+ conc146(T, L2, L).
+
+genexp([Exp], Exp).
+genexp(L, Exp) :-
+ conc146(Before, [N1,N2|After], L),
+ memb146(Op, ['+','-','*','/']),
+ NExp =.. [Op, N1, N2],
+ conc146(Before, [NExp|After], L1),
+ genexp(L1, Exp).
+'''
diff --git a/prolog/problems/license_plates/genexp_2/en.py b/prolog/problems/license_plates/genexp_2/en.py
new file mode 100644
index 0000000..c5c61fb
--- /dev/null
+++ b/prolog/problems/license_plates/genexp_2/en.py
@@ -0,0 +1,18 @@
+id = 146
+name = 'genexp/2'
+slug = 'generate an arithmetic expression from a list'
+
+description = '''\
+<p><code>genexp(L, E)</code>: the expression <code>E</code> is obtained from the list <code>L</code> by inserting arithmetic operators between list elements. Your code should generate all valid solutions.</p>
+<pre>
+ ?- genexp([1,2,3], L).
+ L = 1+2+3 ;
+ L = 1+2-3 ;
+ L = (1+2)*3 ;
+ L = (1+2)/3 ;
+ L = 1-2+3 ;
+ L = 1-2-3 ;
+ ...
+</pre>'''
+
+hint = {}