Advertisement
Guest User

code HELP

a guest
Feb 27th, 2020
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. 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.
  2.  
  3. There are three kinds of edit operations:
  4.  
  5. Add a letter to start,
  6. Remove a letter from start,
  7. Substitute a letter in start for another.
  8. Each edit operation contributes 1 to the difference between two words.
  9.  
  10. >>> big_limit = 10
  11. >>> feline_fixes("cats", "scat", big_limit) # cats -> scats -> scat
  12. 2
  13. >>> feline_fixes("purng", "purring", big_limit) # purng -> purrng -> purring
  14. 2
  15. >>> feline_fixes("ckiteus", "kittens", big_limit) # ckiteus -> kiteus -> kitteus -> kittens
  16. 3
  17. 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.
  18.  
  19. You may modify the template however you want or delete it entirely.
  20.  
  21. 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.
  22.  
  23. These two calls to feline_fixes should take about the same amount of time to evaluate:
  24.  
  25. >>> limit = 2
  26. >>> feline_fixes("ckiteus", "kittens", limit) > limit
  27. True
  28. >>> sphinx_swap("ckiteusabcdefghijklm", "kittensnopqrstuvwxyz", limit) > limit
  29. True
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. def feline_fixes(start, goal, limit):
  41. """A diff function that computes the edit distance from START to GOAL."""
  42. # gl = goal
  43. # st = start
  44. # word = ''
  45. # for i in st:
  46. # for x in gl:
  47. # if i == x:
  48. # word += x
  49. # gl.replace(x, '')
  50. # break
  51. # return abs(len(goal) - len(word))
  52. #
  53.  
  54. # BEGIN PROBLEM 7
  55. if limit < 0:
  56. return 2
  57. #basecase
  58. if start == goal: # Fill in the condition
  59. # BEGIN
  60. return 0
  61. # END
  62. if len(start) < 2 or len(goal) < 2:
  63. return 1
  64.  
  65. if(start[0] == goal[0]):
  66. substitute_diff = feline_fixes(start[1:], goal[1:], limit - 1)
  67. add_diff = limit
  68. remove_diff = limit
  69. else:
  70. add_diff = 1 + feline_fixes(goal[0] + start, goal, limit - 1)
  71. remove_diff = 1 + feline_fixes(start[1:], goal, limit - 1)
  72. substitute_diff = 1 + feline_fixes(start[1:], goal[1:], limit - 1)
  73. # BEGIN
  74. return min(add_diff, remove_diff, substitute_diff)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement