# A question that I could not solve

**URL:** <https://swi-prolog.discourse.group/t/a-question-that-i-could-not-solve/5336>\
**Category:** Help!\
**Created:** [April 26, 2022, 4:33pm UTC](https://swi-prolog.discourse.group/t/a-question-that-i-could-not-solve/5336 "2022-04-26T16:33:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![quanex1](https://avatars.discourse-cdn.com/v4/letter/q/5fc32e/32.png) [@quanex1](https://swi-prolog.discourse.group/u/quanex1)\
**Post date:** [April 26, 2022, 4:33pm UTC](https://swi-prolog.discourse.group/t/a-question-that-i-could-not-solve/5336/1 "2022-04-26T16:33:38Z")

</div>

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

 ![Ekran Görüntüsü (16)](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/0/03ff138903a0c9d04ed85d924a048d0eddd56c54.png)

I want the code to:

But what I’m getting is:

My code looks like this:

```prolog
% your code here

```

---

<div class="post-metadata">

**Author:** ![abaljeu](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/abaljeu/32/136_2.png) [@abaljeu](https://swi-prolog.discourse.group/u/abaljeu)\
**Post date:** [April 27, 2022, 7:48pm UTC](https://swi-prolog.discourse.group/t/a-question-that-i-could-not-solve/5336/2 "2022-04-27T19:48:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![quanex1](https://avatars.discourse-cdn.com/v4/letter/q/5fc32e/32.png) [@quanex1](https://swi-prolog.discourse.group/u/quanex1)\
**Post date:** [April 27, 2022, 8:28pm UTC](https://swi-prolog.discourse.group/t/a-question-that-i-could-not-solve/5336/3 "2022-04-27T20:28:38Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [April 28, 2022, 6:11am UTC](https://swi-prolog.discourse.group/t/a-question-that-i-could-not-solve/5336/5 "2022-04-28T06:11:50Z")

</div>

> [@quanex1](#):
>
> Can you write the code and tell me how you think to solve the question, how I should think?

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.

---

<div class="post-metadata">

**Author:** ![abaljeu](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/abaljeu/32/136_2.png) [@abaljeu](https://swi-prolog.discourse.group/u/abaljeu)\
**Post date:** [April 28, 2022, 3:55pm UTC](https://swi-prolog.discourse.group/t/a-question-that-i-could-not-solve/5336/6 "2022-04-28T15:55:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![quanex1](https://avatars.discourse-cdn.com/v4/letter/q/5fc32e/32.png) [@quanex1](https://swi-prolog.discourse.group/u/quanex1)\
**Post date:** [April 28, 2022, 10:26pm UTC](https://swi-prolog.discourse.group/t/a-question-that-i-could-not-solve/5336/7 "2022-04-28T22:26:58Z")

</div>

```prolog

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)](https://www.ic.unicamp.br/~meidanis/courses/mc336/2009s2/prolog/problemas/)
