Problem 5 (200pts): autocorrect
Implement autocorrect
, which takes a user_word
, a list of all valid_words
, a diff_function
, and a limit
.
If the user_word
is contained inside the valid_words
list, autocorrect
returns that word. Otherwise, autocorrect
returns the word from valid_words
that has the lowest difference from the provided user_word
based on the diff_function
. However, if the lowest difference between user_word
and any of the valid_words
is greater than limit
, then user_word
is returned instead.
A diff function takes in three arguments, which are the two strings to be compared (first the user_word
and then a word from valid_words
), as well as the limit
. The output of the diff function, which is a non-negative number, represents the amount of difference between the two strings.
Assume that user_word
and all elements of valid_words
are lowercase and have no punctuation.
Important: if multiple strings have the same lowest difference according to the diff_function
, autocorrect
should return the string that appears fi rst in valid_words
.
Hint: Try using
max
ormin
with the optionalkey
argument.
# Test your implementation
python ok -q 05