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

Water (Richelle & Mackenzie) Adagrams #23

Open
wants to merge 3 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
3 changes: 3 additions & 0 deletions .floo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"url": "https://floobits.com/r_spiel/adagrams "
}
6 changes: 6 additions & 0 deletions .flooignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extern
node_modules
tmp
vendor
.idea/workspace.xml
.idea/misc.xml
136 changes: 136 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#wave 1
def draw_letters
letters_pool = []

letter_quantity = {"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": 2, "U": 4, "V": 2, "W": 2, "X": 1, "Y": 2, "Z": 1}

letter_quantity.each do |letter, qty|
qty.times do
letters_pool << letter.to_s
end
end

#p letters_pool
drawn_letters = letters_pool.sample(10)
return drawn_letters
end

##################################################
#Wave 2
def uses_available_letters?(input, letters_in_hand)
valid_word = nil
input_as_array = input.upcase.split("")

Choose a reason for hiding this comment

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

You can make this a little clearer with chars:

Suggested change
input_as_array = input.upcase.split("")
input_as_array = input.upcase.chars

hand = letters_in_hand.dup

Choose a reason for hiding this comment

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

Great job using dup here!

It's always good to make copies of your input if you are modifying it* and is really easy to forget!

*Unless the point of the method is to modify the input.

letters_boolean = []

input_as_array.each do |letter|
if hand.include?(letter)
index = hand.index(letter)
hand.delete_at(index)
letters_boolean << true
else
letters_boolean << false
end
end

if letters_boolean.include?(false)
valid_word = false
else
valid_word = true
end

return valid_word
Comment on lines +24 to +42

Choose a reason for hiding this comment

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

You can simplify this by returning early:

Suggested change
letters_boolean = []
input_as_array.each do |letter|
if hand.include?(letter)
index = hand.index(letter)
hand.delete_at(index)
letters_boolean << true
else
letters_boolean << false
end
end
if letters_boolean.include?(false)
valid_word = false
else
valid_word = true
end
return valid_word
input_as_array.each do |letter|
if hand.include?(letter)
index = hand.index(letter)
hand.delete_at(index)
else
return false
end
end
return true

This works because as soon as you find a missing letter you know that the word is invalid.

This is a really useful pattern for solving these kinds of problems in a small amount of code.

end
##################################################
#wave 3
def score_word(word)
score = 0
word_array = word.upcase.split("")

word_array.each do |letter|
case letter
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
end

if word_array.length >= 7 && word_array.length <=10
score += 8
end

return score
end
################################################
#wave 4
def highest_score_from(words)
array_of_words = []

words.each do |word|
array_of_words << {
word: word,
score: score_word(word),
length: word.length
}
end

highest_scoring_word = array_of_words.max_by{ |word| word[:score].to_i}

Choose a reason for hiding this comment

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

to_i is redundant here since score_word already returns an Integer.

Suggested change
highest_scoring_word = array_of_words.max_by{ |word| word[:score].to_i}
highest_scoring_word = array_of_words.max_by{ |word| word[:score] }


ties = []
ties = array_of_words.select {|word| word[:score] == highest_scoring_word[:score]} #an array of hashes of the ties
Comment on lines +90 to +91

Choose a reason for hiding this comment

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

You left in an extra initialization of ties but great use of select!

Suggested change
ties = []
ties = array_of_words.select {|word| word[:score] == highest_scoring_word[:score]} #an array of hashes of the ties
ties = array_of_words.select {|word| word[:score] == highest_scoring_word[:score]} #an array of hashes of the ties


winner = []

ties.each do |word_hash|
if word_hash[:length] == 10
winner << word_hash
end
end

if winner.empty?
winner << ties.min_by{ |word_hash| word_hash[:length].to_i }
end

winning_word = {}

winning_word[:word] = winner[0][:word]
winning_word[:score] = winner[0][:score]

return winning_word
Comment on lines +93 to +110

Choose a reason for hiding this comment

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

You can simplify this using find (also, generally you can leave types off of variable names):

Suggested change
winner = []
ties.each do |word_hash|
if word_hash[:length] == 10
winner << word_hash
end
end
if winner.empty?
winner << ties.min_by{ |word_hash| word_hash[:length].to_i }
end
winning_word = {}
winning_word[:word] = winner[0][:word]
winning_word[:score] = winner[0][:score]
return winning_word
return ties.find { |word| word[:length] == 10 } || ties.min_by { |word| word[:length] }

The || here works because find returns nil if nothing is found and nil is falsey.

end

#############################################
#wave 2
# attempt with simpler version 3pm
# def uses_available_letters?(user_word, letters_in_hand)
#
# user_word_array = user_word.split("")
# hand = letters_in_hand.dup
#
# valid_word = nil
#
# user_word_array.each do |letter|
# if hand.include?(letter)
# valid_word = true
# index = hand.index(letter) #returns the index # from hand for that letter
# hand.delete_at(index) #deleted only at the one index so that it would not delete all instances of the letter
#
# else
# valid_word = false
# end
#
# end
#
# return valid_word #returns true or false
# end