Skip to content

Instantly share code, notes, and snippets.

@andrelip
Created April 13, 2020 18:41
Show Gist options
  • Select an option

  • Save andrelip/ac7fe085b64ecc82100ed32d9c9663a8 to your computer and use it in GitHub Desktop.

Select an option

Save andrelip/ac7fe085b64ecc82100ed32d9c9663a8 to your computer and use it in GitHub Desktop.
defmodule PossibleCombinations do
@digits_map %{"1" => ["a", "b", "c"], "2" => ["d", "e", "f"], "3" => ["g", "h", "i"]}
def permutation(list) do
list_of_lists =
list
|> Enum.map(fn digit -> @digits_map[digit] end)
_do_combinations(list_of_lists, "")
|> List.flatten
end
defp _do_combinations([], processed_string) do
processed_string
end
defp _do_combinations(remaining_items, processed_string) do
[current_list | new_remaining_items] = remaining_items
current_list
|> Enum.map(fn item_level -> calculate_combinations(new_remaining_items, "#{processed_string}#{item_level}" ) end)
end
end
digits = ["1", "2", "3"]
PossibleCombinations.permutation(digits)
#=> ["adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg", "bdh",
#=> "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg",
#=> "ceh", "cei", "cfg", "cfh", "cfi"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment