Skip to content

Instantly share code, notes, and snippets.

@mxriverlynn
Created April 28, 2011 16:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mxriverlynn/946728 to your computer and use it in GitHub Desktop.
Save mxriverlynn/946728 to your computer and use it in GitHub Desktop.
I have a range, which could be made of two integers or a floating point numbers, and a value within that range.
I need to convert the range to a fixed size range (0..10) and adjust the value accordingly.
if the value is below the range, return 0.
if the value is above the range, return 10.
for example, range 1 through 100, value 50. convert to range 1 through 10 and adjusted value is 5.
another example, range 10.5 through 55, value of 27.2. convert to range 1 through 10 and the adjusted value is ???
I need to know how to do the conversion of the original range to the target range, and how to adjust the value, accordingly.
class RangeResults
def score(value)
range_size = self.high_risk_above - self.goal_below
adjusted_value = (value - self.goal_below)
adjusted_value = 0 if adjusted_value < 0
adjusted_value = range_size if value > self.high_risk_above
value_percent = (adjusted_value / range_size)
raw_score = (10 * value_percent)
score = raw_score.round(0).to_i
puts "........adjusted value: #{adjusted_value}"
puts "........range size: #{range_size}"
puts "........value %: #{value_percent}"
puts "........raw score: #{raw_score}"
puts "........final score: #{score}"
score
end
end
range = RangeResults.new(:goal_below => 10, :high_risk_above => 60)
range.score(10) #=> 0
range.score(5) #=> 0
range.score(20) #=> 2
range.score(35) #=> 5
range.score(60) #=> 10
range.score(80) #=> 10
@davidalpert
Copy link

Seems to me that you'd need to normalize the initial values relative to the lowest possible initial value, then find the relative position of that normalized value relative to the size of the range, then scale that to your new range size (0..10), something like this (pardon my syntax):

 dev adjust()

      range_size = range_high_point - range_low_point

      transformed_value = (original_value - range_low_point) / range_size * 10 
 end

@mxriverlynn
Copy link
Author

i think i'm on teh same track as you, in terms of what needs to happen and how to execute it. my solution appears to be a more verbose version of yours... so that's good... it means I'm dong something right. :)

@davidalpert
Copy link

okay, I took a closer look at your code, looks like the same logic, expressed differently. you've got more boundary condition checks but the same general idea. That's what I get for skimming a gist while trying to debug a failing build. ;-)

@DeanWeber
Copy link

I think david has it. I wasn't sure what you were wanting until I read his simplified suggestion. Strange thing is... years ago, I had a similar algorithm when I created the USPS algorithm for postal rate calculations. I created ranges with increments within the ranges until a new range bracket was found. I think I replaced 10 with a sizing variable dependent upon the range needs. Anyway, this looks dead on to me.

Good job David! You're quicker on the draw than I am.

@mxriverlynn
Copy link
Author

thanks david / dean. this appears to be working the way i need it to. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment