Problem 6 (200pts): sphinx_fixes
Implement sphinx_fixes
, which is a diff function that takes two strings.
It returns the minimum number of characters that must be changed in the start
word in order to transform it into the goal
word.
If the strings are not of equal length, the difference in lengths is added to the total.
Here are some examples:
>>> big_limit = 10
>>> sphinx_fixes("nice", "rice", big_limit) # Substitute: n -> r
1
>>> sphinx_fixes("range", "rungs", big_limit) # Substitute: a -> u, e -> s
2
>>> sphinx_fixes("pill", "pillage", big_limit) # Don't substitute anything, length difference of 3.
3
>>> sphinx_fixes("roses", "arose", big_limit) # Substitute: r -> a, o -> r, s -> o, e -> s, s -> e
5
>>> sphinx_fixes("rose", "hello", big_limit) # Substitute: r->h, o->e, s->l, e->l, length difference of 1.
5
Important: If the number of characters that must change is greater than
limit
, thensphinx_fixes
should return any number larger thanlimit
and should minimize the amount of computation needed to do so.These two calls to
sphinx_fixes
should take about the same amount of time to evaluate:>>> limit = 4 >>> sphinx_fixes("roses", "arose", limit) > limit True >>> sphinx_fixes("rosesabcdefghijklm", "arosenopqrstuvwxyz", limit) > limit True
Before writing any code, unlock the tests to verify your understanding of the question:
$ python ok -q 06 -u
Once you are done unlocking, begin implementing your solution. You can check your correctness with:
$ python ok -q 06
Try turning on autocorrect in the GUI. Does it help you type faster? Are the corrections accurate?