Last active
May 3, 2023 23:34
-
-
Save mjmeilahn/d9ee0daad38bc4bdf26341aaf1973765 to your computer and use it in GitHub Desktop.
Python: Comparing Strings
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
# Compare TWO strings and return TRUE if the 2nd String only requires one change to match the original string. | |
# Otherwise return FALSE. | |
def compareStrings(orig, diff): | |
count = 0 | |
for char in list(diff): | |
match = [i for i in list(orig) if i == char] | |
if len(match) == 1: count += 1 | |
return True if count == (len(orig) - 1) else False | |
print(compareStrings('make', 'rake')) # True | |
print(compareStrings('make', 'mke')) # True | |
print(compareStrings('make', 'ake')) # True | |
print(compareStrings('make', 'ekam')) # False | |
print(compareStrings('make', 'make')) # False | |
print(compareStrings('make', 'rare')) # False | |
print(compareStrings('make', 'ke')) # False | |
print(compareStrings('make', 'ma')) # False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment