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 two" and "Twelve plus one".
#
#Note that if one string can be made only out of the letters
#of another, but with duplicates, we do NOT consider them
#anagrams. For example, "Elvis" and "Live Viles" would not
#be anagrams.
#Write your function here!
#Below are some lines of code that will test your
function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print: True, False, True, False, each on their own line.
print(are_anagrams("Elvis", "Lives"))
print(are_anagrams("Elvis", "Live Viles"))
print(are_anagrams("Eleven plus two", "Twelve plus one"))
print(are_anagrams("Nine minus seven", "Five minus
three"))
Python code:
#defining are_anagrams function
def are_anagrams(string1,string2):
#converting the string to lowercase and then
removing the spaces in it, then sorting the letters in it and
checking if they
#are same and returns true if they are same else
False
return(sorted(list("".join(string1.lower().split())))==sorted(list("".join(string2.lower().split()))))
#testing and printing for different testcases
print(are_anagrams("Elvis","Lives"))
print(are_anagrams("Elvis","Live Viles"))
print(are_anagrams("Eleven plus two","Twelve plus one"))
print(are_anagrams("Nine minus seven","Five minus three"))
Screenshot:
Output:
Get Answers For Free
Most questions answered within 1 hours.