From 9229ce44a17f001b89a6654758bc614252ffd042 Mon Sep 17 00:00:00 2001 From: Slavunderkind Date: Thu, 12 Jan 2017 11:22:42 +0200 Subject: [PATCH 1/3] Add initialize method and change variable names --- Training/Easy/horse_racing.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Training/Easy/horse_racing.rb b/Training/Easy/horse_racing.rb index 6580aa8..2aa121e 100644 --- a/Training/Easy/horse_racing.rb +++ b/Training/Easy/horse_racing.rb @@ -1,16 +1,21 @@ # Solution for https://www.codingame.com/ide/puzzle/horse-racing-duals class HorseRacing + attr_accessor :horses_number + + def initialize + @horses_number = gets.to_i + end + def find_difference strenghts = init_strenghts strenghts.each_cons(2).map { |a, b| (b - a).abs }.min end def init_strenghts - n = gets.to_i strenghts_array = [] - n.times do - pi = gets.to_i - strenghts_array << pi + horses_number.times do + horse_strenght = gets.to_i + strenghts_array << horse_strenght end strenghts_array.sort end From 224785d32d1e0f214a1a4b69dbf699222c65cac4 Mon Sep 17 00:00:00 2001 From: Slavunderkind Date: Thu, 12 Jan 2017 17:51:41 +0200 Subject: [PATCH 2/3] Refactoring on init strengths method --- Training/Easy/horse_racing.rb | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/Training/Easy/horse_racing.rb b/Training/Easy/horse_racing.rb index 2aa121e..787b490 100644 --- a/Training/Easy/horse_racing.rb +++ b/Training/Easy/horse_racing.rb @@ -1,23 +1,18 @@ # Solution for https://www.codingame.com/ide/puzzle/horse-racing-duals class HorseRacing - attr_accessor :horses_number + attr_accessor :horses_count def initialize - @horses_number = gets.to_i + @horses_count = gets.to_i end def find_difference - strenghts = init_strenghts - strenghts.each_cons(2).map { |a, b| (b - a).abs }.min + strengths = init_strengths + strengths.each_cons(2).map { |a, b| (b - a).abs }.min end - def init_strenghts - strenghts_array = [] - horses_number.times do - horse_strenght = gets.to_i - strenghts_array << horse_strenght - end - strenghts_array.sort + def init_strengths + horses_count.times.each_with_object([]) { |_, result| result << gets.to_i }.sort end def start From 510c9167f64138adda159575bcde27f2d972506f Mon Sep 17 00:00:00 2001 From: Slavunderkind Date: Tue, 24 Jan 2017 14:57:49 +0200 Subject: [PATCH 3/3] Refactor the code --- Training/Easy/horse_racing.rb | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/Training/Easy/horse_racing.rb b/Training/Easy/horse_racing.rb index 787b490..cdf59a9 100644 --- a/Training/Easy/horse_racing.rb +++ b/Training/Easy/horse_racing.rb @@ -1,24 +1,21 @@ # Solution for https://www.codingame.com/ide/puzzle/horse-racing-duals class HorseRacing - attr_accessor :horses_count + attr_reader :horses_count, :strengths - def initialize - @horses_count = gets.to_i - end - - def find_difference - strengths = init_strengths - strengths.each_cons(2).map { |a, b| (b - a).abs }.min + def initialize(horses_count) + @horses_count = horses_count + @strengths = init_strengths end def init_strengths - horses_count.times.each_with_object([]) { |_, result| result << gets.to_i }.sort + Array.new(horses_count) { gets.to_i }.sort end - def start - puts find_difference + def find_difference + strengths.each_cons(2).map { |a, b| (b - a).abs }.min end end -obj = HorseRacing.new -obj.start +horses_count = gets.to_i +obj = HorseRacing.new(horses_count) +puts obj.find_difference