Question

Two words form a "metathesis pair" if you can transform one into the other by swapping...

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

Homework Answers

Answer #1
def metathesis(word1, word2):
    are_anagrams = True
    if len(word1)==len(word2):
        for x in word1:
            # Checking count of character x is same
            if word1.count(x) != word2.count(x):
                are_anagrams = False
        for x in word2:
            # Checking count of character x is same
            if word1.count(x) != word2.count(x):
                are_anagrams = False
        if are_anagrams:
            count = 0
            for i in range(len(word1)):
                if word1[i]!=word2[i]:
                    count += 1
            return count==2
    return False


# Testing
print(metathesis("converse","conserve"))

True

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Python #Write a function called are_anagrams. The function should #have two parameters, a pair of strings....
Python #Write a function called are_anagrams. The function should #have two parameters, a pair of strings. The function should #return True if the strings are anagrams of one another, #False if they are not. # #Two strings are considered anagrams if they have only the #same letters, as well as the same count of each letter. For #this problem, you should ignore spaces and capitalization. # #So, for us: "Elvis" and "Lives" would be considered #anagrams. So would "Eleven plus...
Python The capitalize function should receive a word as a parameter in the form of a...
Python The capitalize function should receive a word as a parameter in the form of a string and return one version where the first character is written in capital letters. Other characters must be unchanged. For example ‘hey’ become ‘Hey’. Write the function clearly. Consider that str has the method upper that returns a new string where all characters are capitalized
This is a code. In words how can I describe this? This is the code. Private...
This is a code. In words how can I describe this? This is the code. Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim word As String = "courage" Dim letter As String = "" Dim numVowels As Integer = 0 For i As Integer = 0 To (word.Length − 1) letter = word.Substring(i, 1) If IsVowel(letter) Then numVowels += 1 End If Next txtBox.Text = CStr(numVowels) End Sub Function IsVowel(letter As String) As Boolean letter = letter.ToUpper If (letter = "A") Or...
Two stars are in a binary pair – one is a G2V and the other is...
Two stars are in a binary pair – one is a G2V and the other is a K0V. Assume they formed at the same time. These two stars are close enough that they could exchange material between them. Which of the following events could happen in the future as these stars evolve? a. A type 1 supernova could happen b. A type 2 supernova could happen c. A dwarf nova could happen d. A radio pulsar could form e. A...
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Anagram checking Design an algorithm for checking whether two given words are anagrams, i.e., whether one...
Anagram checking Design an algorithm for checking whether two given words are anagrams, i.e., whether one word can be obtained by permuting the letters of the other. (For example, the words tea and eat are anagrams.)
1.Write a function which takes a string that contains two words separated by any amount of...
1.Write a function which takes a string that contains two words separated by any amount of whitespace, and returns a string in which the words are swapped around and the whitespace is preserved. Hint: use regular expressions where \s detects the whitespaces in a string. Example: given "Hello     World", return "World         Hello" 2.Pandas exercises: Write a python program using Pandas to create and display a one-dimensional array-like object containing an array of data. Write a python program using Pandas to...
This is an intro to Python question: #Write a function called linear() that takes two parameters...
This is an intro to Python question: #Write a function called linear() that takes two parameters #- a list of strings and a string. Write this function so #that it returns the first index at which the string is #found within the list if the string is found, or False if #it is not found. You do not need to worry about searching #for the search string inside the individual strings within #the list: for example, linear(["bobby", "fred"], "bob") #should...