-
Notifications
You must be signed in to change notification settings - Fork 44
C22-Phoenix-Marjana Khatun #26
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?
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,126 @@ | ||
| export const drawLetters = () => { | ||
| // Implement this method for wave 1 | ||
| }; | ||
| // Implement this method for wave 1 | ||
| 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 | ||
| } | ||
| const listLetters = []; | ||
| while (listLetters.length != 10) { | ||
| let letters = Object.keys(letterPool); | ||
| let randomIndex = Math.floor(Math.random() * letters.length); | ||
| let randomLetter = letters[randomIndex]; | ||
| let value = letterPool[randomLetter]; | ||
| if (value > 0) { | ||
| letterPool[randomLetter] = value - 1; | ||
| listLetters.push(randomLetter); | ||
| } | ||
| } | ||
| return listLetters | ||
| } | ||
|
|
||
| export const usesAvailableLetters = (input, lettersInHand) => { | ||
| // Implement this method for wave 2 | ||
| }; | ||
| // Implement this method for wave 2 | ||
| input = input.toUpperCase(); | ||
| const lettersCopy = lettersInHand.slice(); | ||
| for (let letter of input) { | ||
| const index = lettersCopy.indexOf(letter); | ||
| if (index != -1) { | ||
| lettersCopy.splice(index, 1); | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
Comment on lines
+49
to
+56
Collaborator
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 implementation has an O(n^2) time complexity, how could we use a frequency map to bring the time complexity down to O(n)? |
||
| return true; | ||
| } | ||
|
|
||
| export const scoreWord = (word) => { | ||
| // Implement this method for wave 3 | ||
| // Implement this method for wave 3 | ||
| const score = { | ||
| '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 | ||
| } | ||
| let totalScore = 0; | ||
| word = word.toUpperCase(); | ||
| for (let i = 0; i < word.length; i++) { | ||
|
Collaborator
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 could simplify the syntax inside the loop a little if we use a for (const letter of word) { |
||
| let letter = word[i]; | ||
| let value = score[letter] || 0; | ||
| totalScore += value; | ||
| } | ||
| if (word.length >= 7 && word.length <= 10) { | ||
| totalScore += 8; | ||
| } | ||
| return totalScore; | ||
| }; | ||
|
|
||
| export const highestScoreFrom = (words) => { | ||
| // Implement this method for wave 4 | ||
| }; | ||
| // Implement this method for wave 4 | ||
| if (words.length === 0) { | ||
| return ['', 0] | ||
| } | ||
| let highestWord = ''; | ||
| let highestScore = 0; | ||
| for (let word of words) { | ||
| let wordScore = scoreWord(word); | ||
| if (wordScore > highestScore) { | ||
| highestScore = wordScore; | ||
| highestWord = word; | ||
| } else if (wordScore === highestScore) { | ||
| if (highestWord.length === 10) { | ||
| continue; | ||
| } else if (word.length === 10) { | ||
| highestWord = word; | ||
| } else if (word.length < highestWord.length) { | ||
| highestWord = word; | ||
| } | ||
|
Comment on lines
+118
to
+122
Collaborator
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. Could we combine these cases or do something else to reduce duplication of updating the |
||
| } | ||
| } | ||
| return {'score': highestScore, 'word' : highestWord} | ||
| } | ||
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.
This implementation has the same distribution issue that I mentioned in the Python version where there is an equal chance to pick any letter since we are choosing from the keys. Even though the tests pass, this doesn't meet what the function description is asking for, so I would like you to revisit this if you have time.