-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ashley_Benson_Seaturtles #116
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,157 @@ | ||
const letterPool = { | ||
'A': 9, | ||
'B': 2, | ||
'C': 2, | ||
'D': 4, | ||
'E': 12, | ||
'F': 2, | ||
'G': 3, | ||
'H': 2, | ||
'I': 9, | ||
'J': 1, | ||
'K': 1, | ||
'L': 4, | ||
'M': 2, | ||
'N': 6, | ||
'O': 8, | ||
'P': 2, | ||
'Q': 1, | ||
'R': 6, | ||
'S': 4, | ||
'T': 6, | ||
'U': 4, | ||
'V': 2, | ||
'W': 2, | ||
'X': 1, | ||
'Y': 2, | ||
'Z': 1 | ||
} | ||
|
||
// need to convert #'s back to int | ||
|
||
const scoreDict = { | ||
A: 1, | ||
B: 3, | ||
C: 3, | ||
D: 2, | ||
E: 1, | ||
F: 4, | ||
G: 2, | ||
H: 4, | ||
I: 1, | ||
J: 8, | ||
K: 5, | ||
L: 1, | ||
M: 3, | ||
N: 1, | ||
O: 1, | ||
P: 3, | ||
Q: 10, | ||
R: 1, | ||
S: 1, | ||
T: 1, | ||
U: 1, | ||
V: 4, | ||
W: 4, | ||
X: 8, | ||
Y: 4, | ||
Z: 10, | ||
} | ||
const alphabet = "abcdefghijklmnopqrstuvwxyz".toUpperCase().split(""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This list of characters isn't really needed (see related comment in |
||
|
||
|
||
|
||
|
||
|
||
// Implement this method for wave 1 | ||
export const drawLetters = () => { | ||
// Implement this method for wave 1 | ||
const allLetters = [] | ||
for (const [letter, frequency] of Object.entries(letterPool)) { | ||
for (let i = 0; i < frequency; i++) { | ||
allLetters.push(letter) | ||
} | ||
} | ||
Comment on lines
+68
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice approach to build up a list of all possible letter tiles. |
||
|
||
const hand = [] | ||
|
||
for (let i = 0; i < 10; i++) { | ||
const thisLetter = allLetters[Math.floor(Math.random() * allLetters.length)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than getting the letter directly, then need ing to go back and look up where it was later, consider storing the calculated index const randomPos = Math.floor(Math.random() * allLetters.length); and using that location to both look up the letter to push into the hand, as well as for removing it from the available letter tiles (avoiding the |
||
hand.push(thisLetter) | ||
let letterIndex = allLetters.indexOf(thisLetter) | ||
allLetters.splice(letterIndex, 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember that removing items from an array in O(n). For an arbitrary number of tiles, this would be a O(n^2) operation (but since we're artificially restricting to 10 tiles, we can still say this is O(10n) → O(n)). Consider shuffling the array and then picking any contiguous block of 10 tiles as the hand. This additive approach would remain O(n) regardless of the number of tiles we needed to pick. In general, try to avoid subtractive approaches (at least on arrays) since that will trend towards O(n^2) overall approaches. |
||
} | ||
return hand | ||
|
||
}; | ||
|
||
|
||
// Implement this method for wave 2 | ||
export const usesAvailableLetters = (input, lettersInHand) => { | ||
// Implement this method for wave 2 | ||
const capsWord = input.toUpperCase(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has the benefit of both making a duplicate (so we don't destroy the original word) as well as letting us treat the input word in a case insensitive fashion (assuming the hand is built using capitals). For safety, we should probably capitalize the letters from the hand as well. |
||
|
||
for (let letter of capsWord) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const test = lettersInHand[letter] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't appear to be used. |
||
if (lettersInHand.includes(letter)) { | ||
let letterIndex = lettersInHand.indexOf(letter) | ||
lettersInHand.splice(letterIndex, 1) | ||
Comment on lines
+94
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} else { | ||
return false | ||
} | ||
} | ||
return true | ||
|
||
}; | ||
|
||
|
||
// Implement this method for wave 3 | ||
export const scoreWord = (word) => { | ||
// Implement this method for wave 3 | ||
const input = word.toUpperCase(); | ||
const input_array = input.split(""); | ||
const n = input.length; | ||
let score = 0; | ||
for (let i = 0; i < n; i++) { | ||
let letter = input_array[i]; | ||
Comment on lines
+112
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider iterating over the letters in the (capitalized) word directly using for (const letter of word.toUpperCase()) {
... |
||
if (alphabet.includes(letter)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
if (letter in scoreDict) {
... |
||
score += scoreDict[letter]; | ||
} | ||
} | ||
if (n > 6 && n < 11) { | ||
score += 8; | ||
} | ||
return score; | ||
}; | ||
|
||
// Implement this method for wave 4 | ||
export const highestScoreFrom = (words) => { | ||
// Implement this method for wave 1 | ||
}; | ||
|
||
const n = words.length; | ||
let high_score = { | ||
word: "", | ||
score: 0, | ||
}; | ||
|
||
for (let i = 0; i < n; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider iterating over the words in the candidate list directly using for (const word of words) {
... then replace any references to |
||
if (scoreWord(words[i]) > high_score.score) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calculate the word score once before performing all of these checks const wordScore = scoreWord(word); // assumes for/of suggestion above
... and then replace |
||
high_score = { | ||
word: words[i], | ||
score: scoreWord(words[i]), | ||
}; | ||
//tie | ||
} else if (scoreWord(words[i]) === high_score.score) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice tie-breaker logic. |
||
if (high_score.word.length === 10) { | ||
high_score = high_score; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than re-assigning to itself, we could |
||
} else if (words[i].length === 10) { | ||
high_score = { | ||
word: words[i], | ||
score: scoreWord(words[i]), | ||
}; | ||
} else if (words[i].length < high_score.word.length) { | ||
high_score = { | ||
word: words[i], | ||
score: scoreWord(words[i]), | ||
}; | ||
} | ||
} | ||
} | ||
return high_score | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When doing global search/replace across multiple files, be sure to check for unintended side effects.