In: Computer Science
Two words form a "metathesis pair" if you can transform one into the other by swapping two letters; for example, "converse" and "conserve". Write a function metathesis(word1, word2) to check if the two words can form a metathesis pair. The function will return a Boolean value (True or False). python code without using Zip method
PYTHON CODE
#metathesis() function is used to check whether two words form metathesis pair or not.
#If one word transform into another word by swapping two letters, then return True
#Otherwise return False
#True denotes that two words forms metathesis pair
#False denotes that two words doesn't form metathesis pair
def metathesis(word1, word2):
letterWord1 = []
letterWord2 = []
if len(word1) == len(word2): #Length of string should be same.
lengthOfString = len(word1)
for i in range(lengthOfString):
if word1[i]!=word2[i]:
letterWord1.append(word1[i])
letterWord2.append(word2[i])
if len(letterWord1) == 2 and len(letterWord2)==2 and sorted(letterWord1) == sorted(letterWord2):
return True
return False
word1 = input("Enter first word:")
word2 = input("Enter second word:")
print(metathesis(word1, word2))
PYTHON CODE IM,AGE
OUTPUT IMAGE