Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #7

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
190 changes: 98 additions & 92 deletions fiveletterwords.py
Original file line number Diff line number Diff line change
@@ -1,110 +1,116 @@
import time
start_time = time.time()
#!/usr/bin/env python3
from typing import Final
from time import time

filestub = '/Users/mattparker/Dropbox/python/five_letter_words/'

def load_words():
words_txt = '/Users/mattparker/Dropbox/python/five_letter_words/words_alpha.txt'
with open(words_txt) as word_file:
valid_words = list(word_file.read().split())
'''
reads hardcoded word-file from storage into memory,
parsing it as a `list` of words.
'''
filestub = '/Users/mattparker/Dropbox/python/five_letter_words/'
words_txt = f'{filestub}words_alpha.txt'
with open(words_txt, encoding='UTF-8') as word_file:
valid_words = word_file.read().split()
return valid_words

word_length = 5
# global mutables are a nightmare!
WORD_LEN: Final = 5

word_length2 = word_length*2
word_length4 = word_length2*2
word_length5 = word_length4 + word_length
WORD_LEN2: Final = WORD_LEN * 2
WORD_LEN4: Final = WORD_LEN2 * 2
WORD_LEN5: Final = WORD_LEN4 + WORD_LEN

# number of scanA increases per progress report
stepgap = 1000
STEP_GAP: Final = 1000
'''number of `scanA` increases per progress report'''
# for some reason, that doc-comment only works if the var is global

# Yes, that is the alphabet. In the default order python makes a list in. Weird.
alphabet = ['f', 'g', 'o', 'q', 't', 'b', 'y', 'h', 'r', 'u', 'j', 'w', 'i', 'p', 's', 'd', 'l', 'e', 'k', 'm', 'n', 'v', 'z', 'c', 'a', 'x']
def main():
'''this is where the program actually begins (after the header/definitions)'''
# so it's fair to measure time from here
# "see what I did there? a doc-comment that continues as a regular comment!" @Rudxain
start_time: Final = time()

# I could be clever and write this to be dynamic
# but for now I'll hard code everything assuming five words
number_of_sets = 5
english_words: Final = load_words()

english_words = load_words()
print(f"{len(english_words)} words in total")

print(f"{len(english_words)} words in total")
fl_words: Final = [w for w in english_words if len(w) == WORD_LEN]

fl_words = []
print(f"{len(fl_words)} words have {WORD_LEN} letters")

for w in english_words:
if len(w) == word_length:
fl_words.append(w)

print(f"{len(fl_words)} words have {word_length} letters")
word_sets: Final[list[set[str]]] = []
unique_fl_words: Final[list[str]] = []


word_sets = []

unique_fl_words = []
for w in fl_words:
unique_letters = set(w)
if len(unique_letters) == word_length:
if unique_letters not in word_sets:
for w in fl_words:
unique_letters = set(w)
if len(unique_letters) == WORD_LEN and unique_letters not in word_sets:
word_sets.append(unique_letters)
unique_fl_words.append(w)

number_of_words = len(unique_fl_words)

print(f"{number_of_words} words have a unique set of {word_length} letters")

doubleword_sets = []
doubleword_words = []

scanA = 0
while scanA < number_of_words-1:
scanB = scanA + 1
while scanB < number_of_words:
give_it_a_try = word_sets[scanA] | word_sets[scanB]
if len(give_it_a_try) == word_length2:
doubleword_sets.append(give_it_a_try)
doubleword_words.append([unique_fl_words[scanA], unique_fl_words[scanB]])
scanB += 1
scanA += 1

number_of_doublewords = len(doubleword_sets)

print(f"we found {number_of_doublewords} combos")

counter = 0

success_found = []

scanA = 0
print(f"starting at position {scanA}")

while scanA < number_of_doublewords-1:
if scanA % stepgap == 0:
print(f"Up to {scanA} after {time.time() - start_time} seconds.")

scanB = scanA + 1
while scanB < number_of_doublewords:
give_it_a_try = doubleword_sets[scanA] | doubleword_sets[scanB]
if len(give_it_a_try) == word_length4:
scanC = 0
while scanC < number_of_words:
final_go = give_it_a_try | word_sets[scanC]
if len(final_go) == word_length5:
success = doubleword_words[scanA] + doubleword_words[scanB]
success.append(unique_fl_words[scanC])
success.sort()
if success not in success_found:
success_found.append(success)
print(success)
scanC += 1
counter += 1
scanB += 1
scanA += 1

print(f"Damn, we had {len(success_found)} successful finds!")
print(f"That took {time.time() - start_time} seconds")

print("Here they all are:")
for i in success_found:
print(i)

print("DONE")
number_of_words: Final = len(unique_fl_words)

print(f"{number_of_words} words have a unique set of {WORD_LEN} letters")

doubleword_sets: Final[list[set[str]]] = []
doubleword_words: Final[list[list[str]]] = []

scanA = 0
while scanA + 1 < number_of_words:
scanB = scanA + 1
while scanB < number_of_words:
give_it_a_try = word_sets[scanA] | word_sets[scanB]
if len(give_it_a_try) == WORD_LEN2:
doubleword_sets.append(give_it_a_try)
doubleword_words.append([unique_fl_words[scanA], unique_fl_words[scanB]])
scanB += 1
scanA += 1

number_of_doublewords: Final = len(doubleword_sets)

print(f"we found {number_of_doublewords} combos")

success_found: Final[list[list[str]]] = []

scanA = 0
print(f"starting at position {scanA}")

counter = 0
while scanA < number_of_doublewords - 1:
if scanA % STEP_GAP == 0:
print(f"Up to {scanA} after {time() - start_time} seconds.")

scanB = scanA + 1
while scanB < number_of_doublewords:
give_it_a_try = doubleword_sets[scanA] | doubleword_sets[scanB]
if len(give_it_a_try) == WORD_LEN4:
scanC = 0
while scanC < number_of_words:
final_go = give_it_a_try | word_sets[scanC]
if len(final_go) == WORD_LEN5:
success = doubleword_words[scanA] + doubleword_words[scanB]
success.append(unique_fl_words[scanC])
success.sort()
if success not in success_found:
success_found.append(success)
print(success)
scanC += 1
counter += 1
scanB += 1
scanA += 1

# avoid measuring `print` latency
end_time: Final = time() - start_time

print(f"Damn, we had {len(success_found)} successful finds!")
print(f"That took {end_time} seconds")

print("Here they all are:")
for i in success_found:
print(i)

print("-- DONE! --")

if __name__ == '__main__':
main()