How to solve this puzzle of arithmetic expression in Prolog?

I have a programming problem ( https://blog.svpino.com/2015/05/08/solution-to-problem-5-and-some-other-thoughts-about-this-type-of-questions ):

Write a program that prints out all the possibilities for placing + or - or anything between the numbers 1, 2, ..., 9 (in that order) such that the result is always 100. For example: 1 + 2 + 34 - 5 + 67 - 8 + 9 = 100.

I solved this problem with Python getting 11 answers :

import itertools   
for operator in [p for p in itertools.product(['+','-',''], repeat=8)]:
    values = zip([str(x) for x in range(1, length+1)], operator) + ['9']
    code = ''.join(itertools.chain(*values))
    if 100 == eval(code):
        print "%s = %d" % (code, eval(code))

      

This is my second Python code that is longer ( https://gist.github.com/prosseek/41201d6508f01cf1643e ):

[1, 2, 34, -5, 67, -8, 9]
[1, 23, -4, 56, 7, 8, 9]
[12, 3, -4, 5, 67, 8, 9]
[123, -4, -5, -6, -7, 8, -9]
[1, 23, -4, 5, 6, 78, -9]
[12, 3, 4, 5, -6, -7, 89]
[12, -3, -4, 5, -6, 7, 89]
[123, -45, -67, 89]
[123, 45, -67, 8, -9]
[1, 2, 3, -4, 5, 6, 78, 9]
[123, 4, -5, 67, -89]

      

I also found the suggested solution in Prolog ( http://www.reddit.com/r/programming/comments/358tnp/five_programming_problems_every_software_engineer/cr2dvsz ):

sum([Head|Tail],Signs,Result) :-
   sum(Head,Tail,Signs,Result).
sum(X,[],[],X).

sum(First,[Second|Tail],['+'|Signs],Result) :-
   Head is First + Second,
   sum(Head,Tail,Signs,Result).
sum(First,[Second|Tail],['-'|Signs],Result) :-
   Head is First - Second,
   sum(Head,Tail,Signs,Result).
sum(First,[Second|[Third|Tail]],['+'|[''|Signs]],Result) :- 
   C    is Second*10+Third, 
   Head is First + C, 
   sum(Head,Tail,Signs,Result).
sum(First,[Second|[Third|Tail]],['-'|[''|Signs]],Result) :-
   C    is Second*10+Third,
   Head is First - C,
   sum(Head,Tail,Signs,Result).

      

However, this only gives 4 solutions (not 11 as expected):

?- sum([1,2,3,4,5,6,7,8,9],X,100).
X = [+, +, -,+, +, +,'',+] ;
X = [+, +,'',-, + '', -,+] ;
X = [+,'', -,+, +, +,'',-] ;
X = [+,'', -,+ '', +, +,+] ;
false.

      

This is because it ''

doesn't appear as the first item in the list. Thus, solutions [12,...]

and [123,...]

skipped.

I tried adding sum(First,[Second|Tail],[''|Signs],Result) :- Head is First*10 + Second, sum(Head,Tail,Signs,Result).

, but in doing so, it returns 15 solutions , not 11.

The explanation says that with the wrong interpretation 1+23

before ((1)+2)*10+3

.

?- sum([1,2,3], [+,''], Result). 
Result = 33.

      

Then how to solve this problem in Prolog? How to teach Prolog 1 + 23

is there 24

in this example?

+3


source to share


3 answers


a Python equivalent of eval can be implemented with read_term/3

and is/2

, or

give_100(A) :-
    generate(1, S),
    atomic_list_concat(S, A),
    read_term_from_atom(A, T, []),
    T =:= 100.

generate(9, [9]).
generate(N, [N|Ns]) :-
    N < 9, sep(N, Ns).

sep(N, L) :-
    ( L = [+|Ns] ; L = [-|Ns] ; L = Ns ),
    M is N+1,
    generate(M, Ns).

      



Example request:

?- give_100(X).
X = '1+2+3-4+5+6+78+9' ;
X = '1+2+34-5+67-8+9' ;
X = '1+23-4+5+6+78-9' ;
X = '1+23-4+56+7+8+9' ;
X = '12+3+4+5-6-7+89' ;
X = '12+3-4+5+67+8+9' ;
X = '12-3-4+5-6+7+89' ;
X = '123+4-5+67-89' ;
X = '123+45-67+8-9' ;
X = '123-4-5-6-7+8-9' ;
X = '123-45-67+89' ;
false.

      

+2


source


Using , first we define a nonterminal sep//0

:

sep --> "+" | "-" | "".

      



Then we run the following query (using the phrase/2

, sep//0

, read_from_codes/2

, and (=:=)/2

):

?- set_prolog_flag(double_quotes,chars).
true.

?- phrase(("1",sep,"2",sep,"3",sep,"4",sep,"5",sep,"6",sep,"7",sep,"8",sep,"9"),Cs),
   read_from_codes(Cs,Expr),
   Expr =:= 100.
  Cs = [1,+,2,+,3,-,4,+,5,+,6,+,7,8,+,9], Expr = 1+2+3-4+5+6+78+9
; Cs = [1,+,2,+,3,4,-,5,+,6,7,-,8,+,9],   Expr = 1+2+34-5+67-8+9
; Cs = [1,+,2,3,-,4,+,5,+,6,+,7,8,-,9],   Expr = 1+23-4+5+6+78-9
; Cs = [1,+,2,3,-,4,+,5,6,+,7,+,8,+,9],   Expr = 1+23-4+56+7+8+9
; Cs = [1,2,+,3,+,4,+,5,-,6,-,7,+,8,9],   Expr = 12+3+4+5-6-7+89
; Cs = [1,2,+,3,-,4,+,5,+,6,7,+,8,+,9],   Expr = 12+3-4+5+67+8+9
; Cs = [1,2,-,3,-,4,+,5,-,6,+,7,+,8,9],   Expr = 12-3-4+5-6+7+89
; Cs = [1,2,3,+,4,-,5,+,6,7,-,8,9],       Expr = 123+4-5+67-89
; Cs = [1,2,3,+,4,5,-,6,7,+,8,-,9],       Expr = 123+45-67+8-9
; Cs = [1,2,3,-,4,-,5,-,6,-,7,+,8,-,9],   Expr = 123-4-5-6-7+8-9
; Cs = [1,2,3,-,4,5,-,6,7,+,8,9],         Expr = 123-45-67+89
; false.

      

+2


source


Just like CapelliC's solution, but works with SWI-Prolog and the lambda module:

:- use_module(library(lambda)).

sum_100(Atom) :-
    L = [1,2,3,4,5,6,7,8,9],
    O = [_A,_B,_C,_D,_E,_F,_G,_H,' '],
    maplist(\X^member(X, [+,-,' ']), O),
    foldl(\X^Y^Z^T^(Y = ' '
                    ->  append(Z,[X], T)
                    ;   append(Z,[X,Y], T)), L, O, [], Expr),
    atomic_list_concat(Expr, Atom),
    term_to_atom(Term, Atom),
    Term =:= 100.

      

Example request:

?- sum_100(X).
X = '1+2+3-4+5+6+78+9' ;
X = '1+2+34-5+67-8+9' ;
X = '1+23-4+5+6+78-9' ;
X = '1+23-4+56+7+8+9' ;
X = '12+3+4+5-6-7+89' ;
X = '12+3-4+5+67+8+9' ;
X = '12-3-4+5-6+7+89' ;
X = '123+4-5+67-89' ;
X = '123+45-67+8-9' ;
X = '123-4-5-6-7+8-9' ;
X = '123-45-67+89' ;
false.

      

+1


source







All Articles