Advertisement
Guest User

findpananagrams2.py

a guest
Feb 18th, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. import itertools
  2.  
  3. def get_word_variations(word):
  4.     return ["".join(seq) for seq in itertools.permutations(word)]
  5.  
  6. def find_pananagrams(words, report_threshold):
  7.     wordset = set(words)
  8.     for word in words:
  9.         if word in wordset:
  10.             valid_variations = [var for var in get_word_variations(word) if var in wordset]
  11.             wordset.difference_update(valid_variations)
  12.             score = len(valid_variations)
  13.             if score >= report_threshold:
  14.                 print score, "-", ", ".join(set(valid_variations))
  15.                
  16. if __name__ == "__main__":
  17.     f = open("3letterwords.txt")
  18.     words = [line.strip() for line in f.readlines()]
  19.     find_pananagrams(words, 4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement