-
Notifications
You must be signed in to change notification settings - Fork 33
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
Leaves - Yitgop & Bri #19
base: master
Are you sure you want to change the base?
Changes from all commits
65490d8
dba268b
b6add8c
94bf521
4ad9ef5
b25ac82
988c507
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# This is our data structure | ||
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 | ||
POOL = pool | ||
|
||
# WAVE#1 | ||
def draw_letters | ||
letters_in_hand = POOL | ||
return letters_in_hand.sample(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. this is ostensibly the same as doing:
|
||
end | ||
|
||
# WAVE#2: | ||
def uses_available_letters?(word, letters_in_hand) | ||
hand_hash = letters_in_hand.uniq.map { |x| [x, letters_in_hand.count(x)] }.to_h | ||
word_array = word.upcase.split("") | ||
letter_check = [] | ||
|
||
word_array.map do |letter| | ||
if hand_hash[letter].class == Integer | ||
if hand_hash[letter] >= 1 | ||
hand_hash[letter] -= 1 | ||
letter_check << "True" | ||
elsif hand_hash[letter] == 0 | ||
letter_check << "False" | ||
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. As soon as you fail to find one of the letters, you can bail! just |
||
end | ||
else | ||
letter_check << "False" | ||
end | ||
end | ||
return letter_check.include?("False") ? false : true | ||
end | ||
|
||
# WAVE#3 | ||
def score_word(word) | ||
score_chart = { | ||
"A" => 1, | ||
"E" => 1, | ||
"I" => 1, | ||
"O" => 1, | ||
"U" => 1, | ||
"L" => 1, | ||
"N" => 1, | ||
"R" => 1, | ||
"S" => 1, | ||
"T" => 1, | ||
"D" => 2, | ||
"G" => 2, | ||
"B" => 3, | ||
"C" => 3, | ||
"M" => 3, | ||
"P" => 3, | ||
"F" => 4, | ||
"H" => 4, | ||
"V" => 4, | ||
"W" => 4, | ||
"Y" => 4, | ||
"K" => 5, | ||
"J" => 8, | ||
"X" => 8, | ||
"Q" => 10, | ||
"Z" => 10 | ||
} | ||
score = 0 | ||
word_array = word.upcase.split("") | ||
print word_array | ||
|
||
word_array.map do |letter| | ||
score += score_chart[letter] | ||
end | ||
|
||
if word_array.length == 7 || word_array.length == 8 || word_array.length == 9 || word_array.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. All these if word_array.length >= 7 && word_array.length <= 10
|
||
score += 8 | ||
end | ||
|
||
return score | ||
end | ||
|
||
# WAVE#4 | ||
def highest_score_from(words) | ||
scores = words.map do |word| | ||
score_word(word) | ||
end | ||
highest_score = scores.max | ||
p scores | ||
|
||
winner = nil | ||
words_with_scores = words.zip(scores) | ||
winning_words_with_scores = words_with_scores.select {| word, score | score == highest_score } | ||
winning_words = winning_words_with_scores.map {| word, score| word } | ||
|
||
winning_words.each do |word| | ||
if word.length == 10 | ||
winner = word | ||
return {word: winner, score: highest_score} | ||
else | ||
winner = winning_words.min {|x,y| x.size <=> y.size} | ||
end | ||
end | ||
return {word: winner, score: highest_score} | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
# Get that nice colorized output | ||
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new | ||
|
||
# wave 1 | ||
describe 'Adagrams' do | ||
describe 'draw_letters method' do | ||
it 'draws ten letters from the letter pool' do | ||
|
@@ -26,6 +27,7 @@ | |
end | ||
end | ||
|
||
# wave 2 | ||
describe 'uses_available_letters? method' do | ||
|
||
it 'returns true if the submitted letters are valid against the drawn letters' do | ||
|
@@ -67,6 +69,7 @@ | |
end | ||
end | ||
|
||
# wave 3 | ||
describe 'score_word method' do | ||
it 'returns an accurate numerical score according to the score chart' do | ||
expect(score_word("A")).must_equal 1 | ||
|
@@ -91,6 +94,7 @@ | |
end | ||
end | ||
|
||
# wave 4 | ||
describe 'highest_score_from method' do | ||
it 'returns a hash that contains the word and score of best word in an array' do | ||
words = ['X', 'XX', 'XXX', 'XXXX'] | ||
|
@@ -143,7 +147,8 @@ | |
# verify both have a score of 10 | ||
expect(score_word(words.first)).must_equal 18 | ||
expect(score_word(words.last)).must_equal 18 | ||
|
||
require "pry" | ||
binding.pry | ||
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. please please please! Take all the |
||
best_word = highest_score_from words | ||
|
||
expect(best_word[:word]).must_equal 'AAAAAAAAAA' | ||
|
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.
this is the first time I've seen this done in this way, and it's ingenious!