Skip to content

Instantly share code, notes, and snippets.

@csjh
Created October 17, 2022 06:11
Show Gist options
  • Save csjh/dd3315c3c7a91b4d66b93f5198990ed6 to your computer and use it in GitHub Desktop.
Save csjh/dd3315c3c7a91b4d66b93f5198990ed6 to your computer and use it in GitHub Desktop.
finds 5 five letter words with 25 unique letters
from string import ascii_lowercase
from typing import Dict, Set
import requests
ascii_set = set(ascii_lowercase)
words = list(filter(lambda x: len(x) == 5 and len(set(x)) == 5 and len(set(x).intersection(set('aeiou'))) < 2, requests.get('https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt').text.split()))
adj_list: Dict[str, Set[str]] = dict()
letters_dict: Dict[str, Set[str]] = dict()
for letter in ascii_lowercase:
letters_dict[letter] = set()
for word in words:
for letter in word:
letters_dict[letter].add(word)
for word in words:
adj_list[word] = set()
wrset = set(word)
for letter in ascii_set - wrset:
for associated_word in letters_dict[letter]:
if wrset.isdisjoint(set(associated_word)):
adj_list[word].add(associated_word)
def recursive_thing(word: str, used_letters: Set[str], path: Set[str]) -> Set[str]:
for associated_word in adj_list[word]:
letters_of_associated_word = set(associated_word)
if letters_of_associated_word.isdisjoint(used_letters):
if len(used_letters) == 20:
return path.union({associated_word})
final_path = recursive_thing(associated_word, letters_of_associated_word.union(used_letters), path.union({associated_word}))
if final_path:
return final_path
return None
for word in words:
final_path = recursive_thing(word, set(word), {word})
if final_path:
print(final_path)
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment