Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

A typing speed test loads up a dictionary of words, and gives you one to type: pinball. You, with your fast but inaccurate fingers, type in pinbnal. Darn it!

Make a program that will print out a random word from a dictionary, then ask for the user to type this word back, and finally return whether or not they got the word right. The catch is: the program must accept minor typos.

Accept a typo if it has only one of the following:

  • one letter is repeated in the input 1 or 2 more times. Only accept the letter if it is directly adjacent to a letter it is next to.
  • one letter in the word is repeated 1 time.
  • 2-4 of the letters in the input are scrambled.

Some acceptable typos of the word pinball are:
pingball pinbala pinbanll
Some unacceptable typos:
pibnannl maxwell pinbalgl

Shortest code wins. Good luck.

share|improve this question
3  
an ingteresring undereretanding ohf what a minoe typpe is... what exactly does "scrambled" include, apparently it's not just swapping letters? –  leftaroundabout Jul 4 '12 at 6:57
    
e.x. water -> waert (ert is scrambled ter) or wtrae (trae is scrambled ater) –  beary605 Jul 4 '12 at 7:02
2  
But what's pinbala or pinbnal then? The second l is missing completely in both cases. –  leftaroundabout Jul 4 '12 at 7:35
2  
And pingball adds a new letter - it's fundamentally the same error as pinbalgl. –  Peter Taylor Jul 4 '12 at 8:07
    
I've fixed the parameters to fit my examples. –  beary605 Jul 4 '12 at 15:10

1 Answer 1

Mathematica 179

Edit: As Primo correctly notes, the following does not adhere to the constraint of pardoning only those intrusions from QWERTY neighbors. DL distance, of course, has nothing to do with distance on a keyboard. Oh, well. I'll leave up my response, which someone with more patience will easily better.


Code

d = DictionaryLookup[]; i = d[[RandomInteger@{1, Length@d}]];o = InputString["Type: " <> i];
Print["in: ", i, "\nout: ", o, "\n", Switch[i~DamerauLevenshteinDistance~o, 0, "perfect",
1, "ok", _, "wrong"]]

Explanation

  1. d = DictionaryLookup[] stores a built-in dictionary of 92 k English words in d.
  2. i = d[[RandomInteger@{1, Length@d}]] selects a random word and stores it in i.
  3. o = InputString["Type: " <> i]; opens a dialog window (see figure) with the prompt "Type [i]."
  4. i~DamerauLevenshteinDistance~o computes the Damerau-Levenshtein distance beween the requested word and the word typed by the user. If the DL distance = 0, "perfect"; if the DL distance = 1, "ok"; otherwise, "wrong".

tureen


Test Results

For the first 7 tests, the input string, i, was manually set to "pinball". The results were as desired with the exception of "pinbalgl", which was considered acceptable because it returns a DL distance value of 1.

in: pinball
out: pinball
perfect

in: pinball
out: pingball
ok

in: pinball
out: pinbala
ok

in: pinball
out: pinbanll
ok

in: pinball
out: pinbannl
wrong

in: pinball
out: maxwell
wrong

in: pinball
out: pinbalgl
ok

in: tureen
out: turen
ok

in: feeder
out: feedr
ok

in: tarmacs
out: tarmax
wrong

share|improve this answer
    
I like the interface :D Not sure if it answers the question exactly as stated though. Judging from the examples, (I think) it should accept an inserted character (and here's where the wording gets a bit unclear) if and only if one or both of the two characters it is adjacent to in the string is also adjacent to it on a keyboard (presumably qwerty). A test slightly stronger than the Levenshtein distance. –  primo Dec 17 '12 at 19:06
    
@primo. I see your point. I missed entirely the constraint about adjacent keys. Clearly, that would require considerably more coding. –  David Carraher Dec 17 '12 at 19:12
    
Even so, I would still consider this an acceptable solution, given the lack of participation. You already have my +1 –  primo Dec 17 '12 at 19:33
    
Thanks. I thought the question looked lonely and abandoned being unanswered. So I gave it my best shot. To craft a fully suitable response, I'd need more time than I have. –  David Carraher Dec 17 '12 at 19:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.