Skip to content

Instantly share code, notes, and snippets.

@farleyta
Created January 11, 2022 14:05
Show Gist options
  • Save farleyta/34a2478dfa6bf3329b880d35316fa9a3 to your computer and use it in GitHub Desktop.
Save farleyta/34a2478dfa6bf3329b880d35316fa9a3 to your computer and use it in GitHub Desktop.
Analyze the most common letters in five-letter words vs all words. Satisfies some curiosity that arose while playing Wordle.
// Webster's Unabridged English Dictionary from:
// https://github.com/adambom/dictionary
const dictionary = require("./dictionary.json");
const alphabet = 'abcdefghijklmnopqrstuvwxyz';
const fiveLetters = Object.keys(dictionary).filter(word => word.length === 5);
// console.log(alphabet.length);
// console.log(fiveLetters.length);
const alphabetDict = alphabet.split('').reduce((dict, letter) => {
dict[letter] = 0;
return dict;
}, {});
// console.log(alphabetDict);
// counts the number of letters in each word and adds to the overall alphabet count
const getDistribution = words => {
return words.reduce((dict, word) => {
word.toLowerCase().split('').forEach(letter => {
if (dict[letter] !== undefined) {
dict[letter] = dict[letter] + 1;
}
return dict;
});
return dict;
}, alphabetDict);
}
// console.log(distribution);
// Sorts by most common and limits to 10 letters
const getDistributionSorted = words => {
const distribution = getDistribution(words);
return Object.keys(distribution).sort((a, b) => {
return distribution[b] - distribution[a];
}).reduce((acc, letter) => {
if (Object.getOwnPropertyNames(acc).length < 10) {
acc[letter] = distribution[letter];
}
return acc;
}, {});
}
console.log('Five Letters: ');
console.log(getDistributionSorted(fiveLetters));
console.log('All Letters: ');
console.log(getDistributionSorted(Object.keys(dictionary)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment