Ad
Code
Diff
  • def high_and_low(str_in)
      str_in.split.minmax_by{|n|-n.to_i}.join(' ')
    end
    • def high_and_low(str_in)
    • str_in.split.map(&:to_i).minmax.reverse.join(' ')
    • str_in.split.minmax_by{|n|-n.to_i}.join(' ')
    • end

more faster

require 'benchmark'

strings = []

10000.times do
  string = Array.new(rand(500..1000)) { rand(10).to_s }.join('')
  strings << string
end

def solution1(array)
  array.each do |s|
    s.chars.sum(&:to_i)
  end
end

def solution2(array)
  array.each do |s|
    s.sum - s.size * 48
  end
end

Benchmark.bmbm do |x|
  x.report { solution1(strings) }
  x.report { solution2(strings) }
end

result

Rehearsal ------------------------------------
   1.032923   0.004906   1.037829 (  1.044178)
   0.009852   0.000544   0.010396 (  0.010945)
--------------------------- total: 1.048225sec

       user     system      total        real
   1.032176   0.006350   1.038526 (  1.045689)
   0.007727   0.000118   0.007845 (  0.007965)
Code
Diff
  • def getIDS(string)
      string.sum - string.size * 48
    end
    • def getIDS(string)
    • string.chars.sum(&:to_i)
    • string.sum - string.size * 48
    • end