Skip to content

Tigers - Melley #130

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"jest.jestCommandLine": "yarn test"
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^24.8.0",
"babel-plugin-module-resolver": "^3.2.0",
"eslint": "^8.28.0",
"eslint-plugin-jest": "^27.1.6",
"jest": "^24.8.0",
"regenerator-runtime": "^0.12.1"
},
Expand Down
116 changes: 115 additions & 1 deletion src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,129 @@
const letterPool = Array(9)

Choose a reason for hiding this comment

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

👍 😀

.fill("A")
.concat(Array(2).fill("B"))
.concat(Array(2).fill("C"))
.concat(Array(4).fill("D"))
.concat(Array(12).fill("E"))
.concat(Array(2).fill("F"))
.concat(Array(3).fill("G"))
.concat(Array(2).fill("H"))
.concat(Array(9).fill("I"))
.concat(Array(1).fill("J"))
.concat(Array(1).fill("K"))
.concat(Array(4).fill("L"))
.concat(Array(2).fill("M"))
.concat(Array(6).fill("N"))
.concat(Array(8).fill("O"))
.concat(Array(2).fill("P"))
.concat(Array(1).fill("Q"))
.concat(Array(6).fill("R"))
.concat(Array(4).fill("S"))
.concat(Array(6).fill("T"))
.concat(Array(4).fill("U"))
.concat(Array(2).fill("V"))
.concat(Array(2).fill("W"))
.concat(Array(1).fill("X"))
.concat(Array(2).fill("Y"))
.concat(Array(1).fill("Z"));
const scoreDict = {
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,
};

export const drawLetters = () => {

Choose a reason for hiding this comment

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

👍

// Implement this method for wave 1
let adjustedLetterPool = [...letterPool];
let handList = [];
for (let i = 0; i < 10; i++) {
let currentLetter =
adjustedLetterPool[Math.floor(Math.random() * adjustedLetterPool.length)];
let letterIndex = adjustedLetterPool.indexOf(currentLetter);
adjustedLetterPool.splice(letterIndex, 1);
handList.push(currentLetter);
}
return handList;
};

export const usesAvailableLetters = (input, lettersInHand) => {

Choose a reason for hiding this comment

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

👍 Very clear

// Implement this method for wave 2
// // Implement this method for wave 2
let lettersInHandCount = {};
for (const letter of lettersInHand) {
if (letter in lettersInHandCount) {
lettersInHandCount[letter] += 1;
} else {
lettersInHandCount[letter] = 1;
}
}

const upperCaseInput = input.toUpperCase();
for (const letter of upperCaseInput) {
if (!(letter in lettersInHandCount)) {
return false;
}
if (lettersInHandCount[letter] === 0) {
return false;
} else {
lettersInHandCount[letter] -= 1;
}
}
return true;
};

export const scoreWord = (word) => {

Choose a reason for hiding this comment

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

👍

// Implement this method for wave 3
const uppercaseWord = word.toUpperCase();
let score = 0;
for (const letter of uppercaseWord) {
score += scoreDict[letter];
}
if (word.length > 6) {
score += 8;
}
return score;
};

export const highestScoreFrom = (words) => {

Choose a reason for hiding this comment

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

👍

// Implement this method for wave 4
let highestScore = 0;
let winningWord = "";
for (const word of words) {
let currentScore = scoreWord(word);
if (word.length === 10) {
return { word: word, score: currentScore };
}
if (currentScore > highestScore) {
highestScore = currentScore;
winningWord = word;
} else if (
currentScore === highestScore &&
word.length < winningWord.length
) {
winningWord = word;
}
}
return { word: winningWord, score: highestScore };
};
59 changes: 29 additions & 30 deletions test/adagrams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
usesAvailableLetters,
scoreWord,
highestScoreFrom,
} from "adagrams";
} from 'adagrams';

const LETTER_POOL = {
A: 9,
Expand Down Expand Up @@ -34,15 +34,15 @@ const LETTER_POOL = {
Z: 1,
};

describe("Adagrams", () => {
describe("drawLetters", () => {
it("draws ten letters from the letter pool", () => {
describe('Adagrams', () => {
describe('drawLetters', () => {
it('draws ten letters from the letter pool', () => {
const drawn = drawLetters();

expect(drawn).toHaveLength(10);
});

it("returns an array, and each item is a single-letter string", () => {
it('returns an array, and each item is a single-letter string', () => {
const drawn = drawLetters();

expect(Array.isArray(drawn)).toBe(true);
Expand All @@ -51,52 +51,49 @@ describe("Adagrams", () => {
});
});

it("does not draw a letter too many times", () => {
it('does not draw a letter too many times', () => {
for (let i = 0; i < 1000; i++) {
const drawn = drawLetters();
const letter_freq = {};
const letterFreq = {};
for (let letter of drawn) {
if (letter in letter_freq) {
letter_freq[letter] += 1;
} else {
letter_freq[letter] = 1;
if (letter in letterFreq) {
letterFreq[letter] += 1;
} else {letterFreq[letter] = 1;
}
}

for (let letter of drawn) {
expect(letter_freq[letter]).toBeLessThanOrEqual(LETTER_POOL[letter]);
}
for(let letter of drawn) {
expect(letterFreq[letter]).toBeLessThanOrEqual(LETTER_POOL[letter]);
}
}
});
});

describe("usesAvailableLetters", () => {
it("returns true if the submitted letters are valid against the drawn letters", () => {
const drawn = ["D", "O", "G", "X", "X", "X", "X", "X", "X", "X"];
const word = "DOG";
describe('usesAvailableLetters', () => {
it('returns true if the submitted letters are valid against the drawn letters', () => {
const drawn = ['D', 'O', 'G', 'X', 'X', 'X', 'X', 'X', 'X', 'X'];
const word = 'DOG';

const isValid = usesAvailableLetters(word, drawn);
expect(isValid).toBe(true);
});

it("returns false when word contains letters not in the drawn letters", () => {
const drawn = ["D", "O", "X", "X", "X", "X", "X", "X", "X", "X"];
const word = "DOG";
it('returns false when word contains letters not in the drawn letters', () => {
const drawn = ['D', 'O', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'];
const word = 'DOG';

const isValid = usesAvailableLetters(word, drawn);
expect(isValid).toBe(false);
});

it("returns false when word contains repeated letters more than in the drawn letters", () => {
const drawn = ["D", "O", "G", "X", "X", "X", "X", "X", "X", "X"];
const word = "GOOD";
it('returns false when word contains repeated letters more than in the drawn letters', () => {
const drawn = ['D', 'O', 'G', 'X', 'X', 'X', 'X', 'X', 'X', 'X'];
const word = 'GOOD';

const isValid = usesAvailableLetters(word, drawn);
expect(isValid).toBe(false);
});
});

describe("scoreWord", () => {
describe('scoreWord', () => {
const expectScores = (wordScores) => {
Object.entries(wordScores).forEach(([word, score]) => {
expect(scoreWord(word)).toBe(score);
Expand All @@ -120,7 +117,8 @@ describe("Adagrams", () => {
});

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

it("adds an extra 8 points if word is 7 or more characters long", () => {
Expand All @@ -133,7 +131,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 +143,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