RosettaCodeData/Task/Combinations/Euphoria/combinations.eu
2026-04-30 12:34:36 -04:00

32 lines
1.1 KiB
Text

sequence combinations = {}
procedure recurse(sequence remaining, /* digits remaining to compute */
integer ne, /* number of digits expected for a valid combination */
sequence selected = {}) /* digits already selected for a potential combination */
integer nr = length(remaining) -- number of digits remaining to compute
integer ns = length(selected) -- number of digits already selected for a potential combination
if ns+nr = ne then
combinations = append(combinations, selected & remaining)
return
end if
if nr = 1 or ns = ne then
combinations = append(combinations, selected)
return
end if
-- select next
recurse(remove(remaining, 1), ne, selected & remaining[1])
-- skip next
recurse(remove(remaining, 1), ne, selected)
end procedure
function get_combinations(sequence s, integer lg)
combinations = {}
recurse(s, lg)
return combinations
end function
constant TEST_SEQUENCE = {0,1,2,3,4}
combinations = {}
recurse(TEST_SEQUENCE, 3)
? combinations