Convert solver functions into a solver class.
This is prep for having additional solvers that can be trivially swapped in and out.
This commit is contained in:
1 parent
0639113205
commit
414380740f
2 files changed
+23
-16
No files matched your search
@@ -3,10 +3,14 @@
|
||||
import string
|
||||
from collections import defaultdict
|
||||
|
||||
import constraints
|
||||
import dictionary
|
||||
import game
|
||||
|
||||
|
||||
def analyze_dictionary(wordle_dictionary):
|
||||
class SimilaritySolver(object):
|
||||
@staticmethod
|
||||
def _analyze_dictionary(words):
|
||||
# Word Position, Letter: Count
|
||||
position_matches = [
|
||||
defaultdict(int),
|
||||
@@ -19,7 +23,7 @@ def analyze_dictionary(wordle_dictionary):
|
||||
# Letter: Count
|
||||
word_matches = defaultdict(int)
|
||||
|
||||
for word in wordle_dictionary:
|
||||
for word in words:
|
||||
unique_letters = set(word)
|
||||
for letter in unique_letters:
|
||||
word_matches[letter] += 1
|
||||
@@ -31,8 +35,8 @@ def analyze_dictionary(wordle_dictionary):
|
||||
|
||||
return position_matches, word_matches
|
||||
|
||||
|
||||
def score_word(position_matches, word_matches, word):
|
||||
@staticmethod
|
||||
def _score_word(position_matches, word_matches, word):
|
||||
result = 0
|
||||
|
||||
# Green Score
|
||||
@@ -48,15 +52,14 @@ def score_word(position_matches, word_matches, word):
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def scored_words(wordle_dictionary=dictionary.words()):
|
||||
def scored_words(self, wordle_dictionary=dictionary.words()):
|
||||
# word: score
|
||||
scored_words = dict()
|
||||
|
||||
position_matches, word_matches = analyze_dictionary(wordle_dictionary)
|
||||
position_matches, word_matches = self._analyze_dictionary(wordle_dictionary)
|
||||
|
||||
for word in wordle_dictionary:
|
||||
scored_words[word] = score_word(position_matches, word_matches, word)
|
||||
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)
|
||||
@@ -65,18 +68,20 @@ def scored_words(wordle_dictionary=dictionary.words()):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
solve = SimilaritySolver()
|
||||
|
||||
consts = constraints.Constraints()
|
||||
consts.update_graded(
|
||||
"cares",
|
||||
[
|
||||
solver.LetterResult.INCORRECT,
|
||||
solver.LetterResult.INCORRECT,
|
||||
solver.LetterResult.INCORRECT,
|
||||
solver.LetterResult.INCORRECT,
|
||||
solver.LetterResult.INCORRECT,
|
||||
game.LetterResult.INCORRECT,
|
||||
game.LetterResult.INCORRECT,
|
||||
game.LetterResult.INCORRECT,
|
||||
game.LetterResult.INCORRECT,
|
||||
game.LetterResult.INCORRECT,
|
||||
],
|
||||
)
|
||||
|
||||
valid_words = consts.filter_words(dictionary.words())
|
||||
|
||||
print("Top: ", solver.scored_words(valid_words)[:5])
|
||||
print("Top: ", solve.scored_words(valid_words)[:5])
|
||||
+4
-2
@@ -6,9 +6,11 @@ import solver
|
||||
|
||||
|
||||
class TestSolver(unittest.TestCase):
|
||||
def test_scored_words(self):
|
||||
def test_SimilaritySolver(self):
|
||||
"""Minimal end to end test."""
|
||||
result = solver.scored_words()
|
||||
|
||||
solve = solver.SimilaritySolver()
|
||||
result = solve.scored_words()
|
||||
|
||||
expected = [
|
||||
("cares", 139631),
|
||||
|
||||
Reference in new issue
Block a user