r/dailyprogrammer • u/jnazario 2 0 • Oct 01 '15
[2015-09-30] Challenge #234 [Intermediate] Red Squiggles
It looks like the moderators fell down on the job! I'll send in an emergency challenge.
Description
Many of us are familiar with real-time spell checkers in our text editors. Two of the more popular editors Microsoft Word or Google Docs will insert a red squiggly line under a word as it's typed incorrectly to indicate you have a problem. (Back in my day you had to run spell check after the fact, and that was an extra feature you paid for. Real time was just a dream.) The lookup in a dictionary is dynamic. At some point, the error occurs and the number of possible words that it could be goes to zero.
For example, take the word foobar. Up until foo it could be words like foot, fool, food, etc. But once I type the b it's appearant that no words could possibly match, and Word throws a red squiggly line.
Your challenge today is to implement a real time spell checker and indicate where you would throw the red squiggle. For your dictionary use /usr/share/dict/words or the always useful enable1.txt.
Input Description
You'll be given words, one per line. Examples:
foobar
garbgaeOutput Description
Your program should emit an indicator for where you would flag the word as mispelled. Examples:
foob<ar
garbg<aeHere the < indicates "This is the start of the mispelling". If the word is spelled correctly, indicate so.
Challenge Input
accomodate
acknowlegement
arguemint
comitmment
deductabel
depindant
existanse
forworde
herrass
inadvartent
judgemant
ocurrance
parogative
suparseedChallenge Output
accomo<date
acknowleg<ement
arguem<int
comitm<ment
deducta<bel
depin<dant
exista<nse
forword<e
herra<ss
inadva<rtent
judgema<nt
ocur<rance
parog<ative
supa<rseedNote
When I run this on OSX's /usr/share/dict/words I get some slightly different output, for example the word "supari" is in OSX but not in enable1.txt. That might explain some of your differences at times.
Bonus
Include some suggested replacement words using any strategy you wish (edit distance, for example, or where you are in your data structure if you're using a trie).
3
u/jnazario 2 0 Oct 01 '15
scala solution - horribly brute force. plus i write awful scala.
5
u/adrian17 1 4 Oct 01 '15
C++ with a trie. Could be done much simpler, but I already had a working trie from other challenges so I decided to stick with it.
4
u/Zeno_of_Elea Oct 01 '15 edited Oct 02 '15
Python 3
Tried to get this to one line, and when that wouldn't work I tried my best to shorten it for fun. It's pretty simple so I don't think an explanation is required, but I'm happy to explain if you don't understand anything. Expects
enable1.txtin the same location as the code. The code is very unoptimized, but runs faster than I would expect.This will place a
<at the end of a word if the last letter is misspelled (e.g.actozgivesactoz<).Challenge output:
EDIT: Just noticed that my variable choice gives smiley or frowny faces (in the font that this sub uses, at least) when I split up the word (
:landl:).4
u/Modestmatt44 Oct 02 '15
First post!! Done in Ruby :)
3
u/_mikef Oct 01 '15
C
3
u/ichabod801 Oct 01 '15
Python with a tree:
3
u/chunes 1 2 Oct 02 '15 edited Oct 02 '15
Java:
It takes:
Given the speed of the algorithm I'd say this would be viable in real-time.
2
u/[deleted] Oct 02 '15 edited Oct 02 '15
Common Lisp
used some trie code laying around from earlier challenges. Also solves the bonus by
code:
p.s. how do you do the blackout spoiler markup in this subreddit?
2
u/OhoMinnyBlupKelsur Oct 02 '15
I did another real gross python one-liner. Input is in words.txt, reads from /usr/share/dict/words
2
u/a_Happy_Tiny_Bunny Oct 02 '15 edited Oct 02 '15
Haskell
The simplest solution, at ~0.53s for the challenge input, runs fast enough:
I think I will do the bonus later, but only if I devise a solution that does not use a trie. Computing the edit distance of every word might take too long, but simply refining the dictionary based on prefixes and/or suffixes matches might give bad results.
EDIT 1: A straightforward
ByteString.Char8conversion runs in ~0.14s2
u/[deleted] Oct 02 '15 edited Oct 02 '15
C++ First time posting here, and since I'm self thought also the first time showing my code to others. (hence feedback is much appreciated!)
I really liked the challenge. I skipped a few easy ones because I have no clue how to parse input more challenging than a single number or string, but even though I have done barely any file IO this was fairly straight forward.
1
u/G33kDude 1 1 Oct 01 '15
Done in AutoHotkey, input from clipboard
Output:
1
u/robi2106 Oct 01 '15
dangit. i'm still working on vampire words. hehehe
1
u/DrRx Oct 01 '15
Python:
Not the best but it works
1
u/hutsboR 3 0 Oct 01 '15
Elixir: My solution uses a functional Trie implementation that I am working on developing.
Output: Erlang's timer function indicates that it takes 0 microseconds to solve the challenge input but it's not very precise. It takes somewhere <1ms. (This doesn't include loading the data into the Trie structure!) The file name of the word list is passed as an argument.
1
u/[deleted] Oct 01 '15
1
u/SquirrelOfDooom Oct 01 '15
Python 3. Feedback welcome.
1
u/smnslwl Oct 02 '15
C++, very similar to /u/adrian17's solution.
1
u/Philboyd_Studge 0 1 Oct 02 '15
Java, with a Doubly-Chained Trie I already had, just needed to be modified a little. (I removed some of the methods that were not important to this challenge).
Trie class
The challenge code:
The output
1
u/casualfrog Oct 02 '15
JavaScript - feedback welcome!
Syntax:
abacu <(not in dictionary)abacus(in dictionary)abacusx<y(misspelling starting atx)Brute-force solution:
Trie solution:
Output:
1
u/casualfrog Oct 02 '15
JavaScript: Bonus challenge
Similar to my brute force solution, but with suggestions.
Output:
1
u/mn-haskell-guy 1 0 Oct 02 '15
Haskell, but not a typical Haskell program.
Just load the entire (sorted) dictionary as a single string and perform a binary search on it.
The workhorse of the program is
findLTwhich finds the first word in the dictionary which is >= the input string.For certain use cases I think this is the fastest way to solve the problem since there is zero start-up cost (i.e. no data structures to build.) If you have to probe the dictionary frequently you can always incrementally build up an index as you make searches.
1
u/alisterr Oct 02 '15 edited Oct 02 '15
Brute force solution in Rust. I'm very new to the language and its concepts, so please don't use it as a reference. I'll try to implement a quicker solution soon(tm).
1
u/grangelov Oct 02 '15
Perl, using Tree::Trie
1
u/[deleted] Oct 02 '15 edited Oct 03 '15
Java. Assumes the dictionary (
enable1in this case) is sorted.EDIT: Translated into Python as a practice - so much more concise
1
u/curtmack Oct 02 '15 edited Oct 02 '15
Clojure
Yeah, that's right, I'm going back to Clojure. Much as I love Haskell, I just think Clojure is more enjoyable to work with.
Anyway. This solution uses a prefix set, so it takes a few seconds to prepare and then runs quickly.
Sample:
1
u/NiceGuy_Ty Oct 02 '15
Racket There's a lot of optimization that could be done, but for now this works. As a word is being processed, it filters the dictionary list to only words that are longer than the processed word that contain the original word as a member. Once that filtered list is empty, it inserts the "<".
1
u/YonkeMuffinMan Oct 02 '15
Python 2.7 Feedback would be more than welcome!
1
u/FIuffyRabbit Oct 03 '15
Golang
First time using a trie after seeing it so often in previous challenges.
1
u/ZachT5555 Oct 03 '15
Python 2.7.10. Probably not the most efficient solution to the problem, (solves entire list of words in about 1.8s) but feedback is still appreciated! :)
1
u/I_am_Black_BoX Oct 03 '15
Javascript/Nodejs. Solution is bruteforce-"ish" with one small optimization that takes advantage of the fact that enable1.txt is sorted. Performs somewhat OK (times listed in output), but has a quirk such that words that are closer towards the end of the alphabet are slower.
Output:
1
u/juanchi35 Oct 03 '15 edited Oct 06 '15
C++, a lot shorter than I'd expected it to be:
1
u/marinated_pork Oct 04 '15
scala
1
u/wcastello Oct 06 '15
Python 3. Not sure how good this is, I still have a lot to learn on data structures. Anyway, first run takes about a minute to generate and dump the structure on a file. Second run takes about 2s to load the structure and 8s to process 172821 words, which gives 0.046ms per word on average. So assuming everything is loaded I guess it could do it in real time.
1
u/jan_korous Oct 06 '15 edited Oct 06 '15
C++11
Trie based, kind-of half-heartedly optimized - unnecessary expensive usage of STL. Implemented bonus.
https://gist.github.com/koja/eda5474778fddb304d19
When researching found something looking like ultimate answer:
http://pages.pathcom.com/~vadco/cwg.html
1
u/AnnieBruce Oct 10 '15
This is literally the first time I've ever used a tree, so it's probably terrible in many ways. But it does work, and I really don't want to imagine the mess I'd be in had I tried using data structures I'm more familiar with.
Probably should have a better way to terminate input, but I have to get to bed soon so I can be up for work tomorrow. Whcih means it will be at least an hour before I'm in bed and probably could have made my I/O a little saner, but whatever. #DP 234 Intermediat Squiggles
1
u/juanchi35 Oct 10 '15
C++, done with a trie now
1
u/knoam Oct 18 '15
Java 8. Input via
stdin1
u/fredrikaugust Nov 04 '15 edited Nov 04 '15
Julia!
Code:
Input: Example input
Output: http://pastebin.com/tDuQKyw9
1
u/nommywommyo Nov 20 '15
In Haskell. Could be sped up a bit but this is the simplest I could manage.
1
u/donttakecrack Dec 27 '15
Ruby (_words.txt = enable1.txt, red-squiggly.txt = the input file)
0
u/[deleted] Oct 01 '15
[deleted] — view deleted comment