Scala, 164 155 132 131 bytes
s=>t=>1 to 1<<s.size-1 map(i=>s.indices.map(j=>s(j)+","*(i>>j&1)).mkString.split(','))filter(_.forall(i=>i.toInt<=t&i==i.toInt+""))
Try it online!
Generating all partitions is still the most byte-consuming part, since there is no built-in for that.
The original version took a different approach (164 bytes), which was optimized by @user via three nice tricks (155 bytes), see comments for details and code. Now, I changed the strategy itself, which gives the current solution (132 bytes), and another byte removed thanks to another hint by @user (now: 131 bytes).
The current version uses the 1-bit-index-positions of a counter variable as split-positions.
The original version generated the indices of split-positions directly via combinations of all valid lengths and ranges.
Some details on the "bit-hacking":
The outer loop generates all numbers from 1
up to 2^(numDigits-1)
, where the upper bound is represented via bit-shifting as 1<<s.size-1
(side note: as a consequence, this solution, based on signed Integer shifts, does only work for input numbers of at most 31 digits, which should be fair enough, but could easily be extended via BigInt to arbitrary input lengths).
In binary representation, the outer loop variable i
goes, for example, from 00001
, 00010
, 00011
, ... up to 10000
for a 5-digit input such as 15008
.
The j
'th bit position (counted from the right, starting at 0) indicates whether we should inject a "comma character" after the j
'th index position of the input string. This is executed by the inner loop:
The inner loop traverses the input string s
char by char at index j
and evaluates the expression i>>j&1
.
This expression "probes" whether the j
'th bit of i
is set by shifting i
by j
bit-positions to the right and then bit-AND-ing it with 1, which evaluates to 0 or 1, depending on whether the j
'th bit is 1 or 0.
As a consequence, the current char s(j)
is preceeded by either the empty string ","*0
or by a comma ","*1
, depending on the j
'th bit of i
.
(side note: bit representation 10000
represents the case of not including any comma, since it is injected after the last position. This case could also be achieved via 00000
instead, but then one would have to use the longer loop expression 0 until 1<<s.size-1
instead).
15008, 7
example, is that output example supposed to be exhaustive? If so, why aren't[1,5,0]
,[0,0]
,[1,0]
etc. in the output? In fact, it seems as though your two examples are contradictory. Are we supposed to only keep partitions where each element is below the threshold (i.e the first example), or do we remove elements from each partition above the threshold (second example) \$\endgroup\$[1,22,2,3]
and[1,2,22,3]
) - is that what it means? \$\endgroup\$[1, 500, 8]
be a valid partitioning? \$\endgroup\$