Created
April 28, 2011 16:46
-
-
Save mxriverlynn/946728 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks david / dean. this appears to be working the way i need it to. :)