Created
December 23, 2021 21:07
-
-
Save lgliducik/afa2b62a48504d723da2b417a5472994 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
import pytest | |
def diff(first, second): | |
len_diff = len(first) - len(second) | |
if abs(len_diff) > 1: | |
return False | |
if len_diff > 0: | |
short_str = second | |
long_str = first | |
else: | |
short_str = first | |
long_str = second | |
offset = 0 | |
error_counter = 0 | |
for short_index in range(len(short_str)): | |
if short_str[short_index] == long_str[short_index + offset]: | |
continue | |
if error_counter > 0: | |
return False | |
error_counter += 1 | |
if len_diff == 0: | |
continue | |
offset += 1 | |
if short_str[short_index] != long_str[short_index + offset]: | |
return False | |
if len_diff: | |
return True | |
else: | |
return error_counter == 1 | |
def test_equal_str(): | |
assert not diff("1234", "1234") | |
def test_replace_sym_in_middle(): | |
assert diff("1234", "1254") | |
def test_insert_sym_in_middle(): | |
assert diff("12345", "125345") | |
def test_delete_sym_in_middle(): | |
assert diff("1234", "124") | |
def test_one_sym_insert_sym_in_the_end(): | |
assert diff("1", "12") | |
def test_insert_sym_in_the_end(): | |
assert diff("1234", "12345") | |
def test_delete_sym_in_the_end(): | |
assert diff("12", "1") | |
def test_insert_2_sym(): | |
assert not diff("1234", "123474") | |
def test_too_much_changes(): | |
assert not diff("12345", "1255555345") | |
def test_delete_2_sym(): | |
assert not diff("1234", "12") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment