Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Surjection Strings (with explanation comments) solution in Clear category for Surjection Strings by SpeakingCucumber
def isometric_strings(first_word, second_word):
# Here code will check if first word and second word is the same.
# Because if yes - this means that both words fulfills requirements of homomorphism
# And no another manipulations needed.
if first_word == second_word:
return True
# If not - code will create a dict where letters from first word will be keys
# and letters from another word will be values
else:
dict_letter_to_letter = {}
for index, every_letter in enumerate(first_word):
# Code will iterate through the letters of the first word
# If there is no such letter in keys of dict - code will add it
if every_letter not in dict_letter_to_letter.keys():
dict_letter_to_letter[every_letter] = second_word[index]
# If such letter from the first word is not in the keys of dict
# Code will check if it's value is equal to the current letter from the second word
# If its not equal - this means that letter does not fulfill requirements of homomorphism
# Thus, words does not fulfill requirements of homomorphism
else:
letter_to_check = dict_letter_to_letter[every_letter]
if letter_to_check != second_word[index]:
return False
# We assume that both words fulfill homomorphism's requirements by default, thus - return True by default
return True
if __name__ == "__main__":
print("Example:")
print(isometric_strings("add", "egg"))
# These "asserts" are used for self-checking and not for an auto-testing
assert isometric_strings("add", "egg") == True
assert isometric_strings("foo", "bar") == False
assert isometric_strings("", "") == True
assert isometric_strings("all", "all") == True
assert isometric_strings("gogopy", "doodle") == False
print("Coding complete? Click 'Check' to earn cool rewards!")
Dec. 5, 2021