Last active
March 27, 2020 00:30
-
-
Save hrldcpr/95b3a323bd7f1e3a73e856b3d6c31e5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output looks like: