tests for solve

This commit is contained in:
Daniel Tsvetkov 2022-01-17 11:38:03 +01:00
parent ff737bdb67
commit a79a0e5bae
3 changed files with 30 additions and 17 deletions

10
lib.py
View File

@ -3,14 +3,18 @@ WORD_LENGTH = 5
ROUNDS = 6 ROUNDS = 6
INSTRUCTIONS = "Positional letters (green) are CAPITAL, non-positional (yellow) are small, others are blanks e.g. SHe-r" INSTRUCTIONS = "Positional letters (green) are CAPITAL, non-positional (yellow) are small, others are blanks e.g. SHe-r"
BLANK = "-" BLANK = "-"
DICTIONARY = '/usr/share/dict/words'
def cat_words(): def cat_words(dictionary=DICTIONARY):
with open('/usr/share/dict/words') as f: if type(dictionary) is list:
for word in dictionary:
yield word
else:
with open(dictionary) as f:
for word in f.readlines(): for word in f.readlines():
# remove end of line # remove end of line
word = word[:-1] word = word[:-1]
# filter out five letter words # filter out five letter words
if len(word) == WORD_LENGTH and "'" not in word and not word[0].isupper(): if len(word) == WORD_LENGTH and "'" not in word and not word[0].isupper():
yield word yield word

10
play.py
View File

@ -47,10 +47,6 @@ def main_loop():
print("Word was: {}".format(word)) print("Word was: {}".format(word))
def main():
main_loop()
def test(): def test():
assert build_letter_hint('yummy', 'slily') == '----Y' assert build_letter_hint('yummy', 'slily') == '----Y'
assert build_letter_hint('yummy', 'slyly') == 'y---Y' assert build_letter_hint('yummy', 'slyly') == 'y---Y'
@ -59,6 +55,10 @@ def test():
assert build_letter_hint('mmmyy', 'slyyy') == '---YY' assert build_letter_hint('mmmyy', 'slyyy') == '---YY'
if __name__ == "__main__": def main():
test() test()
main_loop()
if __name__ == "__main__":
main() main()

View File

@ -1,6 +1,6 @@
import random import random
from lib import cat_words, INSTRUCTIONS, BLANK, ROUNDS, DEBUG from lib import cat_words, INSTRUCTIONS, BLANK, ROUNDS, DEBUG, DICTIONARY
def remove_non_letters(word, non_letters): def remove_non_letters(word, non_letters):
@ -32,8 +32,8 @@ def remove_non_positional_letters(word, non_positional_letters):
return False return False
def round_words(non_letters, all_positional_letters, all_non_positional_letters): def round_words(non_letters, all_positional_letters, all_non_positional_letters, dictionary=DICTIONARY):
for word in cat_words(): for word in cat_words(dictionary):
skip = remove_non_letters(word, non_letters) skip = remove_non_letters(word, non_letters)
if skip: if skip:
continue continue
@ -135,13 +135,22 @@ def main_loop():
break break
if all([letter.isupper for letter in letters]): if all([letter.isupper for letter in letters]):
print("WIN") print("WIN")
break
else: else:
print("Sorry. Remaining words: {}".format(len(this_round_words))) print("Sorry. Remaining words: {}".format(len(this_round_words)))
for word in this_round_words: for word in this_round_words:
print(word) print(word)
def test():
assert [w for w in round_words('', ['----y'], ['-----'], dictionary=['slily'])] == ['slily']
assert [w for w in round_words('', ['----y'], ['y----'], dictionary=['slyly'])] == ['slyly']
assert [w for w in round_words('', ['---yy'], ['y----'], dictionary=['slyyy'])] == ['slyyy']
assert [w for w in round_words('', ['---yy'], ['-----'], dictionary=['slyyy'])] == ['slyyy']
def main(): def main():
test()
main_loop() main_loop()