A question that I could not solve

I’m using: SWI-Prolog version ???

I want the code to:

But what I’m getting is:

My code looks like this:

% your code here

Tell what you know and at what point you are stuck.

In order to solve the problem, first I made up another question which has test cases as follows
Input → [50,10,20,100,140,5,5]
Output → [[50,10,20],[100],[140,5,5]] // Summation of elements of every subset of the list<=150

Input → [10,20,30,40,50]
Output → [[10,20,30,40,50]]

So, I tried to write such a code in prolog but I could not do it. Can you write the code and tell me how you think to solve the question, how I should think?

Thanks in advance!

This is the job of your teacher, or your colleagues who are working on the same problems, or probably your own responsibility. I usually try to stay out of those threads but this is too arrogant.

1 Like

You still haven’t answered me. Have you ever written a program in Prolog? What is your attempt to solve this one?

1 Like


sum_list([], 0).

sum_list([H|T], Sum) :-
   sum_list(T, SubSum),
   Sum is H + SubSum.

group_list([],_).

group_list([H|T],[[H|Th]|T]):-
    ( group_list(T,[[Th]|T]); group_list([H|T],[_|T]) ),
    sum_list([H|Th],Sum),
    150>=Sum.

Yes, I wrote roughly 20 code to solve such a problems in P-99: Ninety-Nine Prolog Problems (unicamp.br)

1 Like