diff --git a/ascii_art.rb b/Training/Easy/ascii_art.rb similarity index 100% rename from ascii_art.rb rename to Training/Easy/ascii_art.rb diff --git a/chuck_norris.rb b/Training/Easy/chuck_norris.rb similarity index 100% rename from chuck_norris.rb rename to Training/Easy/chuck_norris.rb diff --git a/defibrillators.rb b/Training/Easy/defibrillators.rb similarity index 100% rename from defibrillators.rb rename to Training/Easy/defibrillators.rb diff --git a/horse_racing.rb b/Training/Easy/horse_racing.rb similarity index 100% rename from horse_racing.rb rename to Training/Easy/horse_racing.rb diff --git a/mime_type.rb b/Training/Easy/mime_type.rb similarity index 100% rename from mime_type.rb rename to Training/Easy/mime_type.rb diff --git a/power_of_thor.rb b/Training/Easy/power_of_thor.rb similarity index 100% rename from power_of_thor.rb rename to Training/Easy/power_of_thor.rb diff --git a/temperatures.rb b/Training/Easy/temperatures.rb similarity index 100% rename from temperatures.rb rename to Training/Easy/temperatures.rb diff --git a/the_descent.rb b/Training/Easy/the_descent.rb similarity index 100% rename from the_descent.rb rename to Training/Easy/the_descent.rb diff --git a/Training/Medium/scrabble.rb b/Training/Medium/scrabble.rb new file mode 100644 index 0000000..5bd0388 --- /dev/null +++ b/Training/Medium/scrabble.rb @@ -0,0 +1,51 @@ +# Solution for https://www.codingame.com/ide/puzzle/scrabble +class Scrabble + attr_accessor :dictionary, :letters, :letters_weight + + def initialize + words_number = gets.to_i + @dictionary = Array.new(words_number) { gets.chomp } + @letters = gets.chomp + @letters_weight = init_letters_weight + end + + def init_letters_weight + { + 1 => %w(e a i o n r t l s u), + 2 => %w(d g), + 3 => %w(b c m p), + 4 => %w(f h v w y), + 5 => %w(k), + 8 => %w(j x), + 10 => %w(q z) + } + end + + def matching?(word) + word.each_char do |char| + return false if letters.count(char) < word.count(char) || !letters.include?(char) + end + true + end + + def find_all_words + words_with_points = dictionary.each_with_object({}) do |word, result| + result[word] = calculate_points(word) if matching?(word) + end + words_with_points.key(words_with_points.values.max) + end + + def calculate_points(word) + points = 0 + word.each_char do |char| + letters_weight.each do |k, v| + points += k if v.include?(char) + end + end + points + end +end + +obj = Scrabble.new +answer = obj.find_all_words +puts answer