In: Computer Science
Having some trouble with this python question. An answer would be greatly appreciated!
#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"))
I have uploaded the images of the code,typed code and output of the code. I provided explanation using comments.
Some predefined functions that are used in the program.
1)String.replace(char1,char2):It replaces the all char1 in the
string with char2
2)String.lower():Convert the the string into Lowercase
3)len(list):return the length of the List.
4)list.count(x):return the number of occurrence of character x in
the list.
Images of the code:
Note:You may not get indented code by copying the typed code(below) .Please refer code images and indent the copied code.
Typed code:
#Function named 'are_anagrams' that take a pair of strings as
parameters
def are_anagrams(string1,string2):
#As we have to Ignore spaces.
#Removing space(s) in the String1 and String2
string1=string1.replace(" ","");
string2=string2.replace(" ","");
#As we have to Ignore case
#Converting Both the Strings into Lowercase
string1=string1.lower()
string2=string2.lower()
#Converting Both strings into a List Characters
string1=list(string1)
string2=list(string2)
#Comparing the length of the String1 and String2
if(len(string1)==len(string2)):
#Comparing Occurrence Of each Character in String1, String2 are
equal
for x in string1:
if(string1.count(x)!=string2.count(x)):
#If the Number of Occurrence of a character varies then return
false
return False
#If the Number of Occurrence of every character eqaul then return
false
return True
else:
#If the length varies then return False
return False
#Some test Cases
print(are_anagrams("Elvis","Lives"))
print(are_anagrams("Elvis","Lives Viles"))
print(are_anagrams("Eleven plus two","Twelve plus one"))
print(are_anagrams("Nine minus seven","Five minus three"))
Output:
If You Have Any Doubt's please ask using comments
Have A Great Day!