Last active
March 29, 2021 20:34
-
-
Save Stovoy/f15498651c0f3a906ee46f52b76a91db to your computer and use it in GitHub Desktop.
This file contains 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 string | |
import requests | |
letters = 'lairodm' | |
must_have = 'm' | |
length = 4 | |
raw = requests.get( | |
'https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt').text.split('\n') | |
dictionary = set() | |
for line in raw: | |
dictionary.add(line.strip().lower()) | |
min_length = 4 | |
max_length = 20 | |
prefixes = [] | |
for l in range(0, max_length + 1): | |
prefixes_for_length = set() | |
for word in dictionary: | |
prefix = word[0:l] | |
if len(prefix) == l: | |
prefixes_for_length.add(prefix) | |
prefixes.append(prefixes_for_length) | |
def int2base(x, base): | |
if x == 0: | |
return [0] | |
digits = [] | |
while x: | |
digits.append(int(x % base)) | |
x = int(x / base) | |
digits.reverse() | |
return digits | |
def base2int(digits, base): | |
x = 0 | |
value = 1 | |
for i in reversed(range(0, len(digits))): | |
x += digits[i] * value | |
value *= base | |
return x | |
words = [] | |
for length in range(min_length, max_length + 1): | |
permutation = 0 | |
print(length, flush=True) | |
while True: | |
indicies = int2base(permutation, len(letters)) | |
while len(indicies) < length: | |
indicies = [0] + indicies | |
if len(indicies) > length: | |
break | |
s = '' | |
for i in indicies: | |
s += letters[i] | |
for i in range(1, len(indicies)): | |
if s[0:i] not in prefixes[i]: | |
indicies[i-1] += 1 | |
while indicies[i-1] > len(letters): | |
indicies[i-1] = 0 | |
if i-2 == -1: | |
indicies = [0] + indicies | |
permutation = base2int(indicies, len(letters)) | |
break | |
else: | |
i -= 1 | |
permutation = base2int(indicies, len(letters)) | |
continue | |
permutation += 1 | |
if must_have not in s: | |
continue | |
if s in dictionary: | |
words.append(s) | |
print(s, flush=True) | |
with open('words.txt', 'w+') as f: | |
for word in words: | |
f.write(word) | |
f.write('\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment