# 5. autocorrect
def autocorrect(entered_word: str, word_list: list[str], diff_function, limit: int) -> str:
# BEGIN PROBLEM 5
if entered_word in word_list:
return entered_word
else:
check_lst = []
for source_word in word_list:
if diff_function(entered_word, source_word, limit) <= limit:
check_lst.append(True)
if not check_lst:
return entered_word
else:
closest_word = word_list[0]
for source_word in word_list:
if diff_function(entered_word, source_word, limit) < diff_function(entered_word, closest_word, limit):
closest_word = source_word
return closest_word
# END PROBLEM 5
# 6.furry_fixes
def furry_fixes(entered: str, source: str, limit: int) -> int:
# BEGIN PROBLEM 6
if limit < 0:
return 0
if not entered or not source:
return len(entered) + len(source)
if entered[0] == source[0]:
return furry_fixes(entered[1:], source[1:], limit)
else:
return furry_fixes(entered[1:], source[1:], limit - 1) + 1
# END PROBLEM 6
# 7.minimum_mewtations
def minimum_mewtations(entered: str, source: str, limit: int) -> int:
# BEGIN
if limit < 0:
return 0
if entered == source:
return 0
if len(entered) == 0:
return len(source)
if len(source) == 0:
return len(entered)
if entered[0] == source[0]:
return minimum_mewtations(entered[1:], source[1:], limit)
else:
add_diff = 1 + minimum_mewtations(entered, source[1:], limit - 1)
remove_diff = 1 + minimum_mewtations(entered[1:], source, limit - 1)
sub_diff = 1 + minimum_mewtations(entered[1:], source[1:], limit - 1)
return min(add_diff, remove_diff, sub_diff)
# END