Skip to content

Commit bddf776

Browse files
committed
Refactor the code
1 parent e474941 commit bddf776

1 file changed

Lines changed: 14 additions & 15 deletions

File tree

Training/Medium/scrabble.rb

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
# Solution for https://www.codingame.com/ide/puzzle/scrabble
22
class Scrabble
3-
attr_accessor :words_count, :dictionary, :letters, :letters_weight
3+
attr_accessor :dictionary, :letters, :letters_weight
44

55
def initialize
6-
@n = gets.to_i
7-
@dictionary = @n.times.each_with_object([]) { |_, result| result << gets.chomp }
6+
words_number = gets.to_i
7+
@dictionary = Array.new(words_number) { gets.chomp }
88
@letters = gets.chomp
99
@letters_weight = init_letters_weight
1010
end
1111

1212
def init_letters_weight
13-
letters_weight = {}
14-
letters_weight[1] = %w(e a i o n r t l s u)
15-
letters_weight[2] = %w(d g)
16-
letters_weight[3] = %w(b c m p)
17-
letters_weight[4] = %w(f h v w y)
18-
letters_weight[5] = ['k']
19-
letters_weight[8] = %w(j x)
20-
letters_weight[10] = %w(q z)
21-
letters_weight
13+
{
14+
1 => %w(e a i o n r t l s u),
15+
2 => %w(d g),
16+
3 => %w(b c m p),
17+
4 => %w(f h v w y),
18+
5 => %w(k),
19+
8 => %w(j x),
20+
10 => %w(q z)
21+
}
2222
end
2323

2424
def matching?(word)
@@ -29,9 +29,8 @@ def matching?(word)
2929
end
3030

3131
def find_all_words
32-
words_with_points = {}
33-
dictionary.find_all do |word|
34-
words_with_points[word] = calculate_points(word) if matching?(word)
32+
words_with_points = dictionary.each_with_object({}) do |word, result|
33+
result[word] = calculate_points(word) if matching?(word)
3534
end
3635
words_with_points.key(words_with_points.values.max)
3736
end

0 commit comments

Comments
 (0)