From b8e5d888bc82740405e8ae6c438473c8084bc3b4 Mon Sep 17 00:00:00 2001 From: Slavunderkind Date: Mon, 16 Jan 2017 12:11:15 +0200 Subject: [PATCH 1/3] Move files into a relevant folders --- ascii_art.rb => Training/Easy/ascii_art.rb | 0 .../Easy/chuck_norris.rb | 0 .../Easy/defibrillators.rb | 0 .../Easy/horse_racing.rb | 0 mime_type.rb => Training/Easy/mime_type.rb | 0 .../Easy/power_of_thor.rb | 0 .../Easy/temperatures.rb | 0 .../Easy/the_descent.rb | 0 Training/Medium/spoon.rb | 57 +++++++++++++++++++ 9 files changed, 57 insertions(+) rename ascii_art.rb => Training/Easy/ascii_art.rb (100%) rename chuck_norris.rb => Training/Easy/chuck_norris.rb (100%) rename defibrillators.rb => Training/Easy/defibrillators.rb (100%) rename horse_racing.rb => Training/Easy/horse_racing.rb (100%) rename mime_type.rb => Training/Easy/mime_type.rb (100%) rename power_of_thor.rb => Training/Easy/power_of_thor.rb (100%) rename temperatures.rb => Training/Easy/temperatures.rb (100%) rename the_descent.rb => Training/Easy/the_descent.rb (100%) create mode 100644 Training/Medium/spoon.rb 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/spoon.rb b/Training/Medium/spoon.rb new file mode 100644 index 0000000..b4690c3 --- /dev/null +++ b/Training/Medium/spoon.rb @@ -0,0 +1,57 @@ +STDOUT.sync = true # DO NOT REMOVE +# Solution for https://www.codingame.com/ide/puzzle/there-is-no-spoon-episode-1 +class Spoon + attr_accessor :width, :height, :nodes + + def initialize + @width = gets.to_i # the number of cells on the X axis + @height = gets.to_i # the number of cells on the Y axis + @nodes = [] + init_nodes + end + + def init_nodes + n = 0 + height.times do + line = gets.chomp # width characters, each either 0 or . + find_node(line, n) + n += 1 + end + end + + def find_node(chars, n) + chars.each_char.with_index do |char, i| + next if char == '.' + + nodes.push([i, n]) + end + end + + def find_right_node(node) + nodes.select { |n| n if n[1] == node[1] && n[0] > node[0] }.min + end + + def find_bottom_node(node) + nodes.select { |n| n if n[0] == node[0] && n[1] > node[1] }.min + end + + def add_coordinates(result) + if result.nil? + ' -1 -1' + else + " #{result[0]} #{result[1]}" + end + end + + def start + nodes.each do |node| + result = "#{node[0]} #{node[1]}" + result += add_coordinates(find_right_node(node)) + result += add_coordinates(find_bottom_node(node)) + puts result + end + end +end + +obj = Spoon.new +obj.start From e474941d4eb64810cf496a421930b6a6bb8df1b9 Mon Sep 17 00:00:00 2001 From: Slavunderkind Date: Mon, 16 Jan 2017 17:51:05 +0200 Subject: [PATCH 2/3] Solution for scrabble puzzle --- Training/Medium/scrabble.rb | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Training/Medium/scrabble.rb diff --git a/Training/Medium/scrabble.rb b/Training/Medium/scrabble.rb new file mode 100644 index 0000000..dca02f8 --- /dev/null +++ b/Training/Medium/scrabble.rb @@ -0,0 +1,55 @@ +# Solution for https://www.codingame.com/ide/puzzle/scrabble +class Scrabble + attr_accessor :words_count, :dictionary, :letters, :letters_weight + + def initialize + @n = gets.to_i + @dictionary = @n.times.each_with_object([]) { |_, result| result << gets.chomp } + @letters = gets.chomp + @letters_weight = init_letters_weight + end + + def init_letters_weight + letters_weight = {} + letters_weight[1] = %w(e a i o n r t l s u) + letters_weight[2] = %w(d g) + letters_weight[3] = %w(b c m p) + letters_weight[4] = %w(f h v w y) + letters_weight[5] = ['k'] + letters_weight[8] = %w(j x) + letters_weight[10] = %w(q z) + letters_weight + 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.find_all do |word| + words_with_points[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 + + def start + puts find_all_words + end +end + +obj = Scrabble.new +obj.start From 638fceb8accb33e853e60e6f87bda6d7e7a5059c Mon Sep 17 00:00:00 2001 From: Slavunderkind Date: Tue, 24 Jan 2017 12:23:52 +0200 Subject: [PATCH 3/3] Refactor the code --- Training/Medium/scrabble.rb | 36 +++++++++++------------ Training/Medium/spoon.rb | 57 ------------------------------------- 2 files changed, 16 insertions(+), 77 deletions(-) delete mode 100644 Training/Medium/spoon.rb diff --git a/Training/Medium/scrabble.rb b/Training/Medium/scrabble.rb index dca02f8..5bd0388 100644 --- a/Training/Medium/scrabble.rb +++ b/Training/Medium/scrabble.rb @@ -1,24 +1,24 @@ # Solution for https://www.codingame.com/ide/puzzle/scrabble class Scrabble - attr_accessor :words_count, :dictionary, :letters, :letters_weight + attr_accessor :dictionary, :letters, :letters_weight def initialize - @n = gets.to_i - @dictionary = @n.times.each_with_object([]) { |_, result| result << gets.chomp } + 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 - letters_weight = {} - letters_weight[1] = %w(e a i o n r t l s u) - letters_weight[2] = %w(d g) - letters_weight[3] = %w(b c m p) - letters_weight[4] = %w(f h v w y) - letters_weight[5] = ['k'] - letters_weight[8] = %w(j x) - letters_weight[10] = %w(q z) - 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) @@ -29,9 +29,8 @@ def matching?(word) end def find_all_words - words_with_points = {} - dictionary.find_all do |word| - words_with_points[word] = calculate_points(word) if matching?(word) + 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 @@ -45,11 +44,8 @@ def calculate_points(word) end points end - - def start - puts find_all_words - end end obj = Scrabble.new -obj.start +answer = obj.find_all_words +puts answer diff --git a/Training/Medium/spoon.rb b/Training/Medium/spoon.rb deleted file mode 100644 index b4690c3..0000000 --- a/Training/Medium/spoon.rb +++ /dev/null @@ -1,57 +0,0 @@ -STDOUT.sync = true # DO NOT REMOVE -# Solution for https://www.codingame.com/ide/puzzle/there-is-no-spoon-episode-1 -class Spoon - attr_accessor :width, :height, :nodes - - def initialize - @width = gets.to_i # the number of cells on the X axis - @height = gets.to_i # the number of cells on the Y axis - @nodes = [] - init_nodes - end - - def init_nodes - n = 0 - height.times do - line = gets.chomp # width characters, each either 0 or . - find_node(line, n) - n += 1 - end - end - - def find_node(chars, n) - chars.each_char.with_index do |char, i| - next if char == '.' - - nodes.push([i, n]) - end - end - - def find_right_node(node) - nodes.select { |n| n if n[1] == node[1] && n[0] > node[0] }.min - end - - def find_bottom_node(node) - nodes.select { |n| n if n[0] == node[0] && n[1] > node[1] }.min - end - - def add_coordinates(result) - if result.nil? - ' -1 -1' - else - " #{result[0]} #{result[1]}" - end - end - - def start - nodes.each do |node| - result = "#{node[0]} #{node[1]}" - result += add_coordinates(find_right_node(node)) - result += add_coordinates(find_bottom_node(node)) - puts result - end - end -end - -obj = Spoon.new -obj.start