-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1.5-one-away.py
More file actions
53 lines (40 loc) · 1.19 KB
/
Copy path1.5-one-away.py
File metadata and controls
53 lines (40 loc) · 1.19 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
""" 1.5 One Away: There are three types of edits that can be performed on strings: insert a character, remove a character, or replace a character. Given two strings, write a function to check if they are one edit (or zero edits) away. """
def one_edit_away(s1, s2):
""" Example inputs and outputs
>>> one_edit_away("pale", "ple")
True
>>> one_edit_away("pales", "pale")
True
>>> one_edit_away("pale", "bale")
True
>>> one_edit_away("pale", "bake")
False
"""
if abs(len(s1) - len(s2)) > 1:
return False
if len(s1) >= len(s2):
longer = s1
shorter = s2
else:
longer = s2
shorter = s1
edit = False
l = s = 0
while l < len(longer) and s < len(shorter):
if longer[l] != shorter[s]:
if edit:
return False
edit = True
if len(longer) == len(shorter):
s += 1
l += 1
else:
l += 1
else:
s += 1
l += 1
return True
if __name__ == "__main__":
import doctest
if doctest.testmod().failed == 0:
print "tests pass"