-
Notifications
You must be signed in to change notification settings - Fork 33
Leaves - Katie and Dominique #18
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# Wave 1 | ||
def draw_letters | ||
raw_tiles = [ | ||
{letter: "A", weight: 9, score: 1}, | ||
{letter: "B", weight: 2, score: 3}, | ||
{letter: "C", weight: 2, score: 3}, | ||
{letter: "D", weight: 4, score: 2}, | ||
{letter: "E", weight: 12, score: 1}, | ||
{letter: "F", weight: 2, score: 4}, | ||
{letter: "G", weight: 3, score: 2}, | ||
{letter: "H", weight: 2, score: 4}, | ||
{letter: "I", weight: 9, score: 1}, | ||
{letter: "J", weight: 1, score: 8}, | ||
{letter: "K", weight: 1, score: 5}, | ||
{letter: "L", weight: 4, score: 1}, | ||
{letter: "M", weight: 2, score: 3}, | ||
{letter: "N", weight: 6, score: 1}, | ||
{letter: "O", weight: 8, score: 1}, | ||
{letter: "P", weight: 2, score: 3}, | ||
{letter: "Q", weight: 1, score: 10}, | ||
{letter: "R", weight: 6, score: 1}, | ||
{letter: "S", weight: 4, score: 1}, | ||
{letter: "T", weight: 6, score: 1}, | ||
{letter: "U", weight: 4, score: 1}, | ||
{letter: "V", weight: 2, score: 4}, | ||
{letter: "W", weight: 2, score: 4}, | ||
{letter: "X", weight: 1, score: 8}, | ||
{letter: "Y", weight: 2, score: 4}, | ||
{letter: "Z", weight: 1, score: 10}] | ||
|
||
tile_set = [] | ||
|
||
# extracts letter and score keys and puts them in array called raw_tiles | ||
raw_tiles.each do |a| | ||
a[:weight].times do | ||
tile_set.push(a[:letter]) | ||
end | ||
end | ||
|
||
# extracts 10 random letters and assigns them to the array named hand | ||
hand = tile_set.sample(10) | ||
return hand | ||
end | ||
|
||
#Official hand | ||
letters_in_hand = draw_letters | ||
|
||
#Wave 2 | ||
#Word is changed to array so we can see if the word is an anagram of the hand | ||
def uses_available_letters?(input, letters_in_hand) | ||
input = input.upcase.chars | ||
boolean_value = true | ||
input.each do |letter| | ||
if input.count(letter) > letters_in_hand.count(letter) | ||
boolean_value = false | ||
end | ||
end | ||
boolean_value | ||
end | ||
|
||
# Wave 3 | ||
# Return the score of the given word | ||
def score_word(word) | ||
word = word.upcase.chars | ||
sum = 0 | ||
total_sum = 0 | ||
|
||
score_hash = {1=> ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], 2=> ["D", "G"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An alternative approach would be to store the letter scores in a hash, something like this: LETTER_SCORES = {
"A" => 1
"B" => 3,
"C" => 3,
"D" => 2,
# ...
} Then to get the score for a letter, you can say You could create this hash from your original array of hashes Organizing the data this way would simplify the logic below. |
||
3=> ["B", "C", "M", "P"], 4=> ["F", "H", "V", "W", "Y"], 5=> ["K"], 8=> ["J", "X"], | ||
10=> ["Q", "Z"]} | ||
|
||
total_sum = 8 if word.length >= 7 | ||
word.each do |letter| | ||
score_hash.each do |key, value| | ||
value.each do |check| | ||
if check == letter | ||
sum = key.to_i | ||
total_sum += sum | ||
end | ||
end | ||
end | ||
end | ||
return total_sum | ||
end | ||
|
||
# Wave 4 | ||
# Return word and score of word with highest score | ||
def highest_score_from(words) | ||
high_score_hash = {word: "", score: 0} | ||
high_score = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We reworked the logic for this method and you don't end up using high_score and high_score_length. Rake provided this warning for you: /Users/becca/Documents/GitHub/c12/adagrams/lib/adagrams.rb:90: warning: assigned but unused variable - high_score You should remove these variables from your code. |
||
high_score_length = 0 | ||
words.each do |word| | ||
score = score_word(word) | ||
if score > high_score_hash[:score] | ||
high_score_hash[:score] = score_word(word) | ||
high_score_hash[:word] = word | ||
elsif score == high_score_hash[:score] | ||
winner_word = tiebreaker(high_score_hash[:word], word) | ||
high_score_hash[:score] = score | ||
high_score_hash[:word] = winner_word | ||
end | ||
end | ||
return high_score_hash | ||
end | ||
|
||
def tiebreaker(word1, word2) | ||
if word1.length == 10 | ||
return word1 | ||
elsif word2.length == 10 | ||
return word2 | ||
elsif word1.length < word2.length | ||
return word1 | ||
elsif word2.length < word1.length | ||
return word2 | ||
end | ||
return word1 | ||
end | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the sake of cleanliness, remove these extra lines. |
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clever use of the data structure you created. Consider also how you could use a data structure organized like this: {"A"=>9, "B"=>2,...}