r/dailyprogrammer • u/nottoobadguy • Feb 11 '12
[2/11/2012] challenge #3 [difficult]
Welcome to cipher day!
For this challenge, you need to write a program that will take the scrambled words from this post, and compare them against THIS WORD LIST to unscramble them. For bonus points, sort the words by length when you are finished. Post your programs and/or subroutines!
Here are your words to de-scramble:
mkeart
sleewa
edcudls
iragoge
usrlsle
nalraoci
nsdeuto
amrhat
inknsy
iferkna
28
Upvotes
1
u/[deleted] Feb 12 '12 edited Feb 12 '12
c++
output:
3
u/lnxaddct Feb 12 '12
Simple python solution including the bonus: https://gist.github.com/1806430
2
u/_redka 0 0 Feb 11 '12 edited Feb 11 '12
Ruby -> 5 lines
results:
2
u/eruonna Feb 11 '12
Haskell:
2
u/stevelosh Feb 11 '12 edited Feb 11 '12
Clojure:
EDIT: Bonus.
EDIT 2: Better version -- doesn't sort the wordlist multiple times:
2
u/drb226 0 0 Feb 11 '12
15 lines of Haskell: http://hpaste.org/63490
Room for "optimization", but that's the gist of it. Stops once it finds the first match per scrambled word.
2
u/BobDorian Feb 11 '12
Was fun :)
1
u/callekabo Feb 11 '12
I did two versions in python 3.
Here's the one I did first: http://pastebin.com/LKHWygMH
Then I did another version after seeing the solutions posted here: http://pastebin.com/CwRyF3zs
My first solution is quicker though, 0.077 seconds compared to 0.125 seconds on my computer :)
EDIT: after adding a break if the word was found in the second solution it now takes 0.1 seconds.
1
u/[deleted] Feb 11 '12
Common Lisp: http://pastebin.com/kvXXnLMG
1
u/robin-gvx 0 2 Feb 11 '12
http://hastebin.com/raw/rafejoxari
I couldn't test this one because the VM's segfaulting, and I haven't found the cause yet.
Also it might have false positives. It would match "wool" and "llow", for example.
1
u/329ygwh1135t Feb 12 '12
suhweeet, sweet Python 2.6 (because I'm working on a project that specifies 2.6 in the requirements, ok!?)
1
u/bigmell Feb 12 '12
Solution in perl, few extra lines for clarity
1
u/CMahaff Feb 12 '12 edited Feb 12 '12
C!
This was a learning experience for me, so the code is pretty bad. Kinda cheated on entering the data, overused memory, not flexible, messy output. I'm not a very experienced programmer, the only language I really know is Java, and I'm not even in college yet. But it does work!
And a big thank you to Rhomboid at /r/learnprogramming for helping me get it running.
Around 100 lines if compacted. Takes .02-.03 seconds to run.
Here it is: http://pastebin.com/Y7ERECL7
1
u/mrdth Feb 12 '12
20 lines of PHP (plus a few comments)
Edit: better variable names
1
u/mallardtheduck Feb 13 '12 edited Feb 13 '12
My C++ version: http://pastebin.com/APyZJWWj (84 nicely formatted lines with a few comments).
"wordlist.hpp" just contains the word list as a array of
std::stringand 'wordlist_size`, which is self-explanatory.Probably not the most efficient solution, nor the shortest, but maybe a novel way to solve the problem...
EDIT: Here's a 64-line attempt with a completely different approach: http://pastebin.com/7M7mRYhd
And here's the 46-line naive approach: http://pastebin.com/YUiw6Gkx
(And here's wordlist.hpp if you want to run those: http://pastebin.com/ZNKJXFTW )
1
u/couldyousaythatagain Feb 16 '12
Result in C, using an NFA. ~500 lines. It explores all possible words using a queue of traversals over the states.
http://pastebin.com/sKm5rZ08
OUTPUT:
1
u/leonardo_m May 28 '12 edited May 28 '12
Solution in D, runtime about 0.05 seconds on an old PC. splitter() is lazy. It uses dstrings so it's not so memory-efficient. A solution using strings and immutable(ubyte)[] for the keys saves memory with such ASCII-only dictionary.
Output:
1
u/dawpa2000 Feb 11 '12
JavaScript
~60 lines
0
u/[deleted] Feb 11 '12
Done in perl 5.14.2
http://pastebin.com/LBbK6hvw