Apd
2025-01-19 17:31:03 UTC
This was the original routine as taken from the code posted here:
<vm1o34$1frsf$***@fretwizzer.eternal-september.org>
It checks if a group of letters can make a word of the same length or
shorter. Input is a word from the dictionary to check (a string) and a
list of letters you provide with a count of each one (an object of
key/value pairs), e.g:
"banana", {"b":1, "a":3, "n":2}
Spacing adjusted for readability, and commented:
// - - -
const potentialWord = (word, inputCounts) => {
const wordCounts = letterCount(word) // Make list for dictionary word.
for (const char in wordCounts) { // Loop over each letter in word.
// If letter not in your input or word has more of that letter...
if (!inputCounts[char] || wordCounts[char] > inputCounts[char]) {
return false // Quit, word cannot be made.
}
}
return true // Loop completed, thus word can be made..
}
// - - -
My updated version allows for any number of wildcards ("?") which will
match any letter, e.g:
"bana?a", {"b":1, "a":3, "n":1, "?":1}
// - - -
const potentialWord = (word, inputCounts) => {
const wordCounts = letterCount(word) // Make list for dictionary word.
let wildCard = inputCounts["?"] ? inputCounts["?"] : 0 // Wildcard count
for (const char in wordCounts) { // Loop over each letter in word.
// If letter not in your input or word has more of that letter...
if (!inputCounts[char] || wordCounts[char] > inputCounts[char]) {
// Check for wildcards.
if (wildCard <= 0) { // If none present or none remaining.
return false // Quit, word cannot be made.
}
if (!inputCounts[char]) { // If word letter not in input
// and more of this letter in word than remaining wildcards:
if (wordCounts[char] > wildCard) {
return false // Quit, word cannot be made.
}
// Otherwise, use this wildcard for this letter and decrease
// remaining wildcards by count of that letter in word.
else wildCard -= wordCounts[char]
}
else { // If word letter found in input
// and more of this letter in word than input letter plus
// remaining wildcards:
if (wordCounts[char] > inputCounts[char] + wildCard) {
return false // Quit, word cannot be made.
}
// otherwise, use this wildcard for this letter and
// decrease remaining wildcards by one.
else wildCard --
}
}
}
return true // loop completed, thus word can be made..
}
// - - -
I don't know how easy that is to follow. It took me some time to work
out after being sidetracked by what I thought would be necessary but
turned out to be irrelevant. I think it's now bug-free.
<vm1o34$1frsf$***@fretwizzer.eternal-september.org>
It checks if a group of letters can make a word of the same length or
shorter. Input is a word from the dictionary to check (a string) and a
list of letters you provide with a count of each one (an object of
key/value pairs), e.g:
"banana", {"b":1, "a":3, "n":2}
Spacing adjusted for readability, and commented:
// - - -
const potentialWord = (word, inputCounts) => {
const wordCounts = letterCount(word) // Make list for dictionary word.
for (const char in wordCounts) { // Loop over each letter in word.
// If letter not in your input or word has more of that letter...
if (!inputCounts[char] || wordCounts[char] > inputCounts[char]) {
return false // Quit, word cannot be made.
}
}
return true // Loop completed, thus word can be made..
}
// - - -
My updated version allows for any number of wildcards ("?") which will
match any letter, e.g:
"bana?a", {"b":1, "a":3, "n":1, "?":1}
// - - -
const potentialWord = (word, inputCounts) => {
const wordCounts = letterCount(word) // Make list for dictionary word.
let wildCard = inputCounts["?"] ? inputCounts["?"] : 0 // Wildcard count
for (const char in wordCounts) { // Loop over each letter in word.
// If letter not in your input or word has more of that letter...
if (!inputCounts[char] || wordCounts[char] > inputCounts[char]) {
// Check for wildcards.
if (wildCard <= 0) { // If none present or none remaining.
return false // Quit, word cannot be made.
}
if (!inputCounts[char]) { // If word letter not in input
// and more of this letter in word than remaining wildcards:
if (wordCounts[char] > wildCard) {
return false // Quit, word cannot be made.
}
// Otherwise, use this wildcard for this letter and decrease
// remaining wildcards by count of that letter in word.
else wildCard -= wordCounts[char]
}
else { // If word letter found in input
// and more of this letter in word than input letter plus
// remaining wildcards:
if (wordCounts[char] > inputCounts[char] + wildCard) {
return false // Quit, word cannot be made.
}
// otherwise, use this wildcard for this letter and
// decrease remaining wildcards by one.
else wildCard --
}
}
}
return true // loop completed, thus word can be made..
}
// - - -
I don't know how easy that is to follow. It took me some time to work
out after being sidetracked by what I thought would be necessary but
turned out to be irrelevant. I think it's now bug-free.