Home > Software engineering >  Searching using a single integer in a hash whose keys are ranges in Ruby
Searching using a single integer in a hash whose keys are ranges in Ruby

Time:08-30

I have this hash in Ruby:

hash = {
   0..25  => { low_battery_count:     13 }, 
  26..75  => { average_battery_count:  4 }, 
  76..100 => { good_battery_count:     4 }
}

Now, what is want is a method (preferably a built-in one) in which if I pass a value (integer e.g. 20, 30, etc), it should return me the value against the range in which this integer lies.

I have already solved it using each method but now, I want to optimize it, even more, to do it without each or map methods to reduce its complexity.

For Example:

method(3) #=> {:low_battery_count=>13}
method(35) #=> {:average_battery_count=>4}
method(90) #=> {:good_battery_count=>4}

CodePudding user response:

You need to get exact key in order to fetch a value from the hash. So either way a passed number should be tested against the available keys (ranges).

You might consider introducing an optimisation based on assigning pre-determined ranges to the constants and using a case equality check:

SMALL = 0..25
MEDUIM = 26..75
LARGE = 76..100


def method(n)
  case n
  when SMALL then data[SMALL]
  when MEDUIM then data[MEDUIM]
  when LARGE then data[LARGE]
  end
end

def data
  @data ||= {
    SMALL => { low_battery_count: 13 },
    MEDUIM => { average_battery_count: 4 },
    LARGE =>{ good_battery_count: 4 }
  }
end

method(25)
=> {:low_battery_count=>13}
method(44)
=> {:average_battery_count=>4}
method(76)
=> {:good_battery_count=>4}

CodePudding user response:

We can do this using some of built in methods but there is not any direct way to do this

class Hash
  def value_at_key_range(range_member)
    keys.select{ |range| range.include?(range_member) }.map{ |key| self[key]}
  end
end

hash = {0..25=>{:low_battery_count=>13}, 26..75=>{:average_battery_count=>4}, 76..100=>{:good_battery_count=>4}}

p hash.value_at_key_range(10)

output

[{:low_battery_count=>13}]
  • Related