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

Earth - Ting-Yi & Ringo #11

Open
wants to merge 6 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
78 changes: 78 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require 'csv'

def draw_letters
letter_pool = %W( #{ "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} ).join.split("")

Choose a reason for hiding this comment

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

there's a lot going on to make this work, and I wonder if all the steps are necessary? the join and thensplit is particularly suspect.

random_letter = letter_pool.shuffle.take(10)

Choose a reason for hiding this comment

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

you can do this in one line with the .sample method!

return random_letter
end

def uses_available_letters?(input, letters_in_hand)
letters_in_hand_copy = letters_in_hand.clone
input.upcase.each_char do |letter|
if letters_in_hand_copy.include?(letter)
letters_in_hand_copy.delete_at(letters_in_hand_copy.index(letter))
else
return false
end
end
return true
end

def score_word(word)
score = 0
word.upcase.each_char do |character|
case character
when "A", "E", "I", "O", "U", "L", "N", "R", "S", "T"
score += 1
when "D", "G"
score += 2
when "B", "C", "M", "P"
score += 3
when "F", "H", "V", "W", "Y"
score += 4
when "K"
score += 5
when "J", "X"
score += 8
when "Q", "Z"
score += 10
end
Comment on lines +25 to +39

Choose a reason for hiding this comment

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

You can update the score value of each letter more reliably by using a hash here. :)

end

if word.length >= 7
score += 8
end

return score
end

def highest_score_from(words)
winner = {
word: "",
score: 0
}

high_score = words.max_by { |word| score_word(word) }
high_words = words.select { |word| score_word(word) == score_word(high_score) }

if high_words.length == 1
winner[:word] = high_words[0]
winner[:score] = score_word(high_words[0])
elsif high_words.any? { |word| word.length == 10 }
ten_letter_words = high_words.select { |word| word if word.length == 10 }
winner[:word] = ten_letter_words[0]
winner[:score] = score_word(ten_letter_words[0])
else
shortest_word_length = (high_words.min_by { |word| word.length }).length
short_words_array = high_words.select { |word| word.length == shortest_word_length }
winner[:word] = short_words_array[0]
winner[:score] = score_word(short_words_array[0])
end

return winner
end

def is_in_english_dict?(input)
dictionary = CSV.read('../adagrams/assets/dictionary-english.csv').flatten
return dictionary.include?(input.downcase)
end
24 changes: 24 additions & 0 deletions test/adagrams_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,28 @@
expect(best_word[:score]).must_equal 18
end
end

describe "is_in_english_dict?" do
it "finds real word in dictionary" do
good_word = "bamboo"
expect(is_in_english_dict?(good_word)).must_equal true
end

it "doesn't find fake word in dictionary" do
bad_word = "zybje"
expect(is_in_english_dict?(bad_word)).wont_equal true

empty_word = ""
expect(is_in_english_dict?(empty_word)).wont_equal true

too_long_word = "incongruous"
expect(is_in_english_dict?(too_long_word)).wont_equal true
end
Comment on lines +183 to +197

Choose a reason for hiding this comment

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

currently these tests aren't passing

end

end