Last active
January 8, 2022 11:34
-
-
Save teddygroves/e2211cb3b75814a3033ed76c0cf9e8ec to your computer and use it in GitHub Desktop.
A script I wrote to try and find the best starting word in wordle
This file contains 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
"""Find the most common letters in five letter English words at each position. | |
This is to help you play wordle (https://www.powerlanguage.co.uk/wordle/) | |
""" | |
import requests | |
URL = "https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt" | |
ALPHABET = "abcdefghijklmnopqrstuvwxyz" | |
N_SLOTS = 5 | |
def order_letters_and_get_probs(nword_by_letter, number_of_words): | |
letters = sorted(nword_by_letter, key=nword_by_letter.get)[::-1] | |
probs = [nword_by_letter[letter] / number_of_words for letter in letters] | |
return letters, probs | |
def format_probs(letters_and_probs): | |
letters, probs = letters_and_probs | |
return " ".join( | |
[f"{letter}: {round(prob, 2)}" for letter, prob in zip(letters, probs)] | |
) | |
def main(): | |
print(f"Fetching a list of words from {URL}...") | |
words = [ | |
word | |
for word in requests.get(URL).text.split("\r\n") | |
if len(word) == N_SLOTS | |
] | |
slots = list(range(N_SLOTS)) | |
words_by_slot = { | |
slot: { | |
letter: [word for word in words if word[slot] == letter] | |
for letter in ALPHABET | |
} | |
for slot in slots | |
} | |
nword_per_letter_by_slot = { | |
k: {kk: len(vv) for kk, vv in v.items()} | |
for k, v in words_by_slot.items() | |
} | |
letters_and_probs_by_slot = { | |
slot: order_letters_and_get_probs( | |
nword_per_letter_by_slot[slot], len(words) | |
) | |
for slot in slots | |
} | |
print("\n*******Most common letters at each position**********") | |
for slot in slots: | |
print(f"{slot + 1}: {''.join(letters_and_probs_by_slot[slot][0])}") | |
print("\n*******Probability of each letter at each slot**********") | |
for slot in slots: | |
print(f"{slot + 1}: {format_probs(letters_and_probs_by_slot[slot])}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output when I ran this script just now: