r/dailyprogrammer • • Nov 26 '14
[2014-11-26] Challenge #190 [Intermediate] Words inside of words
Description
This weeks challenge is a short yet interesting one that should hopefully help you exercise elegant solutions to a problem rather than bruteforcing a challenge.
Challenge
Given the wordlist enable1.txt, you must find the word in that file which also contains the greatest number of words within that word.
For example, the word 'grayson' has the following words in it
Grayson
Gray
Grays
Ray
Rays
Son
On
Here's another example, the word 'reports' has the following
reports
report
port
ports
rep
You're tasked with finding the word in that file that contains the most words.
NOTE : If you have a different wordlist you would like to use, you're free to do so.
Restrictions
- To keep output slightly shorter, a word will only be considered a word if it is 2 or more letters in length
- The word you are using may not be permuted to get a different set of words (You can't change 'report' to 'repotr' so that you can add more words to your list)
Finally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
2
u/G33kDude 1 1 Nov 26 '14
The given wordlist doesn't contain any words less than 5 characters long. You might consider merging it with another list
9
u/Cosmologicon 2 3 Nov 26 '14 edited Nov 26 '14
Not what I would call elegant. It's O(L2) to score a word of L letters, so O(NL2) overall. I was hoping to think of something better, but L is pretty small to begin with, so you're not going to gain much there.
4
u/G33kDude 1 1 Nov 26 '14
I can confirm that it's
However, my code is very... slow. Oh, and crashy. I couldn't think of a way to speed it up further than what I had done. AutoHotkey doesn't support multithreading so I tried some hacky multi-proces code. I basically ended up making windows really unstable for several minutes. I'm going to try to optimize some more...
4
u/[deleted] Nov 27 '14 edited Dec 22 '18
deleted 0.5528 What is ^^^this?
3
u/chunes 1 2 Nov 27 '14 edited Nov 27 '14
Java:
This one lists all the subwords in a given word, prints the longest word without any subwords, and also prints the word with the most subwords. Tip for those with performance issues:
This runs almost instantly once the file is loaded.
Output
3
u/threeifbywhiskey 0 1 Nov 27 '14
My Ruby solution starts by storing all of the words in a Hash for fast lookup. After that, it's just a matter of counting, for each word, the number of subsequences of length 2 or greater that appear in the table.
3
u/Magnevv Nov 27 '14 edited Nov 27 '14
Python solution. Pretty good runtime linear over the words, but cubed time for word length, so it should be something like O(nm2) where n is the amount of words, and m is the max word length. Uses about 4 seconds on my netbook. Creates a trie, or a prefix table to be able to do speedy checks for subwords. You can probably do some hacks where you only check the large words, and break once you know that no smaller word will be better, but I didn't bother doing this for this problem.
2
u/cdkisa Nov 27 '14
What sort of run time duration are people experiencing (i.e. how long does it take your method to return)?
2
u/[deleted] Nov 27 '14
Perl solution. Runs in about 8s on my machine.
1
u/hutsboR 3 0 Nov 26 '14 edited Nov 26 '14
Dart: I did some testing with my dictionary (150k words~) and it seems to be trivial to even consider words with a length of less than 15~ or so. I started at filtering out words with a length less than 7, then 15 and got the same result both times.
Output: This is using wordlist.txt AND enable1.txt. I receive the same result.
1
u/Dutsj Nov 26 '14
Solution in Java:
Output:
1
u/marchelzo Nov 26 '14
Haskell:
I was going to try some memoization, which is why I used unsafePerformIO to load the file, but then it seemed easier to just do it the "dumb" way.
1
u/samuelstan Nov 26 '14
Clojure
Like the super short Python solution, checking a single word is O(L2 ) so checking all of them is O(nL2 ). I couldn't think of any assumptions I could make to slim things down (obviously longer words are better, but it's hard to apply that rule generally...)
Sample output:
EDIT: Formatting
1
u/jnazario 2 0 Nov 26 '14
F#, my not optimized solution. runtime and complexity is awful.
1
u/CrazyM4n Nov 27 '14 edited Nov 27 '14
Haskell Solution:
Well, it works. It scales terribly though, but it ran in about 3 seconds for a 30 kilobyte file. I'm honestly quite afraid to try it with a larger file, as with the given example1.txt it went for 3 minutes until I decided to stop it. If anyone could help actually make this work in a respectable time, I'd appreciate it.
Example output, run on the first small portion of the file (yeah, I know it's not formatted :C)
This should be faster, but I'm not 100% sure:
It's just a fundamental problem of my method, I can't speed it up much more.
EDIT: After running the second one for 2 hours, it gave me this:
1
u/xpressrazor Nov 27 '14 edited Nov 27 '14
Python very slow
1
u/cdkisa Nov 27 '14
VB.Net 4.0 *thanks \u\chunes for the tip on the hashtable :)
Output
1
u/Charcolios Nov 27 '14
c++, with optimization, runs in under 2 seconds.
my code:
1
u/scubasteve2012 1 0 Nov 27 '14 edited Nov 27 '14
C#. Could probably be much more efficient. Feedback welcome. (Edited to hide solution description to avoid spoilers). Runs in about 8 seconds on my Laptop.
Results:
1
u/-Robbie Nov 27 '14
Haskell:
Output
1
u/ddsnowboard Nov 28 '14
Here's my Java. I got the sneaky way, so it doesn't take weeks, but it's long and a little ugly. It works in about 5 seconds on my modest-four-years-ago laptop, which is better than I expected, honestly. Four lines of python guy has me beat, but I thought it was worth posting anyway. Any constructive criticism is appreciated.
1
u/hitsballswithracket Nov 28 '14
C++
1
u/aertho Nov 28 '14
Heres a convoluted python attempt, runs on my machine in 0.68 seconds which is less than half of all the solutions I sampled from here.
Please let me know if you've got any ideas for improvements.
1
u/OldNedder Nov 28 '14
Java, about 0.7 seconds.
1
u/kur0saki Nov 28 '14
A pretty straight forward solution using Go:
This takes ~800ms on my windows box. 700-740ms are taken to read the file.
1
u/jeaton Nov 29 '14
C++11:
Output:
1
u/mmstiver Nov 29 '14
C# - Not the best, could be tweaked for better performance. But it's 2 am, and this was supposed to take an hour and be done by 1. I've settled for having it just run under 20 seconds.
1
u/ICanCountTo0b1010 Dec 01 '14
Using trie trees is fun! Here's my python3 solution
1
u/[deleted] Dec 03 '14
A little late to the party. This Python programme works, I think, but it's really really slow when iterating over the prescribed list. I'm trying to find a way to make it not slow.
1
u/dallbee Dec 18 '14
Wish I knew how to make this faster, but it's stable and straightforward. Implementation uses a RedBlackTree.
Written in D:
1
u/ohneSalz Dec 25 '14
C++ A little bit late, but it finally works.