Skip to content

Instantly share code, notes, and snippets.

@hosh
Created May 18, 2009 20:26
Show Gist options
  • Save hosh/113720 to your computer and use it in GitHub Desktop.
Save hosh/113720 to your computer and use it in GitHub Desktop.
def blah(x)
return 1 if x == 1 # Special case
return nil unless x > 1
y = x.to_i/10.0
z = 10**(Math.log10(y).ceil)
return (z/2).ceil if y <= z/2
return z
end
# Does this work?
require 'pp'
pp [1, 5, 10, 11, 50, 51, 100, 101, 500, 501, 10001, 50000, 50001].map { |x| [x, blah(x)] }
#[[1, 1],
# [5, 1],
# [10, 1],
# [11, 5],
# [50, 5],
# [51, 10],
# [100, 10],
# [101, 50],
# [500, 50],
# [501, 100],
# [10001, 5000],
# [50000, 5000],
# [50001, 10000]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment