sandeshsoni
Need help! need to optimize.
My terminal hangs when I run the code.
I have a list of 1_78_000 words i.e 178k words
I need pairs of words, such that when joined together, their length is exactly ten.
# this is how I get those 178k words from a file
# do I need to use streams here?
# Its just a txt file with one word per line
def all_words do
{:ok, content} = File.read("assets/dictionary.txt")
String.split(content, "\n", trim: true)
end
# Length of individial word must be 3 or more
# two words combined, their length must be exactly 10.
# example: moto + camera = motocamera
# I am sending `all_words` here.
def foo list
for a <- list, b <-list,
String.length(a) > 2 && String.length(b) > 2 && String.length(a <> b)==10,
do: a <> b
end
# do I need to add :into ?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
This operation is quadratic in its runtime as you state it. You might be able to shave a bit, if you filter shorter than 3 from the start, also it might help to generate
a <> bandb <> aat the same time when not using a comprehension but a recursive function only checking current word and tail of the input.If though this is an exercise on comprehensions, prefiltering is probably your best bet.
Ankhers
The reason this is taking so long is because you are traversing the list so many times (178,001 which is 31,684,178,000 elements to run through).
When you have that comprehension, you are basically doing the same as
So for each element in the list, you are actually traversing the entire list again.
What you may be able to do is reduce the list into a map with the key being the length of the word, and the values in a list. So you may end up with something like
Then you could just merge the numbers that equal 10. This should shrink your runtime a bit. There are probably ways to do this though.
sandeshsoni
can you please elaborate more in terms of code?
I tried this using the same code with a list of 1k words… it worked.
But for a large list, terminal just stops forever
sandeshsoni
looks helpful!
I was thinking of a map… your suggestion of string length as map key sounds useful.
I will give this a try!
NobbZ
It’s not forever, just for a very long time.
If it takes a second for a thousand element list, it will take 4 for a list of two thousand elements, 16 for 4k, etc.
And I will provide some code when I’m at my computer.
NobbZ
This is how I mean it:
This is just a draft though, and still might have some room for optimisations, but at least it should be of O(n * log n) instead of O(n²).
Eiji
Here is how I have generated
178_000random “words” (just~r/[a-zA-Z]{1,11}/) with random length from 1 to 11:From this random input I have received
35_349_451combinations in average209seconds.Note: This time is much too big as I’m on my “standard” PC usage with web browser, code editor and video player.
Here is my example code:
I got
previous clausewarning, but it’s only because@target_lengthmodule attribute is set at compile time. Feel free to modify it as you wish. For sure I believe that it’s definitely possible to write better code - it’s just my typical “5 min” example as I don’t have much time now.Let me know what do you think about it.
sandeshsoni
Thank you both for comments.
After reading all the comments,
This is what I came to
– Iterate words from dictionary
– Enum.group them by word.length
– Next, using comprehension, join them where the length == 10
– In my case I had to match word, so,
– – if word is at beginning length is key,
– – else if word is at end then key is -(10 - length)
The actual problem statement to me is, create word pairs for given_input_phone_number using dictionary words.
Here is some of the code.
complete problem solution is here.
https://github.com/sandeshsoni/code_samples/blob/master/elixir/numbero/lib/numbero.ex
I know there is room for performance optimisation.
Right now it takes around 10sec to complete execution.
Eiji
ah, now I better see your use case
Look that you can generate all cases for all
10-digitnumbers at compile time (with saving results toprivdirectory or database like:mnesia) and it would be then really easy and quick to fetch them.