Answer to Question #43113 in Python for shami

Question #43113
Write a class/function to return True if 2 input strings are anagram to each other. string1 is an anagram of string2 if string2 can be obtained by rearranging the characters in string1. Example:
string1 = ‘smart’, string2 = ‘marts’  result = True
string1 = ‘secure’, string2 = ‘rescue’  result = True
1
Expert's answer
2014-06-06T11:45:57-0400
# Answer on Question #43113 - Programming - Python
from collections import Counter
def anagrams(s1, s2):
return Counter(s1) == Counter(s2)
def test():
assert anagrams('', '')
assert anagrams('a', 'a')
assert not anagrams('a', 'b')
assert anagrams('smart', 'marts')
assert anagrams('secure', 'rescue')
assert not anagrams('secure', 'rescu')
assert anagrams('abcabc', 'aabbcc')
print "Tests passed"
test()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS