-
Notifications
You must be signed in to change notification settings - Fork 710
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
Cheetahs- Tasha C. #45
base: main
Are you sure you want to change the base?
Changes from all commits
cfe1b8e
bbf514e
9087095
81eead6
2bf8702
0d7dc41
45765c2
1c63b00
da38134
cf9478e
33d5710
17bb455
13ce999
79d3d72
7734f54
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,138 @@ | ||
export const drawLetters = () => { | ||
// 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, | ||
}; | ||
|
||
export const usesAvailableLetters = (input, lettersInHand) => { | ||
// Implement this method for wave 2 | ||
const scoreChart = { | ||
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. Excellent! |
||
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, | ||
}; | ||
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
|
||
export const scoreWord = (word) => { | ||
// Implement this method for wave 3 | ||
}; | ||
// ------------ Wave 05--------------------------- | ||
class Adagrams { | ||
static drawLetters() { | ||
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. Excellent! |
||
let availableLetters = { ...letterPool }; | ||
const lettersInHand = []; | ||
|
||
export const highestScoreFrom = (words) => { | ||
// Implement this method for wave 4 | ||
}; | ||
while (lettersInHand.length < 10) { | ||
const letter = alphabet[Math.floor(Math.random() * alphabet.length)]; | ||
|
||
if (availableLetters[letter] > 0) { | ||
availableLetters[letter]--; | ||
lettersInHand.push(letter); | ||
} | ||
} | ||
return lettersInHand; | ||
} | ||
|
||
static usesAvailableLetters(input, lettersInHand) { | ||
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. Excellent! |
||
const drawnDict = {}; | ||
lettersInHand.forEach((letter) => { | ||
if (letter in drawnDict) { | ||
drawnDict[letter]++; | ||
} else { | ||
drawnDict[letter] = 1; | ||
} | ||
}); | ||
const word = [...input.toUpperCase()]; | ||
|
||
for (const letter of word) { | ||
if (drawnDict[letter] == 0 || !drawnDict[letter]) { | ||
return false; | ||
} else { | ||
drawnDict[letter]--; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
static scoreWord(word) { | ||
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. Excellent! |
||
const wordList = [...word.toUpperCase()]; | ||
let score = 0; | ||
if (wordList.length >= 7) { | ||
score += 8; | ||
} | ||
|
||
wordList.forEach((letter) => (score += scoreChart[letter])); | ||
|
||
return score; | ||
} | ||
|
||
static highestScoreFrom(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. Excellent! |
||
let highestScore = 0; | ||
let winningWord = ""; | ||
|
||
words.forEach((word) => { | ||
const score = Adagrams.scoreWord(word); | ||
|
||
if (score > highestScore) { | ||
highestScore = score; | ||
winningWord = word; | ||
} else if (score === highestScore) { | ||
if (word.length === 10 && winningWord.length != 10) { | ||
winningWord = word; | ||
} else if ( | ||
word.length < winningWord.length && | ||
winningWord.length != 10 | ||
) { | ||
winningWord = word; | ||
} | ||
} | ||
}); | ||
|
||
const winner = { word: winningWord, score: highestScore }; | ||
return winner; | ||
} | ||
} | ||
|
||
export default Adagrams; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,4 @@ | ||
import { | ||
drawLetters, | ||
usesAvailableLetters, | ||
scoreWord, | ||
highestScoreFrom, | ||
} from "adagrams"; | ||
|
||
import Adagrams from "adagrams"; | ||
const LETTER_POOL = { | ||
A: 9, | ||
B: 2, | ||
|
@@ -36,14 +30,16 @@ const LETTER_POOL = { | |
|
||
describe("Adagrams", () => { | ||
describe("drawLetters", () => { | ||
// const newAdagram = new Adagrams() | ||
|
||
it("draws ten letters from the letter pool", () => { | ||
const drawn = drawLetters(); | ||
const drawn = Adagrams.drawLetters(); | ||
|
||
expect(drawn).toHaveLength(10); | ||
}); | ||
|
||
it("returns an array, and each item is a single-letter string", () => { | ||
const drawn = drawLetters(); | ||
const drawn = Adagrams.drawLetters(); | ||
|
||
expect(Array.isArray(drawn)).toBe(true); | ||
drawn.forEach((l) => { | ||
|
@@ -53,7 +49,7 @@ describe("Adagrams", () => { | |
|
||
it("does not draw a letter too many times", () => { | ||
for (let i = 0; i < 1000; i++) { | ||
const drawn = drawLetters(); | ||
const drawn = Adagrams.drawLetters(); | ||
const letter_freq = {}; | ||
for (let letter of drawn) { | ||
if (letter in letter_freq) { | ||
|
@@ -73,33 +69,34 @@ describe("Adagrams", () => { | |
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); | ||
const word = "DOG"; | ||
const isValid = Adagrams.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"; | ||
|
||
const isValid = usesAvailableLetters(word, drawn); | ||
const isValid = Adagrams.usesAvailableLetters(word, drawn); | ||
console.log(isValid); | ||
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"; | ||
|
||
const isValid = usesAvailableLetters(word, drawn); | ||
const isValid = Adagrams.usesAvailableLetters(word, drawn); | ||
expect(isValid).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("scoreWord", () => { | ||
const expectScores = (wordScores) => { | ||
Object.entries(wordScores).forEach(([word, score]) => { | ||
expect(scoreWord(word)).toBe(score); | ||
expect(Adagrams.scoreWord(word)).toBe(score); | ||
}); | ||
}; | ||
|
||
|
@@ -120,7 +117,9 @@ describe("Adagrams", () => { | |
}); | ||
|
||
it("returns a score of 0 if given an empty input", () => { | ||
throw "Complete test"; | ||
expectScores({ | ||
"": 0, | ||
}); | ||
Comment on lines
+120
to
+122
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. Excellent! |
||
}); | ||
|
||
it("adds an extra 8 points if word is 7 or more characters long", () => { | ||
|
@@ -133,24 +132,24 @@ 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") }; | ||
const correct = { word: "XXXX", score: Adagrams.scoreWord("XXXX") }; | ||
|
||
expect(highestScoreFrom(words)).toEqual(correct); | ||
expect(Adagrams.highestScoreFrom(words)).toEqual(correct); | ||
}); | ||
|
||
it("accurately finds best scoring word even if not sorted", () => { | ||
const words = ["XXX", "XXXX", "X", "XX"]; | ||
const correct = { word: "XXXX", score: scoreWord("XXXX") }; | ||
const correct = { word: "XXXX", score: Adagrams.scoreWord("XXXX") }; | ||
|
||
throw "Complete test by adding an assertion"; | ||
expect(Adagrams.highestScoreFrom(words)).toEqual(correct); | ||
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. Excellent! |
||
}); | ||
|
||
describe("in case of tied score", () => { | ||
const expectTie = (words) => { | ||
const scores = words.map((word) => scoreWord(word)); | ||
const scores = words.map((word) => Adagrams.scoreWord(word)); | ||
const highScore = scores.reduce((h, s) => (h < s ? s : h), 0); | ||
const tiedWords = scores.filter((s) => s == highScore); | ||
|
||
|
@@ -162,37 +161,37 @@ describe("Adagrams", () => { | |
const words = ["AAAAAAAAAA", "BBBBBB"]; | ||
const correct = { | ||
word: "AAAAAAAAAA", | ||
score: scoreWord("AAAAAAAAAA"), | ||
score: Adagrams.scoreWord("AAAAAAAAAA"), | ||
}; | ||
expectTie(words); | ||
|
||
expect(highestScoreFrom(words)).toEqual(correct); | ||
expect(highestScoreFrom(words.reverse())).toEqual(correct); | ||
expect(Adagrams.highestScoreFrom(words)).toEqual(correct); | ||
expect(Adagrams.highestScoreFrom(words.reverse())).toEqual(correct); | ||
}); | ||
|
||
it("selects the word with fewer letters when neither are 10 letters", () => { | ||
const words = ["MMMM", "WWW"]; | ||
const correct = { word: "WWW", score: scoreWord("WWW") }; | ||
const correct = { word: "WWW", score: Adagrams.scoreWord("WWW") }; | ||
expectTie(words); | ||
|
||
expect(highestScoreFrom(words)).toEqual(correct); | ||
expect(highestScoreFrom(words.reverse())).toEqual(correct); | ||
expect(Adagrams.highestScoreFrom(words)).toEqual(correct); | ||
expect(Adagrams.highestScoreFrom(words.reverse())).toEqual(correct); | ||
}); | ||
|
||
it("selects the first word when both have same length", () => { | ||
const words = ["AAAAAAAAAA", "EEEEEEEEEE"]; | ||
const first = { | ||
word: "AAAAAAAAAA", | ||
score: scoreWord("AAAAAAAAAA"), | ||
score: Adagrams.scoreWord("AAAAAAAAAA"), | ||
}; | ||
const second = { | ||
word: "EEEEEEEEEE", | ||
score: scoreWord("EEEEEEEEEE"), | ||
score: Adagrams.scoreWord("EEEEEEEEEE"), | ||
}; | ||
expectTie(words); | ||
|
||
expect(highestScoreFrom(words)).toEqual(first); | ||
expect(highestScoreFrom(words.reverse())).toEqual(second); | ||
expect(Adagrams.highestScoreFrom(words)).toEqual(first); | ||
expect(Adagrams.highestScoreFrom(words.reverse())).toEqual(second); | ||
}); | ||
}); | ||
}); | ||
|
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.
Excellent!