-
Notifications
You must be signed in to change notification settings - Fork 20
AC2-Ocelots-Elaine(Si Ok) Sohn #19
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
5312dc5
58d46e1
e17107a
8307c2d
e38aa82
2e1a2f1
309e7b1
c7f9ebb
8679811
2df3815
afdb887
2e2f74b
46f493a
2770af8
98cb509
7e2fa49
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,165 @@ | ||
| export const drawLetters = () => { | ||
| // Implement this method for wave 1 | ||
| }; | ||
| class Adagrams{ | ||
| 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, | ||
| }; | ||
|
|
||
| export const usesAvailableLetters = (input, lettersInHand) => { | ||
| // Implement this method for wave 2 | ||
| }; | ||
| LETTER_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. Since these constants at the top of the file are only accessed by one function each, it would be reasonable to place the objects inside the functions rather than as a constant. There are tradeoffs, the structure would clutter the function some, but it keeps the data as close as possible to where it's being used, and would mean that other functions who shouldn't need it couldn't access it. |
||
| 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, | ||
| }; | ||
|
|
||
| constructor() { | ||
| this.input = ""; | ||
| this.word = ""; | ||
| this.highest = { | ||
| word: "", | ||
| score: 0 | ||
| }; | ||
|
|
||
| this.lettersInHand = new Array(); | ||
|
|
||
| }; | ||
|
|
||
| drawLetters() { | ||
| while (this.lettersInHand.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. Nice use of the hand length to control the while loop |
||
| let letter = String.fromCharCode(Math.floor(Math.random() * 26) + 65); | ||
|
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 will give us an even chance of picking any single letter in the alphabet without going over the number of each tile we have. This is slightly different than what the README asks - we won't accurately represent the distribution of tiles because we pick a letter from 1-26, when the chances of picking some letters should be higher than others. How could we update the algorithm to account for this? |
||
|
|
||
| if (this.LETTER_POOL[letter] > 0) { | ||
| this.LETTER_POOL[letter] -= 1; | ||
| this.lettersInHand.push(letter); | ||
| } | ||
|
Comment on lines
+76
to
+79
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 we are directly editing |
||
| letter = String.fromCharCode(Math.floor(Math.random() * 26) + 65); | ||
|
|
||
| }; | ||
| return this.lettersInHand; | ||
| }; | ||
|
|
||
| isValid(word) { | ||
| if(word.length < 1) return false; | ||
|
|
||
| for(let i=0; i < word.length; ++i){ | ||
|
|
||
| if (!(/^[A-Z]$/.test(word[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. Cool use of a regular expression! |
||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| }; | ||
|
|
||
| usesAvailableLetters(input, lettersInHand) { | ||
| this.input = input.toUpperCase(); | ||
| if (!this.isValid(this.input)){ | ||
| return false; | ||
| } | ||
| this.lettersInHand = lettersInHand; | ||
|
|
||
| let letterFreq = {}; | ||
| for (let i=0; i < this.lettersInHand.length; i++) { | ||
| let letter = this.lettersInHand[i] | ||
| letterFreq[letter] = (letterFreq[letter] || 0) +1 ; | ||
| }; | ||
|
Comment on lines
+105
to
+109
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 frequency map! |
||
|
|
||
| for (let i=0; i < this.input.length; i++) { | ||
| let letter = this.input[i]; | ||
| if ((letter in letterFreq)&&(letterFreq[letter] > 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. Small style best practice note: we should include a blank space on either side of operators like |
||
| letterFreq[letter] -= 1; | ||
| } else { | ||
| return false; | ||
| } | ||
| }; | ||
| return true; | ||
| }; | ||
|
|
||
| scoreWord(word) { | ||
| this.word = word.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. We wouldn't typically hold onto the input of a function like this as an attribute on a class. If it isn't important to persist between function calls, then we should re-evaluate if it's something the class itself should hold. |
||
| if (!this.isValid(this.word)){ | ||
| return 0; | ||
| } | ||
| let point = 0; | ||
|
|
||
| for (let i=0; i < this.word.length; ++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. If we won't be reassigning the loop variable in the body of a loop, I recommend using |
||
| point += this.LETTER_SCORE[this.word[i]]; | ||
| } | ||
|
Comment on lines
+127
to
+131
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 here a little with a for...of loop: for (const letter of this.word) {
point += this.LETTER_SCORE[letter];
}
Comment on lines
+127
to
+131
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 could have an unexpected result if the string |
||
|
|
||
| if(this.word.length >= 7){ | ||
| point += 8; | ||
| } | ||
|
|
||
| return point; | ||
| }; | ||
|
|
||
| highestScoreFrom(words) { | ||
|
|
||
| for (let word of words) { | ||
|
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 solution to tie break in a single loop! |
||
| let point = this.scoreWord(word); | ||
|
|
||
| if (point > this.highest.score){ | ||
| this.highest.word = word; | ||
| this.highest.score = point; | ||
| } else if (point === this.highest.score) { | ||
| if(this.highest.word.length === 10){ | ||
| continue; | ||
| } else if ((word.length === 10)||(this.highest.word.length > word.length)){ | ||
| this.highest.word = word; | ||
| this.highest.score = point; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| return this.highest | ||
| }; | ||
|
|
||
| ;} | ||
|
|
||
| export default Adagrams; | ||
|
|
||
| export const scoreWord = (word) => { | ||
| // Implement this method for wave 3 | ||
| }; | ||
|
|
||
| export const highestScoreFrom = (words) => { | ||
| // Implement this method for wave 4 | ||
| }; | ||
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.
If these shouldn't be edited, I would consider creating them as a static, read-only properties: https://stackoverflow.com/a/35242218