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

Tried to compile your code using cython, didn't really work #5

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.o
*.so
*.c
build/
types_dump
words_alpha.txt

171 changes: 91 additions & 80 deletions fiveletterwords.py
Original file line number Diff line number Diff line change
@@ -1,110 +1,121 @@
import time
start_time = time.time()
#from pyannotate_runtime import collect_types
try:
#collect_types.start()

filestub = '/Users/mattparker/Dropbox/python/five_letter_words/'
start_time = time.time()

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())
return valid_words
#filestub = '/Users/mattparker/Dropbox/python/five_letter_words/'

word_length = 5
def load_words():
# I used a ramdisk to speed up loading
words_txt = '/mnt/tmp/words_alpha.txt'
with open(words_txt) as word_file:
valid_words = list(word_file.read().split())
return valid_words

word_length2 = word_length*2
word_length4 = word_length2*2
word_length5 = word_length4 + word_length
word_length = 5

# number of scanA increases per progress report
stepgap = 1000
word_length2 = word_length*2
word_length4 = word_length2*2
word_length5 = word_length4 + word_length

# 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']
# number of scanA increases per progress report
stepgap = 10

# 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
# 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']

english_words = load_words()
# 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

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

fl_words = []
print(f"{len(english_words)} words in total")

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

print(f"{len(fl_words)} words have {word_length} 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 = []

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:
word_sets.append(unique_letters)
unique_fl_words.append(w)
word_sets = []

number_of_words = len(unique_fl_words)
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:
word_sets.append(unique_letters)
unique_fl_words.append(w)

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

doubleword_sets = []
doubleword_words = []
print(f"{number_of_words} words have a unique set of {word_length} letters")

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
doubleword_sets = []
doubleword_words = []

number_of_doublewords = len(doubleword_sets)
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

print(f"we found {number_of_doublewords} combos")
number_of_doublewords = len(doubleword_sets)

counter = 0
print(f"we found {number_of_doublewords} combos")

success_found = []
counter = 0

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

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

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

print(f"Damn, we had {len(success_found)} successful finds!")
print(f"That took {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
#collect_types.stop()
#collect_types.dump_stats("types_dump")
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("Here they all are:")
for i in success_found:
print(i)

print("DONE")

print("DONE")
except KeyboardInterrupt:
#collect_types.stop()
#collect_types.dump_stats("types_dump")
print("Keyboard interrupt caught. Tried to dump stats")
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from setuptools import setup
from Cython.Build import cythonize

setup(
ext_modules=cythonize('fiveletterwords.py')
)
Loading