Given a sequence of numbers, print out all possible sets such that the sum is less than a number

The problem has 3 inputs.

L: a list of all numbers

size: the size each set can be

max: the max sum amongst each set

The challenge is as follows:

Given L, size and max, construct as many sets from L such that the number of elements is size and the sum of each of the elements does not exceed max.

Examples:

func(L=[1,2,3,4], size=2, max=5) = [{1,2}, {1,3}, {2,3}, {1,4}]

Notice how each of the values in the outputted set are sum <= max.

func(L=[1,2,3,4], size=3, max=6) = [{1,2,3}]

func(L=[1,2,3,4], size=3, max=5) = [{}] or empty list, whichever you want

Note that in the set you cannot have duplicated items. ie: {2,1} = {1,2}

Constraints on inputs:

L: 0 or more elements

size: 0 <= size <= len(L)

max: 0 or more

If a list has 0 items, then always return the empty set.

If the size is 0, then always return the empty set.

If max is 0, it is possible that there are negative values in L, in which case the returned sets need not be empty.

Shorted bytes wins!

• I would suggest guaranteeing that L will be non-empty. It is generally advisable to avoid edge cases in code-golf challenges, as they don't add much and can cost many bytes. – Robin Ryder Sep 20 at 18:07
• This could really use some more test cases, especially test cases where the elements in L are not all unique. – DJMcMayhem Sep 20 at 18:22

05AB1E, 6 bytes

.ÆêʒO@

Try it online!

• Duplicated items in list: try this: 2,[1,1,2,2,3,3,4,4],20. [1,2] is repeated – K Split X Sep 20 at 18:04
• @KSplitX Thanks, fixed. It would be helpful if you added that test case to the question! – Grimy Sep 20 at 21:58

Brachylog, 9 bytes

≥ᵗ⟨⟨⊇l⟩+⟩

Try it online!

Takes input as [[L,size],max] through the input variable and generates the output through the output variable.

Python 3, 82 79 bytes

lambda l,s,m:[i for i in __import__("itertools").combinations(l,s)if sum(i)<=m]

Try it online!

-3 bytes thanks to squid

• @squid Thanks! I didn't know that existed – Matthew Jensen Sep 30 at 3:10

Japt, 11 bytes

àV â fÈx §W

Try it

àV â fÈx §W     :Implicit input of array U=L and integers V=size & W=max
àV              :Combinations of U of length V
â            :Deduplicate
f          :Filter by
È         :Function
x        :  Sum
§W     :  Less than or equal to W
• Not unique outputs. Try the following: [1,1,2,2,3,3,4,4],4,20... 1,1,2,3 for example is repeated – K Split X Sep 20 at 17:54
• @KSplitX, Missed that. Fixed – Shaggy Sep 20 at 18:02

R, 56 bytes

function(L,s,m,a=combn(L,s))unique(a[,colSums(a)<=m],,2)

Try it online!

Creates a matrix where each column is a subset of L of size s, and keeps those for which the sum is less than m. Output is a matrix, with each column corresponding to one allowable subset (if there is only one such subset, the output is a s×1 matrix and R will display it on one row instead by default).

Note that this fails for the edge case where L is empty. Handling that edge case as specified in the challenge takes 19 more bytes:

R, 75 bytes

function(L,s,m,a=combn(L,s))if(length(L),unique(a[,colSums(a)<=m],,2),{})

Try it online!

• 53 bytes? I don't think I've ever noticed that unique has a margin argument, that's quite a helpful tip! I usually just unique(t()); the comments on other answers seem to indicate that [1,1] shouldn't be a valid output on [1,1,2,3],2,20 – Giuseppe Sep 20 at 19:26
• @Giuseppe I discovered the margin argument today as well. I was going to unique(t()) but checked the help of unique just in case... and luckily, the argument exists! I guess we need to wait for OP to clarify the challenge to know where to apply unique. – Robin Ryder Sep 20 at 20:13

Gaia, 8 bytes

Σ⊂
euK↑⁇

Try it online!

Takes input as list, size, max sum on separate lines.

QœcS>¥Ðḟ⁵

Try it online!

Pyth, 15 bytes

{SMfgeQsT.PhQht

Try it online!

{SMfgeQsT.PhQhtQ  # (last Q implicit; Q = input)
.P       # all permutations
hQ     #   of Q
htQ  #           of length Q[1:] = Q
f              # filter those with lambda T:
eQ           #   Q[-1]
g             #          >=
sT         #             sum(T)
SM               # sort each element
{                 # deduplicate

Kotlin, 286 bytes

fun p(l:List<Int>,s:Int,m:Int){val r=MutableList(0){""}
val a=l.toSet().toList()