from collections import OrderedDict def frequency(test_str): all_freq = {} for i in test_str: if i in all_freq: all_freq[i] += 1 else: all_freq[i] = 1 ordered = OrderedDict(sorted(all_freq.items())) return ordered def is_anagram(A, B): if frequency(A) == frequency(B): print("Yes, it's an anagram.") else: print("No, it's not an anagram.") testA = "apple macintosh" testB = "laptop machines" testC = "aaddccaa" print(testA,frequency(testA)) print(testB,frequency(testB)) print(testC,frequency(testC)) print("Test A and B:") is_anagram(testA,testB) print("Test A and C:") is_anagram(testA,testC)