3
\$\begingroup\$

This question already has an answer here:

Oops, Auto-correct.

You have a word. But it's wrong. You wonder what word it would be. Thankfully, being a code golfer, You can make a code to take a rough guess.

I/O

You will be given a (wrong, all lowercase) word, and a '(All lowercase)Dictionary list'. You have to output the word with the least Levenshtein distance to the wrong word. If there are multiple words of the same levenshtein distance, use the last one in the list.

Examples

Input => Output
(["apple", "banana", "orange", "pineapple"], "baenanae") => "banana"
(["zeal", "zealot", "zealotry", "zealots", "zealous", "zealously", "zealousness"], "zealousl") => "zealously"
(["abductors", "abducts", "abe", "abeam", "abel"], "") => "abe"
\$\endgroup\$

marked as duplicate by Peter Taylor code-golf Sep 10 '17 at 5:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1
\$\begingroup\$

Python 3 + pylev, 77 bytes

lambda m,s:min({levenshtein(x,s):x for x in m}.items())[1]
from pylev import*

Try it on repl.it!

\$\endgroup\$
  • \$\begingroup\$ Shouldn't that be [-1]? Ah no my bad... what happens with multiple minima? \$\endgroup\$ – Jonathan Allan Sep 10 '17 at 5:16
  • \$\begingroup\$ See my edit above - I assumed you wanted to get the rightmost (as per requirements) but noticed it was getting the second of two values. Try the zealousl example. (possibly for x in m[::-1] will do it) \$\endgroup\$ – Jonathan Allan Sep 10 '17 at 5:18
  • \$\begingroup\$ @JonathanAllan I had missed that part of the spec, but nevertheless the correct answer is always given. If any of the distances are the same, the earlier ones will be overwritten by the later ones, since a dictionary can only have one value for a key. \$\endgroup\$ – LyricLy Sep 10 '17 at 5:26
  • \$\begingroup\$ Oh of course, sweet side-effects Batman :) \$\endgroup\$ – Jonathan Allan Sep 10 '17 at 5:27
1
\$\begingroup\$

05AB1E, 6 bytes

Σ.L(}¤

Try it online! (note: the trailing newline on the input is necessary for the empty string case.)

How?

Σ.L(}¤ - implicitly push the first input (the dictionary list)
Σ   }  - sort the list by:
       -   implicitly push second input (the misspelled word)
 .L    -   Levenshtein distance
   (   -   negated
     ¤ - tail the sorted list  and place the entry on the top of the stack
       - implicitly print top of stack
\$\endgroup\$

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