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

Sophie/Emily's Adagram Submission #22

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

def draw_letters
distribution = {
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,
}

distribution_array = []
distribution.each do |letter, number|
number.times do
distribution_array << letter.to_s
end
end

drawn_letters = []
10.times do
index = rand(0...distribution_array.length)
drawn_letters << distribution_array[index]
distribution_array.delete_at(index)
end
return drawn_letters
end

def uses_available_letters?(input, letters_in_hand)
input_hash = Hash.new(0)
input.upcase!
input = input.split('')
input.each do |letter|
input_hash[letter] += 1
end
input = input_hash

letter_hash = Hash.new(0)
letters_in_hand.each do |letter|
letter_hash[letter] += 1
end
letters_in_hand = letter_hash

input.each do |letter, count|
if letters_in_hand[letter] < count
return false
end
end

return true
end

def score_word(word)
if word.length >= 7
score = 8
else
score = 0
end

hash = Hash.new(0)
word.upcase!
word = word.split('')
word.each do |letter|
hash[letter] += 1
end
word = hash

values = {
"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
Comment on lines +89 to +95

Choose a reason for hiding this comment

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

Interesting that y'all chose two different hash styles in this assignment. IMO this kind of inconsistency can create readability problems, so keep an eye out for that!

}

values.each do |letter, value|
score += (word[letter] * values[letter])
end

return score
end

def highest_score_from(words)

best_word = Hash.new

best_score = 0
winning_word = ""

words.each do |word|
score = score_word(word)
if score > best_score
winning_word = word
best_score = score
elsif word.length == 10 && score == best_score && winning_word.length < 10
winning_word = word
best_score = score
elsif score == best_score && word.length < winning_word.length && winning_word.length != 10
winning_word = word
best_score = score
end
end

best_word[:word] = winning_word
best_word[:score] = best_score

return best_word
end

def is_in_english_dict?(input)
english_dict = CSV.read("assets/dictionary-english.csv").flatten
if english_dict.include?(input)
return true
else
return false
end
end
34 changes: 34 additions & 0 deletions test/adagrams_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,38 @@
expect(best_word[:score]).must_equal 18
end
end

describe 'is_in_english_dict?' do
it 'returns true for words less than or equal to 10 letters in the english dictionary' do

input = "chickens"
result = is_in_english_dict?(input)
expect(result).must_equal true

input = "dog"
result = is_in_english_dict?(input)
expect(result).must_equal true

end

it 'returns false for words not in the english dictionary' do

input = "sdfjs"
result = is_in_english_dict?(input)
expect(result).must_equal false

input = "appple"

Choose a reason for hiding this comment

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

you never eaten an appple before? \j

result = is_in_english_dict?(input)
expect(result).must_equal false

end

it 'returns false for valid words in the dictionary over 10 letters' do

input = "responsibility"
result = is_in_english_dict?(input)
expect(result).must_equal false

end
end
end