Question: Write the autocorrect function, which takes as arguments a user_word, a list of all valid_words, a diff_function, and a limit. If the user_word is contained

Write the autocorrect function, which takes as arguments 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 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. Example of it working: >>> from cats import autocorrect, lines_from_file >>> abs_diff = lambda w1, w2, limit: abs(len(w2) - len(w1)) >>> autocorrect("cul", ["culture", "cult", "cultivate"], abs_diff, 10) returns "cult" def autocorrect(user_word, valid_words, diff_function, limit): """Returns the element of VALID_WORDS that has the smallest difference from USER_WORD. Instead returns USER_WORD if that difference is greater than LIMIT. """

Important: if multiple strings have the same lowest difference according to the diff_function, autocorrect should return the string that appears first in valid_words.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!