Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"dependencies": {
"core-js": "^3.8.0",
"vorpal": "^1.12.0"
"vorpal": "^1.12.0",
"yarn": "^1.22.19"
}
}
149 changes: 144 additions & 5 deletions src/adagrams.js
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
};

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.

let count = 0;
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let lettersInHand = [];
while (count < 10){
Comment on lines +32 to +33

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could remove the variable count and directly check the length of lettersInHand in the condition.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you !

let letterChosen = characters.charAt(Math.floor(Math.random() * 26));

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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 dictOfLettersInHand[input[i]] into a variable before the if-statements:

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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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++){

Choose a reason for hiding this comment

The 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 const over let to declare the variable.

if (upperCaseWord.charAt(i) in lettersDictionary){
pointsForWord += lettersDictionary[upperCaseWord.charAt(i)];
}
}
Comment on lines +107 to +111

Choose a reason for hiding this comment

The 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;
};
11 changes: 8 additions & 3 deletions test/adagrams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ describe("Adagrams", () => {
A: 1,
DOG: 5,
WHIMSY: 17,
"":0
});
});

Expand All @@ -116,11 +117,14 @@ describe("Adagrams", () => {
a: 1,
dog: 5,
wHiMsY: 17,
"" : 0,
});
});

it("returns a score of 0 if given an empty input", () => {
throw "Complete test";
expectScores({
"" : 0,
});
});

it("adds an extra 8 points if word is 7 or more characters long", () => {
Expand All @@ -133,7 +137,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") };
Expand All @@ -145,7 +149,8 @@ describe("Adagrams", () => {
const words = ["XXX", "XXXX", "X", "XX"];
const correct = { word: "XXXX", score: scoreWord("XXXX") };

throw "Complete test by adding an assertion";
//throw "Complete test by adding an assertion";
expect(highestScoreFrom(words)).toEqual(correct);
});

describe("in case of tied score", () => {
Expand Down
Loading