Last active
March 10, 2018 12:45
-
-
Save highoncarbs/bd7998a3ae73d026f8d8a2dcbff68640 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def nearly_equal(str1, str2): | |
m, n = len(str1) + 1, len(str2) + 1 | |
string_table = {} | |
for i in range(m): | |
string_table[i, 0] = i | |
for j in range(n): | |
string_table[0, j] = j | |
for i in range(1, m): | |
for j in range(1, n): | |
if str1[i-1] == str2[j-1]: | |
counter = 0 | |
else: | |
counter = 1 | |
string_table[i, j] = min(string_table[i, j - 1] + 1, | |
string_table[i - 1, j] + 1, | |
string_table[i - 1, j - 1] + counter) | |
return string_table[i, j] == 1 | |
def test_sample_1(): | |
assert nearly_equal('python', 'jython') == True | |
def test_sample_2(): | |
assert nearly_equal('man', 'woman') == False | |
def test_sample_3(): | |
assert nearly_equal('cherry', 'berry') == False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment