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
Binary file added .DS_Store
Binary file not shown.
95 changes: 95 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
require "pry"

# WAVE 1
LETTER_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.

Good use of a constant here. This data structure makes it very easy to tell how many there are of each letter.

def draw_letters
letter_draw = LETTER_POOL.map do |key, value|
(key.to_s * value).split("")
end
letter_draw.flatten!

Choose a reason for hiding this comment

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

Could you use more descriptive variable names here than key and value?


user_hand = letter_draw.sample(10)
return user_hand
end

# WAVE 2
def uses_available_letters?(input, letters_in_hand)
uses_available_letters = true
input.each_char do |letter|
user_count = input.count(letter)
hand_count = letters_in_hand.count(letter)
if user_count > hand_count
uses_available_letters = false
break

Choose a reason for hiding this comment

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

Rather than keeping this extra variable uses_available_letters here, you could return false here, and return true at the bottom.

end
end
return uses_available_letters
end

# WAVE 3
SCORE_CHART = {1 => ["a", "e", "i", "o", "u", "l", "n", "r", "s", "t"],
2 => ["d", "g"],
3 => ["b", "c", "m", "p"],
4 => ["f", "h", "v", "w", "y"],

Choose a reason for hiding this comment

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

I like the idea of using a hash for this, but I think I would switch the keys and the values. I would go with something like:

LETTER_SCORES = {
  "A" => 1
  "B" => 3,
  "C" => 3,
  "D" => 2,
  # ...
}

Then to get the score for a letter, you can say LETTER_SCORES[letter].

5 => ["k"],
8 => ["j", "x"],
10 => ["q", "z"]}

def score_word(word)
total_score = 0

word.each_char do |char|
SCORE_CHART.each do |score, letters|
if letters.include?(char.downcase)
total_score += score
end
end
end

if word.length > 6
total_score += 8
end
return total_score
end

def tie_breaker(word, current_best, list)
new_best = current_best
if word.length == current_best.length
if list.index("#{word}") < list.index("#{current_best}")

Choose a reason for hiding this comment

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

I love that you broke this complex logic out into a separate method. Good organization!

new_best = word
end
elsif word.length == 10
new_best = word
elsif word.length < current_best.length
new_best = word
end
return new_best
end

def highest_score_from(words)
all_scores = {}
words.each do |word|
score = score_word(word)
all_scores[word] = score
end

best_word = ""
best_score = 0
all_scores.each do |word, score|
if word.length == 10
best_word = word
best_score = score
break

Choose a reason for hiding this comment

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

This will do the wrong thing if there's a short word that scores higher than a 10-letter word. For example:

  pry -r ./lib/adagrams.rb
[1] pry(main)> word_a = 'ZZZZZZZZZ'
=> "ZZZZZZZZZ"
[2] pry(main)> word_b = 'AAAAAAAAAA'
=> "AAAAAAAAAA"
[3] pry(main)> score_word(word_a)
=> 98
[4] pry(main)> score_word(word_b)
=> 18
[5] pry(main)> highest_score_from([word_a, word_b])
=> {:word=>"AAAAAAAAAA", :score=>18}

Paying attention to word length 10 only matters for breaking ties.

elsif score > best_score
best_word = word
best_score = score
elsif score == best_score
best_word = tie_breaker(word, best_word, words)
end
end

winner = {word: best_word, score: best_score}
return winner
end