-
Couldn't load subscription status.
- Fork 20
Adagrams-JS-Supriya C #5
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 |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |
| }, | ||
| "dependencies": { | ||
| "core-js": "^3.8.0", | ||
| "vorpal": "^1.12.0" | ||
| "vorpal": "^1.12.0", | ||
| "yarn": "^1.22.19" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,154 @@ | ||
| export const drawLetters = () => { | ||
| // Implement this method for wave 1 | ||
| let lettersDictionary = { | ||
| "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 | ||
| }; | ||
| let count = 0; | ||
| const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
| let lettersInHand = []; | ||
| while (count < 10){ | ||
|
Comment on lines
+32
to
+33
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 remove the variable 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. Thank you ! |
||
| let letterChosen = characters.charAt(Math.floor(Math.random() * 26)); | ||
|
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? 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. Maybe I will use dictionary of letters to pick random characters. Will do some more research on this. |
||
| if (lettersDictionary[letterChosen] > 0){ | ||
| lettersInHand.push(letterChosen); | ||
| count += 1; | ||
| lettersDictionary[letterChosen] -= 1; | ||
| } | ||
| } | ||
| return lettersInHand; | ||
| }; | ||
|
|
||
| export const usesAvailableLetters = (input, lettersInHand) => { | ||
| // Implement this method for wave 2 | ||
| //Code to create the dictionary of the letters in hand | ||
| const dictOfLettersInHand = {} | ||
| for (let i=0; i< lettersInHand.length ; i++){ | ||
| if (lettersInHand[i] in dictOfLettersInHand){ | ||
|
|
||
| let value = dictOfLettersInHand[lettersInHand[i]] ; | ||
| dictOfLettersInHand[lettersInHand[i]] = value +1; | ||
| } | ||
| else { | ||
| dictOfLettersInHand[lettersInHand[i]] = 1; | ||
| } | ||
| } | ||
| //code to check occuerences and no. of occurences of characters | ||
| //in input in the dictionary | ||
| for (let i=0; i< input.length; i++){ | ||
| if(dictOfLettersInHand[input[i]] == null){ | ||
|
Comment on lines
+59
to
+60
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 issue: we should indent the contents of for-loops so it's clear what code is inside the block. |
||
| return false; | ||
| } else if (dictOfLettersInHand[input[i]]===0) | ||
| { | ||
| return false; | ||
| } | ||
| dictOfLettersInHand[input[i]] -= 1; | ||
| } | ||
|
Comment on lines
+59
to
+67
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, I like the use of a frequency map. To make this a little easier to read and reduce repetition, you could pull for (let i = 0; i < input.length; i++) {
letterValue = dictOfLettersInHand[input[i]]
if (letterValue == null || letterValue === 0) {
return false;
}
dictOfLettersInHand[input[i]] -= 1;
} |
||
| return true; | ||
| }; | ||
|
|
||
| export const scoreWord = (word) => { | ||
| // Implement this method for wave 3 | ||
| let lettersDictionary = { | ||
| "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 | ||
| }; | ||
| let pointsForWord = 0; | ||
| let upperCaseWord = word.toUpperCase(); | ||
|
Comment on lines
+100
to
+101
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 suggest placing these variable declarations under the if statement below so they are only created if they will be used. |
||
| if (word.length === 0) | ||
| { | ||
|
Comment on lines
+102
to
+103
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. Specific teams might have their own conventions, but typically we place the opening brace on the same line as the if-statement. |
||
| return 0; | ||
| } | ||
| //Code to add scores of the letters in the word | ||
| for (let i =0; i< upperCaseWord.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 |
||
| if (upperCaseWord.charAt(i) in lettersDictionary){ | ||
| pointsForWord += lettersDictionary[upperCaseWord.charAt(i)]; | ||
| } | ||
| } | ||
|
Comment on lines
+107
to
+111
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 this loop a little with a for...of loop: for (const letter of upperCaseWord) {
if (lettersDictionary[letter] != undefined) {
// If its undefined means letter is a special character
pointsForWord += lettersDictionary[letter];
}
} |
||
| // update the score based on its length | ||
| if ((upperCaseWord.length >6) && (upperCaseWord.length <= 10)) { | ||
| pointsForWord += 8; | ||
| } | ||
| return pointsForWord | ||
| }; | ||
|
|
||
| export const highestScoreFrom = (words) => { | ||
| // Implement this method for wave 4 | ||
| }; | ||
| //Code to find the word with max score and | ||
| //maximum score of the word in the given list of words | ||
| let wordScoreDict = {} | ||
| let maxScore = 0 | ||
| let wordMaxScore = words[0] | ||
| for (let curWord of words) | ||
| { | ||
| let scoreOfCurWord = scoreWord(curWord) | ||
| wordScoreDict[curWord] = scoreOfCurWord | ||
| if (scoreOfCurWord >= maxScore) | ||
| { | ||
| maxScore = scoreOfCurWord; | ||
| wordMaxScore = curWord; | ||
| } | ||
| } | ||
| //code to get the right word based on length of the word | ||
| let resultWord = wordMaxScore | ||
| let minWordLength = wordMaxScore.length | ||
| for(const key in wordScoreDict){ | ||
| let curWordScore = wordScoreDict[key]; | ||
| if (curWordScore === maxScore) | ||
| { | ||
| if(key.length === 10){ | ||
| resultWord = key; | ||
| break; | ||
| } | ||
| else if (key.length <= minWordLength){ | ||
| minWordLength = key.length; | ||
| resultWord = key; | ||
| } | ||
| } | ||
| } | ||
| let finalResult = { word: resultWord, score: scoreWord(resultWord) }; | ||
| return finalResult; | ||
| }; | ||
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.
Small style nitpick: I would consider outdenting this closing bracket so it line up with the beginning of line 2.