Skip to content

Instantly share code, notes, and snippets.

@teddygroves
Last active January 8, 2022 11:34
Show Gist options
  • Save teddygroves/e2211cb3b75814a3033ed76c0cf9e8ec to your computer and use it in GitHub Desktop.
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
"""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()
@teddygroves
Copy link
Author

Output when I ran this script just now:

Fetching a list of words from https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt...
*******Most common letters at each position**********

1: scabtpmdgfrlhkwenouivjyzqx
2: aoeiurlhntpycmwsdbgkvxfzqj
3: rainoleutsmcdgbpkvwyhfzxjq
4: eaitnlorsudckgmpbhfvywzjxq
5: seyatnrdloihkmcpguxfbwzvqj


*******Probability of each letter at each slot**********

1: s: 0.11 c: 0.08 a: 0.07 b: 0.07 t: 0.06 p: 0.06 m: 0.05 d: 0.05 g: 0.05 f: 0.04 r: 0.04 l: 0.04 h: 0.04 k: 0.03 w: 0.03 e: 0.03 n: 0.03 o: 0.02 u: 0.02 i: 0.02 v: 0.02 j: 0.02 y: 0.01 z: 0.01 q: 0.01 x: 0.0
2: a: 0.18 o: 0.14 e: 0.12 i: 0.1 u: 0.09 r: 0.07 l: 0.05 h: 0.05 n: 0.03 t: 0.02 p: 0.02 y: 0.02 c: 0.02 m: 0.01 w: 0.01 s: 0.01 d: 0.01 b: 0.01 g: 0.01 k: 0.01 v: 0.01 x: 0.0 f: 0.0 z: 0.0 q: 0.0 j: 0.0
3: r: 0.1 a: 0.09 i: 0.08 n: 0.08 o: 0.07 l: 0.07 e: 0.06 u: 0.05 t: 0.05 s: 0.04 m: 0.04 c: 0.03 d: 0.03 g: 0.03 b: 0.03 p: 0.03 k: 0.02 v: 0.02 w: 0.02 y: 0.01 h: 0.01 f: 0.01 z: 0.01 x: 0.01 j: 0.0 q: 0.0
4: e: 0.16 a: 0.1 i: 0.08 t: 0.06 n: 0.06 l: 0.06 o: 0.06 r: 0.05 s: 0.05 u: 0.04 d: 0.03 c: 0.03 k: 0.03 g: 0.03 m: 0.03 p: 0.03 b: 0.02 h: 0.02 f: 0.01 v: 0.01 y: 0.01 w: 0.01 z: 0.01 j: 0.0 x: 0.0 q: 0.0
5: s: 0.2 e: 0.12 y: 0.11 a: 0.08 t: 0.07 n: 0.06 r: 0.06 d: 0.05 l: 0.05 o: 0.03 i: 0.03 h: 0.03 k: 0.02 m: 0.02 c: 0.01 p: 0.01 g: 0.01 u: 0.01 x: 0.01 f: 0.01 b: 0.01 w: 0.01 z: 0.0 v: 0.0 q: 0.0 j: 0.0```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment