Conversation
…ing tests to spec file
AdagramsWhat We're Looking For
|
| } | ||
|
|
||
| avail_letters = array_gen(letter_freq) | ||
| used_letters = avail_letters.sample(10) |
There was a problem hiding this comment.
This is lovely! I can see everything I need to see about how this works, and if I get confused about array_gen it's close at hand!
| end | ||
| if input.length > letters_in_hand.length | ||
| return false | ||
| else |
There was a problem hiding this comment.
Small semantic note: If you don't return above or raise, then you won't hit the code in this else anyway. In cases like this, it's nice to leave the meat of the method outside of an else block.
| possible_letters = letters_in_hand.clone | ||
| input.upcase.split(//).each do |char| | ||
| if possible_letters.include?(char) | ||
| possible_letters.delete(char) |
There was a problem hiding this comment.
You will never return true for a word with a repeated letter. For example,
ruby 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.
There was a problem hiding this comment.
Good to know!
If we were to write something like:
i = index (char)
possible_letters.delete_at(i)
would it preserve the other repeats of the letter?
And is there a more elegant way of doing it?
| if possible_letters.include?(char) | ||
| possible_letters.delete(char) | ||
| else | ||
| return false |
There was a problem hiding this comment.
Nice work returning when you have the answer!
Adagrams
Congratulations! You're submitting your assignment.
Comprehension Questions
Enumerablemixin? If so, where and why was it helpful?