Skip to content

Instantly share code, notes, and snippets.

@Hugoberry
Last active November 2, 2023 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hugoberry/10da47bd381918d303c5db903afe5886 to your computer and use it in GitHub Desktop.
Save Hugoberry/10da47bd381918d303c5db903afe5886 to your computer and use it in GitHub Desktop.
let
Source = {"B", "F", "T"},
n = List.Count(Source),
combinations = Number.Power(2,n)-1,
CreateCombination = (mask as number) as list =>
List.Select(List.Transform(List.Positions(Source), (position) =>
if Number.BitwiseAnd(mask, Number.Power(2, position)) <> 0 then Source{position} else null),
each _ <> null),
AllCombinations = List.Transform({1..combinations}, CreateCombination)
in
List.Transform(AllCombinations,Text.Combine)
let
Source = {"A", "B", "C"},
// Function to generate combinations
fnCombinations = (list, k) =>
let
comb = if k = 0 then {{}} else
if List.Count(list) = 0 then {} else
List.Combine({@fnCombinations(List.Skip(list, 1), k),
List.Transform(@fnCombinations(List.Skip(list, 1), k - 1),
each List.Combine({{List.First(list)}, _}))})
in comb,
// Generate combinations for each k
CombineAll = List.Transform(List.Combine(List.Transform({1..List.Count(Source)}, each fnCombinations(Source, _))),Text.Combine)
in
CombineAll
let
Source = {"B", "F", "T"},
AllCombinations = List.Transform(
{1 .. (Number.Power(2, List.Count(Source)) - 1)},
(number) =>
List.Select(
List.Transform(
List.Positions(Source),
(pos) =>
if Number.BitwiseAnd(number, Number.Power(2, pos)) <> 0 then Source{pos} else null
),
(value) => value <> null
)
),
AllCombinationsAsText = List.Transform(AllCombinations, Text.Combine)
in
AllCombinationsAsText
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment