-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoneaway.py
More file actions
62 lines (46 loc) · 1.14 KB
/
oneaway.py
File metadata and controls
62 lines (46 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Given two strings, are they, at most, one edit away?
>>> one_away("make", "make")
True
>>> one_away("make", "fake")
True
>>> one_away("task", "take")
False
>>> one_away("ask" ,"asks")
True
>>> one_away("asks", "ask")
True
>>> one_away("act", "tact")
True
>>> one_away("fat", "fact")
True
>>> one_away("yes", "no")
False
>>> one_away("dessert", "desert")
True
>>> one_away("paint", "painting")
False
"""
def one_away(w1, w2):
"""Given two strings, are they, at most, one edit away?"""
j = 0
fail_count = 0
if len(w2) > len(w1):
w1, w2 = w2, w1
if len(w1) - len(w2) > 1:
return False
for i in range(len(w1)):
if w1[i] != w2[j]:
fail_count += 1
if fail_count > 1:
return False
if len(w1) == len(w2):
j += 1
else:
j += 1
if j >= len(w2):
return True
return True
if __name__ == '__main__':
import doctest
if doctest.testmod().failed == 0:
print "\n*** ALL TESTS PASSED; NICE JOB! ***\n"