Advertisement
Guest User

Untitled

a guest
Aug 5th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys;
  2.  
  3. inputstring = sys.stdin.readline()
  4. inputstring = inputstring.strip()
  5. inputstring = inputstring.replace("\n",' ')
  6. inputstring = inputstring.replace(".",' ')
  7. inputarray = inputstring.split(" ")
  8. inputstring = ''
  9. dictionary = { }
  10.  
  11. iterator = 0
  12. for word in inputarray:
  13.     iterator += 1
  14.     lower = word.lower()
  15.     word_hash = ''.join(sorted(lower))
  16.     if not word_hash in dictionary:
  17.         dictionary[word_hash] = { 'first':iterator,
  18.                          'words':[] }
  19.     if not lower in dictionary[word_hash]['words']:
  20.         dictionary[word_hash]['words'].append(lower)
  21.  
  22. anagrams = []
  23. for word_hash in dictionary:
  24.     if len(dictionary[word_hash]['words']) > 1:
  25.         anagrams.append(dictionary[word_hash]);
  26.  
  27. length_of_array = len(anagrams)
  28. anagrams = sorted( array, key=lambda t: t['first'] )
  29.  
  30. for x in range(0,length_of_array):
  31.     print( '\n' +  ' '.join( anagrams[x]['words'] ) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement