Edges - Goeun Maddie - Adagrams#9
Conversation
… one big array full of letters available
…nd ten letter words, min words and check if any of the words have 10 letters
…d_all_scores and find_max_words
AdagramsWhat We're Looking For
|
| POOL = { 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, Y: 2, Z: 1 } | ||
|
|
||
| SCORE_CHART = { A: 1, B: 3, C: 3, D: 2, E: 1, F: 4, G: 2, H: 4, I: 1, J: 8, K: 5, L: 1, M: 3, N: 1, O: 1, P: 3, Q: 10, R: 1, S: 1, T: 1, U: 1, V: 4, W: 4, X: 8, Y: 4, Z: 10 } | ||
|
|
There was a problem hiding this comment.
I like that you have these as constants. They also both feel like good choices of data-structure for this problem. One change I might recommend is to break the letters out onto separate lines.
| def uses_available_letters?(input, letters_in_hand) | ||
| input = input.chars | ||
| (input & letters_in_hand) == input ? true : false | ||
| end |
There was a problem hiding this comment.
Using the intersection here is a very clever way to solve this problem - good work!
|
|
||
| word.each_char do |char| | ||
| total_points << SCORE_CHART.fetch(char.upcase.to_sym) | ||
| end |
There was a problem hiding this comment.
I'm curious why you chose to use fetch here instead of SCORE_CHART[char.upcase.to_sym]
|
|
||
| def find_max_words(all_scores) | ||
| all_scores.select { |word, score| score == all_scores.values.max } | ||
| end |
There was a problem hiding this comment.
This works! However!
You call all_scores.values.max in the inside of the select loop, which means you're re-doing this work for each score. That results in an O(n^2) time complexity. You can improve on this as follows:
def find_max_words(all_scores)
max_score = all_scores.values.max
all_scores.select { |word, score| score == max_score }
endThis yields an O(n) time complexity.
There was a problem hiding this comment.
Also, you have this method implemented twice.
| all_scores = {} | ||
|
|
||
| unscored_words.each do |word| | ||
| all_scores[word] = score_word(word) |
There was a problem hiding this comment.
What happens if there's a duplicate word?
| def find_first_ten_word(max_words) | ||
| first_ten_word = max_words.select { |word, score| word.length == 10 }.keys[0] | ||
| return first_ten_word | ||
| end |
There was a problem hiding this comment.
Your implementations of find_first_min_word and find_first_ten_word both rely on hash ordering. What I mean by that is, they only work because Ruby keeps track of the order in which key-value pairs are added to a hash.
However, you should not generally rely on hash ordering - it's a bad habit that will get you into trouble once we start JavaScript.
| def highest_score_from(unscored_words) | ||
| all_scores = find_all_scores(unscored_words) | ||
|
|
||
| max_scored_words = find_max_words(all_scores) |
There was a problem hiding this comment.
I like the way you've broken this complex method out using helper methods - that makes it much easier to read!
Adagrams
Congratulations! You're submitting your assignment.
Comprehension Questions
Enumerablemixin? If so, where and why was it helpful?.any,.include?, and.min_by. It was helpful because we didn’t have to create code iterating through the same data repeatedly to extract the data needed. The enumerables were able to do the work in shortened form.binding.prya lot!