Skip to content
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

Open
wants to merge 7 commits into
base: master
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
99 changes: 99 additions & 0 deletions lib/adagrams.rb
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

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!

POOL = pool

# WAVE#1
def draw_letters
letters_in_hand = POOL
return letters_in_hand.sample(10)

Choose a reason for hiding this comment

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

this is ostensibly the same as doing:

  return POOL.sample(10)

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"

Choose a reason for hiding this comment

The 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 return false right the heck here!

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

Choose a reason for hiding this comment

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

All these || in here makes it hard to read. You can do this with two comparisons, what are they?

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
7 changes: 6 additions & 1 deletion test/adagrams_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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']
Expand Down Expand Up @@ -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

Choose a reason for hiding this comment

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

please please please! Take all the binding.pry's out of your code before submission!

best_word = highest_score_from words

expect(best_word[:word]).must_equal 'AAAAAAAAAA'
Expand Down