Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. file_name = input()
  2. word = input()
  3.  
  4. anagrams = []
  5.  
  6. with open(file_name) as f:
  7.  
  8.     for line in f.readlines():
  9.         temp_line = line.replace('\n', '')
  10.         has_anagram = True
  11.         char_list = sorted(list(word))
  12.         file_char_list = sorted(list(temp_line))
  13.  
  14.         if len(char_list) == len(file_char_list):
  15.  
  16.             for i in range(len(char_list)):
  17.                 try:
  18.                     if char_list[i] == file_char_list[i]:
  19.                         has_anagram = True
  20.                     else:
  21.                         has_anagram = False
  22.                         break
  23.                 except IndexError as e:
  24.                     has_anagram = False
  25.                     break
  26.  
  27.             if has_anagram and temp_line != word:
  28.                 anagrams.append(temp_line)
  29.  
  30.  
  31. if len(anagrams) > 0:
  32.     for word in sorted(anagrams):
  33.         print(word)
  34. else:
  35.     print('NO ANAGRAMS')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement