How to generate all possible 3 letter strings

Hello friends
I need to write the same code in Elixir as simply as possible
Please introduce your solutions to me

Python:

import itertools

alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
keywords = [''.join(i) for i in itertools.product(alphabets, repeat=3)]
print(keywords)

Thanks

1 Like
iex> letters = ?a..?z                                                                                         
97..122

iex> for first <- letters, second <- letters, third <- letters, do: [first, second, third] |> List.to_string()
["aaa", "aab", "aac", "aad", "aae", "aaf", "aag", "aah", "aai", "aaj", "aak",
 "aal", "aam", "aan", "aao", "aap", "aaq", "aar", "aas", "aat", "aau", "aav",
 "aaw", "aax", "aay", "aaz", "aba", "abb", "abc", "abd", "abe", "abf", "abg",
 "abh", "abi", "abj", "abk", "abl", "abm", "abn", "abo", "abp", "abq", "abr",
 "abs", "abt", "abu", "abv", "abw", "abx", ...]
13 Likes

I think there is a better way
Because, for example, if we produce 10 letters
This code gets a little confusing

You are welcome :grin:

3 Likes

IO.puts(":grimacing: :heart:")

1 Like
?a..?z
|> combinations(3)
|> Enum.map(&List.to_string/1)
6 Likes

Thanks for sharing
I feel it is not working properly

You want permutations with repetitions, not just combinations.

5 Likes

You do not need to go through list, <<a, b, c>> will do the job.

3 Likes

True, thank you

Another way

  def shuffle(list), do: shuffle(list, length(list))
  def shuffle([], _), do: [[]]
  def shuffle(_, 0), do: [[]]

  def shuffle(list, i) do
    for x <- list, y <- shuffle(list, i - 1), do: [x | y]
  end

Source

2 Likes

with nested fors,

combine = fn
  total when total > 1 ->
    alphabet = Enum.map(?a..?z, &<<&1>>)
    alphabets = List.duplicate(alphabet, total - 1)

    for letters <- alphabets, reduce: alphabet do
      acc ->
        for letter <- letters, item <- acc do
          item <> letter
        end
    end
    |> Enum.sort()

  1 ->
    Enum.map(?a..?z, &<<&1>>)
end

combine.(3)
iex> combine.(3)
["aaa", "aab", "aac", "aad", "aae", "aaf", "aag", "aah", "aai", "aaj", "aak", "aal", "aam", "aan",
 "aao", "aap", "aaq", "aar", "aas", "aat", "aau", "aav", "aaw", "aax", "aay", "aaz", "aba", "abb",
 "abc", "abd", "abe", "abf", "abg", "abh", "abi", "abj", "abk", "abl", "abm", "abn", "abo", "abp",
 "abq", "abr", "abs", "abt", "abu", "abv", "abw", "abx", ...]
1 Like

I think its simpler if we use reduce for collecting

Enum.reduce(1..3, [<<>>], fn _, res -> 
  for c <- ?a..?z, acc <- res, do: <<c,acc::binary>>
end)
3 Likes