type1fool
Hello, Elixir gang. I’m starting to get the hang of Elixir, which has been challenging since I’m not coming from Ruby. I have been struggling to find a solution for a particular problem. I need to generate a list of strings which increments over the alphabet. So["A", "B", "C"] is a very basic example.
When the end of the alphabet is reached, I want the result to look like this: [..."X", "Y", "Z", "AA", "BB"...]. The goal is to create a function like this: generate_list(first_letter, count).
I tried using the Alphabetify package, but it was pretty slow, and didn’t quite give me the results I needed. String.duplicate got me almost there, but I still wasn’t getting the right output.
Does anyone have an idea or package suggestion for this problem? I’ve hit a wall on this one.
Where I’m at ![]()
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |> String.split("", trim: true)
def generate_list(first_letter, count)
when is_binary(first_letter)
when is_integer(count) do
# letters
# |> Stream.cycle
# |> Stream.take(count)
starting_index = letters |> Enum.find_index(fn x -> x == first_letter end)
# 0..count
# |> Enum.map(fn x -> end)
end
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #iex
- #elixirconf-us
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
blatyo
This should do it. The first_letter part isn’t super efficient, but the rest should be.
EDIT: Fix divisor
type1fool
I changed
div(index, 25)todiv(index, 26)and the problem is solved. It correctly appends additional characters too:Thank you @blatyo for this clean solution!
kelvinst
That’s indeed a really good solution. But I got interested by the possibility of improving its performance and here are the results:
PS.: I also did some changes to the API, but basically, instead of cycling through the alphabet and navigating it with an index, I mathematically transform numbers into letters by adding 65 to them and duplicating them
div(number, 26) + 1times like you did.type1fool
@kelvinst Nice! I like this approach and I can’t wait to benchmark these two solutions.
kelvinst
Yeah, it would be nice to benchmark them, since I’m not sure my solution will really be faster. I mean, of course it’s faster with a high “first_letter” because it will not iterate discarding everything, but I’m not sure if the calcs I’ve done didn’t add any extra complexity per iteration.
type1fool
This is my first foray into benchmarking, so these results may be unreliable.
Benchmark Code:
It’s interesting that both approaches get exponentially slower as the count increases. I do like the simplicity of
generate_list/2, so that may be the way I end up going. I don’t expect the count to be higher than 1000, so either function would be usable.If you have any thoughts about the benchmark, let me know.
Thanks again!
kelvinst
So, the thing is that you didn’t bench the case were mine is supposedly faster, which would be with a high starting letter.
type1fool
This is true. I’ll run it again tonight with a higher starting letter.
kelvinst
Also, be sure to get a high letter like
“O” |> String.duplicate(40000)which is the number1039988.type1fool
Indeed you are correct. Starting with a crazy high letter benefits the LettersAndNumbers approach. Thi scenario would not ever happen. The highest letter would be something like
"ZZZZ". Fun experiment nonetheless.RESULTS: