-
Notifications
You must be signed in to change notification settings - Fork 29
Sockets: Jansen & Kate #22
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
base: master
Are you sure you want to change the base?
Changes from all commits
3d47c34
66d0730
ec94b46
aabfa2c
c71d9b2
70d761d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| require "pry" | ||
|
|
||
| # WAVE 1 | ||
| LETTER_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} | ||
|
|
||
| def draw_letters | ||
| letter_draw = LETTER_POOL.map do |key, value| | ||
| (key.to_s * value).split("") | ||
| end | ||
| letter_draw.flatten! | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you use more descriptive variable names here than |
||
|
|
||
| user_hand = letter_draw.sample(10) | ||
| return user_hand | ||
| end | ||
|
|
||
| # WAVE 2 | ||
| def uses_available_letters?(input, letters_in_hand) | ||
| uses_available_letters = true | ||
| input.each_char do |letter| | ||
| user_count = input.count(letter) | ||
| hand_count = letters_in_hand.count(letter) | ||
| if user_count > hand_count | ||
| uses_available_letters = false | ||
| break | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than keeping this extra variable |
||
| end | ||
| end | ||
| return uses_available_letters | ||
| end | ||
|
|
||
| # WAVE 3 | ||
| SCORE_CHART = {1 => ["a", "e", "i", "o", "u", "l", "n", "r", "s", "t"], | ||
| 2 => ["d", "g"], | ||
| 3 => ["b", "c", "m", "p"], | ||
| 4 => ["f", "h", "v", "w", "y"], | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the idea of using a hash for this, but I think I would switch the keys and the values. I would go with something like: LETTER_SCORES = {
"A" => 1
"B" => 3,
"C" => 3,
"D" => 2,
# ...
}Then to get the score for a letter, you can say |
||
| 5 => ["k"], | ||
| 8 => ["j", "x"], | ||
| 10 => ["q", "z"]} | ||
|
|
||
| def score_word(word) | ||
| total_score = 0 | ||
|
|
||
| word.each_char do |char| | ||
| SCORE_CHART.each do |score, letters| | ||
| if letters.include?(char.downcase) | ||
| total_score += score | ||
| end | ||
| end | ||
| end | ||
|
|
||
| if word.length > 6 | ||
| total_score += 8 | ||
| end | ||
| return total_score | ||
| end | ||
|
|
||
| def tie_breaker(word, current_best, list) | ||
| new_best = current_best | ||
| if word.length == current_best.length | ||
| if list.index("#{word}") < list.index("#{current_best}") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I love that you broke this complex logic out into a separate method. Good organization! |
||
| new_best = word | ||
| end | ||
| elsif word.length == 10 | ||
| new_best = word | ||
| elsif word.length < current_best.length | ||
| new_best = word | ||
| end | ||
| return new_best | ||
| end | ||
|
|
||
| def highest_score_from(words) | ||
| all_scores = {} | ||
| words.each do |word| | ||
| score = score_word(word) | ||
| all_scores[word] = score | ||
| end | ||
|
|
||
| best_word = "" | ||
| best_score = 0 | ||
| all_scores.each do |word, score| | ||
| if word.length == 10 | ||
| best_word = word | ||
| best_score = score | ||
| break | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will do the wrong thing if there's a short word that scores higher than a 10-letter word. For example: ➜ pry -r ./lib/adagrams.rb
[1] pry(main)> word_a = 'ZZZZZZZZZ'
=> "ZZZZZZZZZ"
[2] pry(main)> word_b = 'AAAAAAAAAA'
=> "AAAAAAAAAA"
[3] pry(main)> score_word(word_a)
=> 98
[4] pry(main)> score_word(word_b)
=> 18
[5] pry(main)> highest_score_from([word_a, word_b])
=> {:word=>"AAAAAAAAAA", :score=>18}Paying attention to word length 10 only matters for breaking ties. |
||
| elsif score > best_score | ||
| best_word = word | ||
| best_score = score | ||
| elsif score == best_score | ||
| best_word = tie_breaker(word, best_word, words) | ||
| end | ||
| end | ||
|
|
||
| winner = {word: best_word, score: best_score} | ||
| return winner | ||
| end | ||
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.
Good use of a constant here. This data structure makes it very easy to tell how many there are of each letter.