From d570b16b60f909e464a46136fbd3ac3a6e672824 Mon Sep 17 00:00:00 2001 From: Don Garrett Date: Thu, 7 Mar 2024 09:46:05 -0600 Subject: [PATCH] Snapshot permission updates. --- .gitignore | 6 +- LICENSE.md | 48 +++++------ constraints.py | 136 ++++++++++++++++---------------- play.py | 128 +++++++++++++++--------------- solver.py | 25 +++++- static/site.css | 52 ++++++------ templates/dictionary.html | 46 +++++------ templates/layout.html | 52 ++++++------ templates/solver.html | 58 +++++++------- test_constraints.py | 162 +++++++++++++++++++------------------- test_dictionary.py | 68 ++++++++-------- test_game.py | 158 ++++++++++++++++++------------------- test_play.py | 28 +++---- test_solver.py | 73 ++++++++++------- wordle.code-workspace | 2 +- 15 files changed, 540 insertions(+), 502 deletions(-) diff --git a/.gitignore b/.gitignore index 72f6cdc..1061dae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -*.pyc -.venv -__pycache__ +*.pyc +.venv +__pycache__ diff --git a/LICENSE.md b/LICENSE.md index cf1ab25..3ce22da 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,24 +1,24 @@ -This is free and unencumbered software released into the public domain. - -Anyone is free to copy, modify, publish, use, compile, sell, or -distribute this software, either in source code form or as a compiled -binary, for any purpose, commercial or non-commercial, and by any -means. - -In jurisdictions that recognize copyright laws, the author or authors -of this software dedicate any and all copyright interest in the -software to the public domain. We make this dedication for the benefit -of the public at large and to the detriment of our heirs and -successors. We intend this dedication to be an overt act of -relinquishment in perpetuity of all present and future rights to this -software under copyright law. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR -OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -OTHER DEALINGS IN THE SOFTWARE. - -For more information, please refer to +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/constraints.py b/constraints.py index a671308..682f78a 100644 --- a/constraints.py +++ b/constraints.py @@ -1,68 +1,68 @@ -#!/usr/bin/python - -from collections import defaultdict - -import dictionary -from game import LetterResult - - -class Constraints: - def __init__(self): - # Array of known letters in result with None for unknown. - self.correct = [None] * dictionary.WORDLE_LENGTH - # Array with per-letter sets of known bad letters. - self.incorrect = [set() for _ in range(dictionary.WORDLE_LENGTH)] - # Dict of letter -> int with minimum required count of letter in word. - self.count = defaultdict(int) - # If letter is present in set, value in min is both min and max. - self.exact = set() - - # List of LetterResult values. - def update_graded(self, guess, grade): - guess = guess.lower() - - new_count = defaultdict(int) - - for i, letter in enumerate(guess): - match grade[i]: - case LetterResult.CORRECT: - self.correct[i] = letter - new_count[letter] += 1 - case LetterResult.MOVE: - self.incorrect[i].add(letter) - new_count[letter] += 1 - case LetterResult.INCORRECT: - self.incorrect[i].add(letter) - self.exact.add(letter) - new_count[letter] += 0 # Ensure letter is in dict - - for letter, c in new_count.items(): - self.count[letter] = max(c, self.count[letter]) - - def matches(self, guess): - counts = defaultdict(int) - - # All known correct letters in place. - for i, letter in enumerate(guess): - # If we have a known letter, and don't match... - if self.correct[i] and self.correct[i] != letter: - return False - - # If we have a letter that's known to be incorrect in the current location... - if letter in self.incorrect[i]: - return False - - counts[letter] += 1 - - for letter, c in self.count.items(): - if letter in self.exact: - if counts[letter] != c: - return False - else: - if counts[letter] < c: - return False - - return True - - def filter_words(self, words=dictionary.words()): - return set(filter(lambda w: self.matches(w), words)) +#!/usr/bin/python + +from collections import defaultdict + +import dictionary +from game import LetterResult + + +class Constraints: + def __init__(self): + # Array of known letters in result with None for unknown. + self.correct = [None] * dictionary.WORDLE_LENGTH + # Array with per-letter sets of known bad letters. + self.incorrect = [set() for _ in range(dictionary.WORDLE_LENGTH)] + # Dict of letter -> int with minimum required count of letter in word. + self.count = defaultdict(int) + # If letter is present in set, value in min is both min and max. + self.exact = set() + + # List of LetterResult values. + def update_graded(self, guess, grade): + guess = guess.lower() + + new_count = defaultdict(int) + + for i, letter in enumerate(guess): + match grade[i]: + case LetterResult.CORRECT: + self.correct[i] = letter + new_count[letter] += 1 + case LetterResult.MOVE: + self.incorrect[i].add(letter) + new_count[letter] += 1 + case LetterResult.INCORRECT: + self.incorrect[i].add(letter) + self.exact.add(letter) + new_count[letter] += 0 # Ensure letter is in dict + + for letter, c in new_count.items(): + self.count[letter] = max(c, self.count[letter]) + + def matches(self, guess): + counts = defaultdict(int) + + # All known correct letters in place. + for i, letter in enumerate(guess): + # If we have a known letter, and don't match... + if self.correct[i] and self.correct[i] != letter: + return False + + # If we have a letter that's known to be incorrect in the current location... + if letter in self.incorrect[i]: + return False + + counts[letter] += 1 + + for letter, c in self.count.items(): + if letter in self.exact: + if counts[letter] != c: + return False + else: + if counts[letter] < c: + return False + + return True + + def filter_words(self, words=dictionary.words()): + return set(filter(lambda w: self.matches(w), words)) diff --git a/play.py b/play.py index 50182d3..d82e629 100644 --- a/play.py +++ b/play.py @@ -1,64 +1,64 @@ -#!/usr/bin/python - - -import dictionary -import game - - -def display_instructions(): - """Print out instuctions before starting play.""" - - print( - f"""Welcome to Wordle! - - Wordle has a hidden word of five letters, and you get to - make up to six guesses to find out what it is. - - After each guess, you get feedback on each letter of your guess. - * {game.display_result(game.LetterResult.CORRECT, "Correct!")} - * {game.display_result(game.LetterResult.MOVE, "Correct, in a different location.")} - * {game.display_result(game.LetterResult.INCORRECT, "Not in the word.")} - """ - ) - - -def read_valid_guess(guess_number): - while True: - print(f"Guess {guess_number}: ", end="") - guess = input() - if guess in dictionary.words(): - return guess - - print(f"{guess} is not a valid Wordle word. Please try again.") - - -def display_results(guess, results): - print(" ", end="") - for i in range(dictionary.WORDLE_LENGTH): - print(game.display_result(results[i], guess[i]), end="") - print("") - - -def play_game(): - display_instructions() - - target = game.select_word() - - # To help with debugging - # print(f"The target is: {target}") - - for guess_number in range(1, 6): - guess = read_valid_guess(guess_number) - results = game.grade_word(target, guess) - - if game.success_result(results): - print(f"You won! It took {guess_number} guesses.") - return - - display_results(guess, results) - - print(f"Sorry, no guesses remaining. The word was: {target}") - - -if __name__ == "__main__": - play_game() +#!/usr/bin/python + + +import dictionary +import game + + +def display_instructions(): + """Print out instuctions before starting play.""" + + print( + f"""Welcome to Wordle! + + Wordle has a hidden word of five letters, and you get to + make up to six guesses to find out what it is. + + After each guess, you get feedback on each letter of your guess. + * {game.display_result(game.LetterResult.CORRECT, "Correct!")} + * {game.display_result(game.LetterResult.MOVE, "Correct, in a different location.")} + * {game.display_result(game.LetterResult.INCORRECT, "Not in the word.")} + """ + ) + + +def read_valid_guess(guess_number): + while True: + print(f"Guess {guess_number}: ", end="") + guess = input() + if guess in dictionary.words(): + return guess + + print(f"{guess} is not a valid Wordle word. Please try again.") + + +def display_results(guess, results): + print(" ", end="") + for i in range(dictionary.WORDLE_LENGTH): + print(game.display_result(results[i], guess[i]), end="") + print("") + + +def play_game(): + display_instructions() + + target = game.select_word() + + # To help with debugging + # print(f"The target is: {target}") + + for guess_number in range(1, 6): + guess = read_valid_guess(guess_number) + results = game.grade_word(target, guess) + + if game.success_result(results): + print(f"You won! It took {guess_number} guesses.") + return + + display_results(guess, results) + + print(f"Sorry, no guesses remaining. The word was: {target}") + + +if __name__ == "__main__": + play_game() diff --git a/solver.py b/solver.py index eaf3d00..4e107c5 100644 --- a/solver.py +++ b/solver.py @@ -61,10 +61,29 @@ class SimilaritySolver(object): for word in wordle_dictionary: scored_words[word] = self._score_word(position_matches, word_matches, word) - result = list(wordle_dictionary) - result.sort(key=lambda word: scored_words[word], reverse=True) + # Return as list of (word, score) sorted by high-score, word. + result = sorted(scored_words.items(), key=lambda kv: kv[0]) + result = sorted(result, key=lambda kv: kv[1], reverse=True) + return result - return [(word, scored_words[word]) for word in result] + +class LimitsSolver(object): + def scored_words(self, words=dictionary.words()): + # word: score + scored_words = dict() + + for word in words: + reduced = const.filter_words(words) + + # Our score is how many words did we remove from consideration. + score = len(words) - len(reduced) + + scored_words[word] = score + + # Return as list of (word, score) sorted by high-score, word. + result = sorted(scored_words.items(), key=lambda kv: kv[0]) + result = sorted(result, key=lambda kv: kv[1], reverse=True) + return result if __name__ == "__main__": diff --git a/static/site.css b/static/site.css index 872fa72..d603307 100644 --- a/static/site.css +++ b/static/site.css @@ -1,27 +1,27 @@ -.navbar { - background-color: lightslategray; - font-size: 1em; - font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif; - color: white; - padding: 8px 5px 8px 5px; -} - -.navbar a { - text-decoration: none; - color: inherit; -} - -.navbar-brand { - font-size: 1.2em; - font-weight: 600; -} - -.navbar-item { - font-variant: small-caps; - margin-left: 30px; -} - -.body-content { - padding: 5px; - font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; +.navbar { + background-color: lightslategray; + font-size: 1em; + font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif; + color: white; + padding: 8px 5px 8px 5px; +} + +.navbar a { + text-decoration: none; + color: inherit; +} + +.navbar-brand { + font-size: 1.2em; + font-weight: 600; +} + +.navbar-item { + font-variant: small-caps; + margin-left: 30px; +} + +.body-content { + padding: 5px; + font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } \ No newline at end of file diff --git a/templates/dictionary.html b/templates/dictionary.html index 404f2de..05f8335 100644 --- a/templates/dictionary.html +++ b/templates/dictionary.html @@ -1,23 +1,23 @@ - {% extends "layout.html" %} - - {% block title %} - Dictionary - {% endblock %} - - {% block content %} - - {% if page > 0 %}Prev{% endif %} - - Page {{ page }} / {{ max_pages }} - - {% if page < max_pages %}Next{% endif %} - - -
    - {% for word in words %} -
  • {{ word }}
  • - {% endfor %} -
- - - {% endblock %} + {% extends "layout.html" %} + + {% block title %} + Dictionary + {% endblock %} + + {% block content %} + + {% if page > 0 %}Prev{% endif %} + + Page {{ page }} / {{ max_pages }} + + {% if page < max_pages %}Next{% endif %} + + +
    + {% for word in words %} +
  • {{ word }}
  • + {% endfor %} +
+ + + {% endblock %} diff --git a/templates/layout.html b/templates/layout.html index cd94449..3e4f5c9 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -1,27 +1,27 @@ - - - - - - {% block title %}{% endblock %} - - - - - - -
- {% block content %} - {% endblock %} -
-
- This is not associated with the New York Times or the offical version of - Wordle in any way. -
-
- - + + + + + + {% block title %}{% endblock %} + + + + + + +
+ {% block content %} + {% endblock %} +
+
+ This is not associated with the New York Times or the offical version of + Wordle in any way. +
+
+ + \ No newline at end of file diff --git a/templates/solver.html b/templates/solver.html index 90f1eee..0772cda 100644 --- a/templates/solver.html +++ b/templates/solver.html @@ -1,30 +1,30 @@ -{% extends "layout.html" %} - -{% block title %} - Solver! -{% endblock %} - -{% block content %} -
    - {% for guess in guesses %} -
  • {{ guess }} - - {% for i in range(5) %} - - {% endfor %} -
  • - {% endfor %} -
- - - {%if solution %} - The proposed solution is {{ solution }}. - {% else %} - No solution found. - {% endif %} +{% extends "layout.html" %} + +{% block title %} + Solver! +{% endblock %} + +{% block content %} +
    + {% for guess in guesses %} +
  • {{ guess }} + + {% for i in range(5) %} + + {% endfor %} +
  • + {% endfor %} +
+ + + {%if solution %} + The proposed solution is {{ solution }}. + {% else %} + No solution found. + {% endif %} {% endblock %} \ No newline at end of file diff --git a/test_constraints.py b/test_constraints.py index cdba7c2..d463be4 100644 --- a/test_constraints.py +++ b/test_constraints.py @@ -1,81 +1,81 @@ -#!/usr/bin/python - -import unittest - -import constraints -from game import LetterResult - - -class TestConstraints(unittest.TestCase): - def test_empty(self): - consts = constraints.Constraints() - self.assertTrue(consts.matches("abcde")) - self.assertTrue(consts.matches("bcdef")) - - def test_correct(self): - consts = constraints.Constraints() - consts.update_graded( - "abcde", - [ - LetterResult.CORRECT, - LetterResult.INCORRECT, - LetterResult.INCORRECT, - LetterResult.INCORRECT, - LetterResult.INCORRECT, - ], - ) - - self.assertTrue(consts.matches("amnop")) - self.assertTrue(consts.matches("aaaaa")) - - self.assertFalse(consts.matches("abcde")) - self.assertFalse(consts.matches("lmnop")) - self.assertFalse(consts.matches("aedcb")) - self.assertFalse(consts.matches("amnob")) - - def test_correct_count_limit(self): - consts = constraints.Constraints() - consts.update_graded( - "aazzz", - [ - LetterResult.CORRECT, - LetterResult.INCORRECT, - LetterResult.INCORRECT, - LetterResult.INCORRECT, - LetterResult.INCORRECT, - ], - ) - - self.assertTrue(consts.matches("abcde")) - self.assertTrue(consts.matches("abbbb")) - - self.assertFalse(consts.matches("aabbb")) - self.assertFalse(consts.matches("abbba")) - self.assertFalse(consts.matches("bbbbb")) - self.assertFalse(consts.matches("bbbba")) - self.assertFalse(consts.matches("babbb")) - - def test_move(self): - consts = constraints.Constraints() - consts.update_graded( - "abcde", - [ - LetterResult.MOVE, - LetterResult.INCORRECT, - LetterResult.INCORRECT, - LetterResult.INCORRECT, - LetterResult.INCORRECT, - ], - ) - - self.assertTrue(consts.matches("zzzza")) - self.assertTrue(consts.matches("zzzaa")) - self.assertTrue(consts.matches("zaaaa")) - - self.assertFalse(consts.matches("azzzz")) - self.assertFalse(consts.matches("zzzzz")) - self.assertFalse(consts.matches("zzzba")) - - -if __name__ == "__main__": - unittest.main() +#!/usr/bin/python + +import unittest + +import constraints +from game import LetterResult + + +class TestConstraints(unittest.TestCase): + def test_empty(self): + consts = constraints.Constraints() + self.assertTrue(consts.matches("abcde")) + self.assertTrue(consts.matches("bcdef")) + + def test_correct(self): + consts = constraints.Constraints() + consts.update_graded( + "abcde", + [ + LetterResult.CORRECT, + LetterResult.INCORRECT, + LetterResult.INCORRECT, + LetterResult.INCORRECT, + LetterResult.INCORRECT, + ], + ) + + self.assertTrue(consts.matches("amnop")) + self.assertTrue(consts.matches("aaaaa")) + + self.assertFalse(consts.matches("abcde")) + self.assertFalse(consts.matches("lmnop")) + self.assertFalse(consts.matches("aedcb")) + self.assertFalse(consts.matches("amnob")) + + def test_correct_count_limit(self): + consts = constraints.Constraints() + consts.update_graded( + "aazzz", + [ + LetterResult.CORRECT, + LetterResult.INCORRECT, + LetterResult.INCORRECT, + LetterResult.INCORRECT, + LetterResult.INCORRECT, + ], + ) + + self.assertTrue(consts.matches("abcde")) + self.assertTrue(consts.matches("abbbb")) + + self.assertFalse(consts.matches("aabbb")) + self.assertFalse(consts.matches("abbba")) + self.assertFalse(consts.matches("bbbbb")) + self.assertFalse(consts.matches("bbbba")) + self.assertFalse(consts.matches("babbb")) + + def test_move(self): + consts = constraints.Constraints() + consts.update_graded( + "abcde", + [ + LetterResult.MOVE, + LetterResult.INCORRECT, + LetterResult.INCORRECT, + LetterResult.INCORRECT, + LetterResult.INCORRECT, + ], + ) + + self.assertTrue(consts.matches("zzzza")) + self.assertTrue(consts.matches("zzzaa")) + self.assertTrue(consts.matches("zaaaa")) + + self.assertFalse(consts.matches("azzzz")) + self.assertFalse(consts.matches("zzzzz")) + self.assertFalse(consts.matches("zzzba")) + + +if __name__ == "__main__": + unittest.main() diff --git a/test_dictionary.py b/test_dictionary.py index 8a3720d..9beea26 100644 --- a/test_dictionary.py +++ b/test_dictionary.py @@ -1,34 +1,34 @@ -#!/usr/bin/python - -import unittest - -import dictionary - - -class TestDictionary(unittest.TestCase): - def test_empty(self): - """Verify we can get the default words.""" - words = dictionary.words() - - def test_expected(self): - """Verify we have a few expected words.""" - words = dictionary.words() - self.assertIn("cares", words) - self.assertIn("fudge", words) - - def test_unexpected(self): - """Verify we have a few expected words.""" - words = dictionary.words() - self.assertNotIn("doray", words) - self.assertNotIn("xxxxx", words) - - def test_size(self): - """Verify all words are the expected length.""" - words = dictionary.words() - - for word in words: - self.assertEqual(len(word), dictionary.WORDLE_LENGTH) - - -if __name__ == "__main__": - unittest.main() +#!/usr/bin/python + +import unittest + +import dictionary + + +class TestDictionary(unittest.TestCase): + def test_empty(self): + """Verify we can get the default words.""" + words = dictionary.words() + + def test_expected(self): + """Verify we have a few expected words.""" + words = dictionary.words() + self.assertIn("cares", words) + self.assertIn("fudge", words) + + def test_unexpected(self): + """Verify we have a few expected words.""" + words = dictionary.words() + self.assertNotIn("doray", words) + self.assertNotIn("xxxxx", words) + + def test_size(self): + """Verify all words are the expected length.""" + words = dictionary.words() + + for word in words: + self.assertEqual(len(word), dictionary.WORDLE_LENGTH) + + +if __name__ == "__main__": + unittest.main() diff --git a/test_game.py b/test_game.py index 08712ad..5334754 100644 --- a/test_game.py +++ b/test_game.py @@ -1,79 +1,79 @@ -#!/usr/bin/python - -import unittest - -import dictionary -import game - - -class TestGame(unittest.TestCase): - test_words = ( - "aaaaa", - "aabbb", - "aaccc", - "bbbaa", - "bbbbb", - "ccaaa", - "cccaa", - "ccccc", - "abcde", - ) - - def test_select_word(self): - word = game.select_word(self.test_words) - - self.assertIn(word, self.test_words) - - word = game.select_word() - - self.assertIn(word, dictionary.words()) - - def test_grade_word_correct(self): - target = "aaaaa" - guess = "aaaaa" - - result = game.grade_word(target, guess, self.test_words) - - expected = [ - game.LetterResult.CORRECT, - game.LetterResult.CORRECT, - game.LetterResult.CORRECT, - game.LetterResult.CORRECT, - game.LetterResult.CORRECT, - ] - - self.assertEqual(expected, result) - - self.assertTrue(game.success_result(result)) - - def test_grade_word_mixed(self): - C = game.LetterResult.CORRECT - M = game.LetterResult.MOVE - I = game.LetterResult.INCORRECT - - test_cases = ( - ("aabbb", "aabbb", [C, C, C, C, C]), - ("aabbb", "aaccc", [C, C, I, I, I]), - ("bbbaa", "cccaa", [I, I, I, C, C]), - ("aabbb", "cccaa", [I, I, I, M, M]), - ("aabbb", "bbbaa", [M, M, C, M, M]), - ("aabbb", "ccaaa", [I, I, M, M, I]), - ("aabbb", "abcde", [C, M, I, I, I]), - ("aabbb", "ccccc", [I, I, I, I, I]), - ) - - for target, guess, expected in test_cases: - result = game.grade_word(target, guess, self.test_words) - - self.assertEqual(expected, result) - - def test_grade_word_invalid(self): - target = "aaaaa" - guess = "inval" - - with self.assertRaises(Exception): - game.grade_word(target, guess, self.test_words) - - -if __name__ == "__main__": - unittest.main() +#!/usr/bin/python + +import unittest + +import dictionary +import game + + +class TestGame(unittest.TestCase): + test_words = ( + "aaaaa", + "aabbb", + "aaccc", + "bbbaa", + "bbbbb", + "ccaaa", + "cccaa", + "ccccc", + "abcde", + ) + + def test_select_word(self): + word = game.select_word(self.test_words) + + self.assertIn(word, self.test_words) + + word = game.select_word() + + self.assertIn(word, dictionary.words()) + + def test_grade_word_correct(self): + target = "aaaaa" + guess = "aaaaa" + + result = game.grade_word(target, guess, self.test_words) + + expected = [ + game.LetterResult.CORRECT, + game.LetterResult.CORRECT, + game.LetterResult.CORRECT, + game.LetterResult.CORRECT, + game.LetterResult.CORRECT, + ] + + self.assertEqual(expected, result) + + self.assertTrue(game.success_result(result)) + + def test_grade_word_mixed(self): + C = game.LetterResult.CORRECT + M = game.LetterResult.MOVE + I = game.LetterResult.INCORRECT + + test_cases = ( + ("aabbb", "aabbb", [C, C, C, C, C]), + ("aabbb", "aaccc", [C, C, I, I, I]), + ("bbbaa", "cccaa", [I, I, I, C, C]), + ("aabbb", "cccaa", [I, I, I, M, M]), + ("aabbb", "bbbaa", [M, M, C, M, M]), + ("aabbb", "ccaaa", [I, I, M, M, I]), + ("aabbb", "abcde", [C, M, I, I, I]), + ("aabbb", "ccccc", [I, I, I, I, I]), + ) + + for target, guess, expected in test_cases: + result = game.grade_word(target, guess, self.test_words) + + self.assertEqual(expected, result) + + def test_grade_word_invalid(self): + target = "aaaaa" + guess = "inval" + + with self.assertRaises(Exception): + game.grade_word(target, guess, self.test_words) + + +if __name__ == "__main__": + unittest.main() diff --git a/test_play.py b/test_play.py index c68bade..7c05b29 100644 --- a/test_play.py +++ b/test_play.py @@ -1,14 +1,14 @@ -#!/usr/bin/python - -import unittest - -import play - - -class TestPlay(unittest.TestCase): - def test_noop(self): - """Currently no tests for this code.""" - - -if __name__ == "__main__": - unittest.main() +#!/usr/bin/python + +import unittest + +import play + + +class TestPlay(unittest.TestCase): + def test_noop(self): + """Currently no tests for this code.""" + + +if __name__ == "__main__": + unittest.main() diff --git a/test_solver.py b/test_solver.py index a362997..3312566 100644 --- a/test_solver.py +++ b/test_solver.py @@ -1,27 +1,46 @@ -#!/usr/bin/python - -import unittest - -import solver - - -class TestSolver(unittest.TestCase): - def test_SimilaritySolver(self): - """Minimal end to end test.""" - - solve = solver.SimilaritySolver() - result = solve.scored_words() - - expected = [ - ("cares", 139631), - ("sanes", 139129), - ("tares", 138755), - ("bares", 138375), - ("sales", 137491), - ] - - self.assertListEqual(result[:5], expected) - - -if __name__ == "__main__": - unittest.main() +#!/usr/bin/python + +import unittest + +import dictionary +import solver + +test_dictionary = dictionary.words() + + +class TestSolver(unittest.TestCase): + def test_SimilaritySolver(self): + """Minimal end to end test.""" + + solve = solver.SimilaritySolver() + result = solve.scored_words(test_dictionary) + + expected = [ + ("cares", 139631), + ("sanes", 139129), + ("tares", 138755), + ("bares", 138375), + ("sales", 137491), + ] + + self.assertListEqual(result[:5], expected) + + def test_LimitsSolver(self): + """Minimal end to end test.""" + + solve = solver.LimitsSolver() + result = solve.scored_words(test_dictionary) + + expected = [ + ("aahed", 5), + ("aalii", 5), + ("aargh", 5), + ("aaron", 5), + ("abaca", 5), + ] + + self.assertListEqual(result[:5], expected) + + +if __name__ == "__main__": + unittest.main() diff --git a/wordle.code-workspace b/wordle.code-workspace index a821158..f28d986 100644 --- a/wordle.code-workspace +++ b/wordle.code-workspace @@ -9,7 +9,7 @@ "editor.defaultFormatter": "ms-python.black-formatter", "editor.formatOnSave": true, "editor.codeActionsOnSave": { - "source.organizeImports": true + "source.organizeImports": "explicit" }, }, "python.formatting.provider": "none",