Skip to content

Instantly share code, notes, and snippets.

@hrldcpr
Last active March 27, 2020 00:30
Show Gist options
  • Save hrldcpr/95b3a323bd7f1e3a73e856b3d6c31e5e to your computer and use it in GitHub Desktop.
Save hrldcpr/95b3a323bd7f1e3a73e856b3d6c31e5e to your computer and use it in GitHub Desktop.
import collections
from typing import Counter, List, Set, Tuple
def subanagrams(letters: Counter[str], words: Set[str], min_len=4) -> List[Tuple[str, Counter[str]]]:
anagrams = []
for word in words:
if len(word) < min_len: continue
rest = letters.copy()
rest.subtract(collections.Counter(word))
if any(n < 0 for n in rest.values()): continue # word isn't a subset of letters
anagrams.append((word, rest))
return anagrams
def main():
with open('/usr/share/dict/words') as f:
words = {line.strip().upper() for line in f}
anagrams = subanagrams(collections.Counter('VIDEOCLUB'), words)
for word, rest in sorted(anagrams):
print(word, ''.join(sorted(rest.elements())))
if __name__ == '__main__':
main()
@hrldcpr
Copy link
Author

hrldcpr commented Mar 26, 2020

output looks like:

BECLOUD IV
BIDE CLOUV
BILE CDOUV
BLOC DEIUV
BLUE CDIOV
BODE CILUV
BODICE LUV
…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment