-
Notifications
You must be signed in to change notification settings - Fork 29
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
Sockets - Erica & Karla G. #7
base: master
Are you sure you want to change the base?
Conversation
…and subtracts 1 from that letter's count
…. Removed method parameter
…input word is in dictionary csv
AdagramsWhat We're Looking For
Good job overall. There are a few places where things could be cleaned up, which I've tried to call out below, but in general I am happy with what I see here. It's clear to me that the learning goals around working with methods and tests were met. Keep up the hard work! |
def draw_letters | ||
available_letters = {"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, |
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.
I love this data structure - it makes it very clear how many there are of each letter. Could you store it in a constant?
10.times do | ||
letter = available_letters.keys.sample | ||
while available_letters[letter] == 0 | ||
letter = available_letters.keys.sample |
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.
By this logic, you're as likely to draw an e
as you are a z
!
Instead, you might create an array containing all the letters, with code similar to this:
letter_counts = {
# the hash you had before
}
letters = []
letter_counts.each do |letter, count|
count.times do
letters << letter
end
end
Then you could pull a random tile with letters.sample
and get a nice even distribution.
class Array | ||
def delete_elements_in_(array) | ||
array.each do |x| | ||
if index = index(x) |
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.
Be careful about this sort of monkey patching - it's typically considered bad form. Instead, you could write a helper method that's not part of the Array
class:
def delete_from(hay, needles)
needles.each do |needle|
if index = hay.index(needle)
hay.delete_at(index)
end
end
end
input.each_char do |letter| | ||
if letters_in_hand.include?(letter) == true | ||
letters_in_hand.delete_elements_in_([letter]) | ||
used_letters << letter |
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.
Since you only use delete_elements_in_
with an array of length 1, it might make sense to make the argument be a single object instead of an array.
Please make sure you fully understand the code you borrow from other sources before including it in your project.
when "a", "e", "i", "o", "u", "l", "n", "r", "s", "t" | ||
score += 1 | ||
when "d", "g" | ||
score += 2 |
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.
While this case
statement works, it means that the information about which letter has which score is locked into this piece of code, and can't easily be used elsewhere. For example, if you wanted to display the value of each letter in a hand, you would need to repeat this work.
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 LETTER_SCORES[letter]
.
Adagrams
Congratulations! You're submitting your assignment.
Comprehension Questions
Enumerable
mixin? If so, where and why was it helpful?