Guest User

Untitled

a guest
Mar 17th, 2024
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. import functools
  2. from collections import defaultdict
  3.  
  4. with open("wordlist.txt") as f:
  5.     words = [word.strip() for word in f]
  6.  
  7. pangrams = [word for word in words if len(frozenset(word)) == 7]
  8.  
  9. def word_score(w):
  10.     if len(w) == 4:
  11.         return 1
  12.     points = len(w)
  13.     if len(set(w)) == 7:
  14.         points += 7
  15.     return points
  16.  
  17. subset_score = defaultdict(int)
  18.  
  19. for word in words:
  20.     subset_score[frozenset(word)] += word_score(word)
  21.  
  22. @functools.cache
  23. def partial(included, remaining):
  24.     if remaining == "":
  25.         return subset_score[frozenset(included)]
  26.     nextinc = "".join(sorted(included + remaining[0]))
  27.     return partial(included, remaining[1:]) + partial(nextinc, remaining[1:])
  28.  
  29. minscores = []
  30. for pangram in pangrams:
  31.     letters = "".join(sorted(set(pangram)))
  32.     s = 100000000
  33.     for i, let in enumerate(letters):
  34.         s = min(s, partial(letters[i], letters[:i] + letters[i+1:]))
  35.     minscores.append((pangram, s))
  36.  
  37. minscores.sort(key=lambda x: x[1])
  38. print(minscores[:10])
  39.  
Advertisement
Add Comment
Please, Sign In to add comment