Created
April 13, 2020 18:41
-
-
Save andrelip/ac7fe085b64ecc82100ed32d9c9663a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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