Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added 872 characters in body
Source Link
Robin Ryder
  • 15.8k
  • 2
  • 25
  • 71

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!

R, 56 bytes

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

Try it online!

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!

Source Link
Robin Ryder
  • 15.8k
  • 2
  • 25
  • 71

R, 56 bytes

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

Try it online!

AltStyle によって変換されたページ (->オリジナル) /