-
Notifications
You must be signed in to change notification settings - Fork 27
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
Karis/Melissa - Edges - Adagrams #18
base: master
Are you sure you want to change the base?
Conversation
…tters in hand are included in the word
…rray to iterate through them and added upcase
git log git status git log
AdagramsWhat We're Looking For
|
def draw_letters() | ||
letter_bag = ["A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "C", "C", "D", "D", "D", "D", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "F", "F", "G", "G", "G", "H", "H", "I", "I", "I", "I", "I", "I", "I", "I", "I", "J", "K", "L", "L", "L", "L", "M", "M", "N", "N", "N", "N", "N", "N", "O", "O", "O", "O", "O", "O", "O", "O", "P", "P", "Q", "R", "R", "R", "R", "R", "R", "S", "S", "S", "S", "T", "T", "T", "T", "T", "T", "U", "U", "U", "U", "V", "V", "W", "W", "X", "Y", "Y", "Z"] | ||
|
||
# randomly selects the letters |
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.
This data structure makes sense for storing the pool of letters. However, laid out on one line like this it's not easy to see the distribution of letters. Something like this might be a bit easier to read:
letters = [
"A", "A", "A", "A", "A", "A", "A", "A", "A",
"B", "B",
"C", "C",
...
]
Or, if you were feeling fancy:
letter_distribution = {
A: 9,
B: 2,
C: 2,
...
}
letters = []
letter_distribution.each do |letter, count|
count.times do
letters << letter
end
end
def uses_available_letters?(input, letters_in_hand) | ||
|
||
# takes word and puts it into an array | ||
adagram = input.split(//) |
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.
There are two problems with this implementation:
-
You will never return
true
for a word with a repeated letter. For example,hand = ["E", "F", "R", "A", "E", "L", "V", "A", "D", "K"] uses_available_letters?("ADA", hand) # => false
The reason why is that Ruby's
Array#delete
method will remove all matching elements from an array, not just the first match. -
This method is destructive! Because you call
delete
on the input, the hand will be reduced by the letters in the input. For example:hand = draw_letters puts hand # => ["T", "T", "E", "F", "R", "A", "E", "L", "V", "A"] uses_available_letters?('tte', hand) puts hand # => ["E", "F", "R", "A", "E", "L", "V", "A"]
This is bad because it's unexpected behavior. Nothing about "check whether this word is made up of letters in this hand" suggests that the hand will be changed in the process, but that's exactly what happens.
The way to address this problem would be to make a copy of the hand before removing letters from it.
I will acknowledge that none of the tests checked for either of these behaviors, but that doesn't change the fact that they're bugs.
score_chart = | ||
{ | ||
"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 | ||
} |
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.
Again, this data structure feels like a good fit here, but I think it would be more readable if you were to split it across multiple lines.
# highest score | ||
words_with_scores.each do |word, score| | ||
if score == words_with_scores.values.max | ||
best_word = Hash.new |
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.
This works! However!
On line 77, you're calling values.max
inside an each loop. That means you're doing the work of finding the max value (O(n)
) once for each word (n
times), for a total time complexity of O(n^2)
. We can improve this to O(n)
by moving the max
outside the loop:
max_score = words_with_scores.values.max
words_with_scores.each do |word, score|
if score == max_score
# ...
@@ -0,0 +1,117 @@ | |||
# chooses 10 letters randomly from the letter bag | |||
def draw_letters() |
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.
You have included the same file twice, as adagrams.rb
and lib/adagrams.rb
. Probably only the lib
one should be included here.
Adagrams
Congratulations! You're submitting your assignment.
Comprehension Questions
Enumerable
mixin? If so, where and why was it helpful?