Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Implement feline_fixes, which is a diff function that returns the minimum number of edit operations needed to transform the start word into the goal word.
- There are three kinds of edit operations:
- Add a letter to start,
- Remove a letter from start,
- Substitute a letter in start for another.
- Each edit operation contributes 1 to the difference between two words.
- >>> big_limit = 10
- >>> feline_fixes("cats", "scat", big_limit) # cats -> scats -> scat
- 2
- >>> feline_fixes("purng", "purring", big_limit) # purng -> purrng -> purring
- 2
- >>> feline_fixes("ckiteus", "kittens", big_limit) # ckiteus -> kiteus -> kitteus -> kittens
- 3
- We have provided a template of an implementation in cats.py. This is a recursive function with three recursive calls. One of these recursive calls will be similar to the recursive call in sphinx_swap.
- You may modify the template however you want or delete it entirely.
- If the number of edits required is greater than limit, then feline_fixes should return any number larger than limit and should minimize the amount of computation needed to do so.
- These two calls to feline_fixes should take about the same amount of time to evaluate:
- >>> limit = 2
- >>> feline_fixes("ckiteus", "kittens", limit) > limit
- True
- >>> sphinx_swap("ckiteusabcdefghijklm", "kittensnopqrstuvwxyz", limit) > limit
- True
- def feline_fixes(start, goal, limit):
- """A diff function that computes the edit distance from START to GOAL."""
- # gl = goal
- # st = start
- # word = ''
- # for i in st:
- # for x in gl:
- # if i == x:
- # word += x
- # gl.replace(x, '')
- # break
- # return abs(len(goal) - len(word))
- #
- # BEGIN PROBLEM 7
- if limit < 0:
- return 2
- #basecase
- if start == goal: # Fill in the condition
- # BEGIN
- return 0
- # END
- if len(start) < 2 or len(goal) < 2:
- return 1
- if(start[0] == goal[0]):
- substitute_diff = feline_fixes(start[1:], goal[1:], limit - 1)
- add_diff = limit
- remove_diff = limit
- else:
- add_diff = 1 + feline_fixes(goal[0] + start, goal, limit - 1)
- remove_diff = 1 + feline_fixes(start[1:], goal, limit - 1)
- substitute_diff = 1 + feline_fixes(start[1:], goal[1:], limit - 1)
- # BEGIN
- return min(add_diff, remove_diff, substitute_diff)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement