Skip to content

Commit 638fceb

Browse files
committed
Refactor the code
1 parent e474941 commit 638fceb

2 files changed

Lines changed: 16 additions & 77 deletions

File tree

Training/Medium/scrabble.rb

Lines changed: 16 additions & 20 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
@@ -45,11 +44,8 @@ def calculate_points(word)
4544
end
4645
points
4746
end
48-
49-
def start
50-
puts find_all_words
51-
end
5247
end
5348

5449
obj = Scrabble.new
55-
obj.start
50+
answer = obj.find_all_words
51+
puts answer

Training/Medium/spoon.rb

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)