-
Notifications
You must be signed in to change notification settings - Fork 44
WeiQ- Sphinx #29
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
base: main
Are you sure you want to change the base?
WeiQ- Sphinx #29
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,114 @@ | ||||||||||||||||||||||||
const LETTER_POOL = { | ||||||||||||||||||||||||
'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 | ||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
const LetterPoolList = []; | ||||||||||||||||||||||||
for (const [letter, count] of Object.entries(LETTER_POOL)) { | ||||||||||||||||||||||||
for (let i = 0; i < count; i++) { | ||||||||||||||||||||||||
LetterPoolList.push(letter); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
Comment on lines
+30
to
+35
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. Prefer this logic be put in a helper function that you can call in |
||||||||||||||||||||||||
const totalLetterCount = LetterPoolList.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.
Suggested change
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
export const drawLetters = () => { | ||||||||||||||||||||||||
// Implement this method for wave 1 | ||||||||||||||||||||||||
const handOfLetters = []; | ||||||||||||||||||||||||
for (let i = 0; i < 10; i++) { | ||||||||||||||||||||||||
const randomIndex = Math.floor(Math.random() * (totalLetterCount - i)); | ||||||||||||||||||||||||
const randonDrawLetter = LetterPoolList[randomIndex]; | ||||||||||||||||||||||||
handOfLetters.push(randonDrawLetter); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
const lastIndex = totalLetterCount - i - 1; | ||||||||||||||||||||||||
[LetterPoolList[randomIndex], LetterPoolList[lastIndex]] = [LetterPoolList[lastIndex], LetterPoolList[randomIndex]] | ||||||||||||||||||||||||
Comment on lines
+47
to
+48
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 work swapping the randomly drawn letter to the end of the list! This a constant time operation which beats an approach that uses |
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
return handOfLetters; | ||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
export const usesAvailableLetters = (input, lettersInHand) => { | ||||||||||||||||||||||||
// Implement this method for wave 2 | ||||||||||||||||||||||||
const letterFreMap = {}; | ||||||||||||||||||||||||
for (const letter of lettersInHand) { | ||||||||||||||||||||||||
if (!(letter in letterFreMap)) { | ||||||||||||||||||||||||
letterFreMap[letter] = 1; | ||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||
letterFreMap[letter] += 1; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
Comment on lines
+56
to
+62
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. 👍 I like the frequency map here so you can leverage the constant time look up. |
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
for (const letter of input) { | ||||||||||||||||||||||||
if (!(letter in letterFreMap) || letterFreMap[letter] < 1) { | ||||||||||||||||||||||||
return false; | ||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||
letterFreMap[letter] -= 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. You can decrement like |
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
return true; | ||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
const SCORE_CHART = { | ||||||||||||||||||||||||
'A': 1, 'E': 1, 'I': 1, 'O': 1, 'U': 1, 'L': 1, 'N': 1, 'R': 1, 'S': 1, 'T': 1, | ||||||||||||||||||||||||
'D': 2, 'G': 2, | ||||||||||||||||||||||||
'B': 3, 'C': 3, 'M': 3, 'P': 3, | ||||||||||||||||||||||||
'F': 4, 'H': 4, 'V': 4, 'W': 4, 'Y': 4, | ||||||||||||||||||||||||
'K': 5, | ||||||||||||||||||||||||
'J': 8, 'X': 8, | ||||||||||||||||||||||||
'Q': 10, 'Z': 10 | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
export const scoreWord = (word) => { | ||||||||||||||||||||||||
// Implement this method for wave 3 | ||||||||||||||||||||||||
let score = 0 | ||||||||||||||||||||||||
word = word.toUpperCase(); | ||||||||||||||||||||||||
for (let char of word) { | ||||||||||||||||||||||||
Comment on lines
+88
to
+89
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. We should use Can also write lines 88-89 in one line:
Suggested change
|
||||||||||||||||||||||||
score += SCORE_CHART[char]; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if ([7, 8, 9, 10].includes(word.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. While the time complexity of using
Suggested change
Writing the check this way feels like it conveys that you're checking the length is between x and y value. |
||||||||||||||||||||||||
score += 8; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
return score; | ||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
export const highestScoreFrom = (words) => { | ||||||||||||||||||||||||
// Implement this method for wave 4 | ||||||||||||||||||||||||
let currentMax = 0; | ||||||||||||||||||||||||
let currentWinWord = ''; | ||||||||||||||||||||||||
for (const word of words) { | ||||||||||||||||||||||||
if (scoreWord(word) > currentMax) { | ||||||||||||||||||||||||
currentMax = scoreWord(word); | ||||||||||||||||||||||||
currentWinWord = word; | ||||||||||||||||||||||||
} else if (scoreWord(word) === currentMax && currentWinWord.length != 10) { | ||||||||||||||||||||||||
Comment on lines
+103
to
+107
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. Between lines 104-107, you invoke the
Suggested change
|
||||||||||||||||||||||||
if(word.length < currentWinWord.length || word.length === 10) | ||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||
currentWinWord = word; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
return {'word':currentWinWord, 'score':currentMax} | ||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -120,7 +120,9 @@ describe("Adagrams", () => { | |||||
}); | ||||||
|
||||||
it("returns a score of 0 if given an empty input", () => { | ||||||
throw "Complete test"; | ||||||
expectScores({ | ||||||
'':0 | ||||||
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.
Suggested change
|
||||||
}) | ||||||
}); | ||||||
|
||||||
it("adds an extra 8 points if word is 7 or more characters long", () => { | ||||||
|
@@ -133,7 +135,7 @@ describe("Adagrams", () => { | |||||
}); | ||||||
}); | ||||||
|
||||||
describe.skip("highestScoreFrom", () => { | ||||||
describe("highestScoreFrom", () => { | ||||||
it("returns a hash that contains the word and score of best word in an array", () => { | ||||||
const words = ["X", "XX", "XXX", "XXXX"]; | ||||||
const correct = { word: "XXXX", score: scoreWord("XXXX") }; | ||||||
|
@@ -145,7 +147,7 @@ describe("Adagrams", () => { | |||||
const words = ["XXX", "XXXX", "X", "XX"]; | ||||||
const correct = { word: "XXXX", score: scoreWord("XXXX") }; | ||||||
|
||||||
throw "Complete test by adding an assertion"; | ||||||
expect(highestScoreFrom(words)).toEqual(correct); | ||||||
}); | ||||||
|
||||||
describe("in case of tied 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.
Nice job using capital letters to name your constant variable!