Problem 6 (200pts): sphinx_fixes
注意:本题不强制要求你使用递归。 如果你在2022-10-26 23:45前下载了代码,请手动进行以下修改删除此限制条件。 如果你不手动修改,本地将无法通过测试,但不影响OJ上的测试。
--- a/tests/06.py +++ b/tests/06.py @@ -96,9 +96,6 @@ test = { 'bird' >>> autocorrect("gest", small_words_list, sphinx_fixes, 10) 'nest' - >>> # ban iteration, list comprehensions - >>> test.check('cats.py', 'sphinx_fixes', ['While', 'For', 'ListComp']) - True """, 'hidden': False, 'locked': False,
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) # Substitue: 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 >>> feline_fixes("roses", "arose", limit) > limit True >>> feline_fixes("rosesabcdefghijklm", "arosenopqrstuvwxyz", limit) > limit True
Before writing any code, unlock the tests to verify your understanding of the question:
$ python3 ok -q 06 -u
Once you are done unlocking, begin implementing your solution. You can check your correctness with:
$ python3 ok -q 06
Try turning on autocorrect in the GUI. Does it help you type faster? Are the corrections accurate?