Created
June 24, 2026 22:16
-
-
Save mlippert/d99a8f2486ee89dbb734f8396bec94f5 to your computer and use it in GitHub Desktop.
python sample algorithm for wordle penalizing but not removing previous answers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # see the play function at the bottom | |
| # This is really just an example for reference of the words data structure | |
| words = [ | |
| {value: 'stuff', weight: 100, penalty: 0}, | |
| {value: 'flood', weight: 100, penalty: 0}, | |
| {value: 'royal', weight: 100, penalty: 0}, | |
| ] | |
| def get_total_weight(words): | |
| sum([w.weight - w.penalty for w in words]) | |
| def get_index_of_weighted_pos(words, weighted_pos): | |
| remaining_weight = weighted_pos | |
| i = 0 | |
| for word in words: | |
| cur_word_weight = word.weight - word.penalty | |
| # error if cur_word_weight < 0 as weight must be >=0 and penalty must be <= weight | |
| if cur_word_weight < remaining_weight: | |
| return i | |
| remaining_weight -= cur_word_weight | |
| i += 1 | |
| # error the weighted_pos was past the last word | |
| # return the index of the last word in this case | |
| return len(words) - 1 | |
| def update_words_for_next_play(words, answer_word_index): | |
| # decrement the penalty of all words that have a penalty | |
| for word in words: | |
| if word.penalty > 0: | |
| word.penalty -= 1 | |
| # set the penalty for the answer word to it's weight so that it will not | |
| # be the next play's answer, and will be less likely to be the following | |
| # play's answer until its penalty is reduced to 0. | |
| words[answer_word_index].penalty = words[answer_word_index].weight | |
| def play(words): | |
| weight_of_words = get_total_weight(words) | |
| random.seed() | |
| weighted_pos = random.randrange(weight_of_words) | |
| answer_word_index = get_index_of_weighted_pos(words, weighted_pos) | |
| update_words_for_next_play(words, answer_word_index) | |
| # play the game with the selected answer | |
| # since this is daily, I assume that means setting the answer in the database | |
| # for today's game, and updating the date of this last play so it won't kick off | |
| # the next play until tomorrow. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment