Calculate whether the sum of exactly three values in a list is equal to N

…衆ロ難τιáo~ 提交于 2019-12-22 14:40:42

问题


Examples: ([1,2,3,7,6,9], 6). should print True, as 1+2+3=6.

([1,2,3,7,6,9], 5). should print False as there are no three numbers whose sum is 5.

([],N) where N is equal to anything should be false.

Need to use only these constructs:

  • A single clause must be defined (no more than one clause is allowed).
  • Only the following is permitted:

    +, ,, ;, ., !, :-, is, Lists -- Head and Tail syntax for list types, Variables.

I have done a basic coding as per my understanding.

findVal([Q|X],A) :-
   [W|X1]=X,
   [Y|X2]=X,
   % Trying to append the values.
   append([Q],X1,X2),
   % finding sum.
   RES is Q+W+Y,
   % verify here.
   (not(RES=A)->

   % finding the values.

   (findVal(X2,A=)->

true   

;
(findVal(X,A)->   

% return result.

true
;
% return value.

false))
;
% return result.

true
).

It does not seem to run throwing the following error.

ERROR:

Undefined procedure: findVal/2 (DWIM could not correct goal)

Can someone help with this?


回答1:


You can make use of append/3 [swi-doc] here to pick an element from a list, and get access to the rest of the elements (the elements after that element). By applying this technique three times, we thus obtain three items from the list. We can then match the sum of these elements:

sublist(L1, S) :-
    append(_, [S1|L2], L1),
    append(_, [S2|L3], L2),
    append(_, [S3|_], L3),
    S is S1 + S2 + S3.



回答2:


Well, you can iterate (via backtracking) over all the sublists of 3 elements from the input list and see which ones sum 3:

sublist([], []).
sublist([H|T], [H|S]) :- sublist(T, S).
sublist([_|T], S) :- sublist(T, S).

:- length(L, 3), sublist([1,2,3,7,6,9], L), sum_list(L, 6).



回答3:


I'm giving a partial solution here because it is an interesting problem even though the constraints are ridiculous.

First, I want something like select/3, except that will give me the tail of the list rather than the list without the item:

select_from(X, [X|R], R).
select_from(X, [_|T], R) :- select_from(X, T, R).

I want the tail, rather than just member/2, so I can recursively ask for items from the list without getting duplicates.

?- select_from(X, [1,2,3,4,5], R).
X = 1,
R = [2, 3, 4, 5] ;
X = 2,
R = [3, 4, 5] ;
X = 3,
R = [4, 5] ;
X = 4,
R = [5] ;
X = 5,
R = [] ;
false.

Yeah, this is good. Now I want to build a thing to give me N elements from a list. Again, I want combinations, because I don't want unnecessary duplicates if I can avoid it:

select_n_from(1, L, [X]) :- select_from(X, L, _).
select_n_from(N, L, [X|R]) :- 
    N > 1, 
    succ(N0, N), 
    select_from(X, L, Next), 
    select_n_from(N0, Next, R).

So the idea here is simple. If N = 1, then just do select_from/3 and give me a singleton list. If N > 1, then get one item using select_from/3 and then recur with N-1. This should give me all the possible combinations of items from this list, without giving me a bunch of repetitions I don't care about because addition is commutative and associative:

?- select_n_from(3, [1,2,3,4,5], R).
R = [1, 2, 3] ;
R = [1, 2, 4] ;
R = [1, 2, 5] ;
R = [1, 3, 4] ;
R = [1, 3, 5] ;
R = [1, 4, 5] ;
R = [2, 3, 4] ;
R = [2, 3, 5] ;
R = [2, 4, 5] ;
R = [3, 4, 5] ;
false.

We're basically one step away now from the result, which is this:

sublist(List, N) :-
    select_n_from(3, List, R), 
    sumlist(R, N).

I'm hardcoding 3 here because of your problem, but I wanted a general solution. Using it:

?- sublist([1,2,3,4,5], N).
N = 6 ;
N = 7 ;
N = 8 ;
N = 8 ;
N = 9 ;
N = 10 ;
N = 9 ;
N = 10 ;
N = 11 ;
N = 12 ;
false.

You can also check:

?- sublist([1,2,3,4,5], 6).
true ;
false.

?- sublist([1,2,3,4,5], 5).
false.

?- sublist([1,2,3,4,5], 8).
true ;
true ;
false.

New users of Prolog will be annoyed that you get multiple answers here, but knowing that there are multiple ways to get 8 is probably interesting.



来源:https://stackoverflow.com/questions/59168962/calculate-whether-the-sum-of-exactly-three-values-in-a-list-is-equal-to-n

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!