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

Submit Adagrams code #9

Open
wants to merge 5 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
.env
.idea
.test.rb
86 changes: 86 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
require 'csv'

def draw_letters()
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

hand = []

letter_dist = {
"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
}

letter_pool = []

letter_dist.each do |letter, times|
letter_pool << Array.new(times) { letter }
end

letter_pool = letter_pool.flatten

until hand.length == 10 do
index = rand(letter_pool.length)

hand << letter_pool[index]
letter_pool.delete_at(index)
end
return hand
end

def uses_available_letters?(input, letters_in_hand)
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

copy_lih = letters_in_hand.dup
input.each_char do |letter|
if copy_lih.include?(letter)
copy_lih.delete_at(copy_lih.find_index(letter))
else
return false
end
end
return true
end

def score_word(word)
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 I love the use of a hash here.

score_chart = {
"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
}

score = 0

word.upcase.each_char do |letter|
score += score_chart[letter]
end

score += 8 if word.length >= 7

return score
end

def highest_score_from(words)
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 Well done

max_score = 0
highest_word = ""

words.each do |word|
if score_word(word) > max_score
max_score = score_word(word)
highest_word = word
elsif score_word(word) == max_score && word.length == highest_word.length
next
elsif score_word(word) == max_score && word.length == 10
max_score = score_word(word)
highest_word = word
elsif score_word(word) == max_score && word.length < highest_word.length && highest_word.length != 10
max_score = score_word(word)
highest_word = word
end
end
score_hash = { word: highest_word, score: max_score}
return score_hash
end

def is_in_english_dict?(input)
dict = CSV.read('assets/dictionary-english.csv')
dict.include?([input.downcase]) ? true : false
end
Comment on lines +83 to +86
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 Very compact and readable.

15 changes: 15 additions & 0 deletions test/adagrams_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,19 @@
expect(best_word[:score]).must_equal 18
end
end

describe 'is_in_english_dict?' do
it 'checks an input string against a list of valid English words' do

word1 = "APPLE"
word2 = "pencil"
word3 = "pppppppppp"
word4 = "PROGRAMMING"

expect(is_in_english_dict?(word1)).must_equal true
expect(is_in_english_dict?(word2)).must_equal true
expect(is_in_english_dict?(word3)).must_equal false
expect(is_in_english_dict?(word4)).must_equal false
end
end
end