Created
March 20, 2014 12:02
-
-
Save mailpraveens/9662250 to your computer and use it in GitHub Desktop.
Function to check for anagrams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#method to check for anagrams within a set | |
from operator import itemgetter | |
def checkForAnagrams(input): | |
temp = dict() | |
#Sort the input values individually | |
for k,v in input.items(): | |
temp[k]=''.join(sorted(v)) | |
#Now sort the entire list based on the value of the key | |
temp2 = sorted(temp.items(),key = itemgetter(1)) | |
#Group the similar names together and return the list | |
returnVal = [] | |
for x in temp2: | |
returnVal.append(input[x[0]]) | |
return returnVal | |
def main(): | |
print(checkForAnagrams({1:"cat", 2:"dog", 3:"tac", 4:"god", 5:"act"})) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment