Skip to content

Instantly share code, notes, and snippets.

@zerothabhishek
Last active June 23, 2016 12:15
Show Gist options
  • Select an option

  • Save zerothabhishek/e88a9f2d946120b05f6f59241bfb3512 to your computer and use it in GitHub Desktop.

Select an option

Save zerothabhishek/e88a9f2d946120b05f6f59241bfb3512 to your computer and use it in GitHub Desktop.
The partial calculator problem I used in interviews
#
# We have something called a PartialCalculator in the code
# for calculating some health related parameters.
#
# The PartialCalculator works like this -
#
# - take an input hash
# - merge the given data with some standard values
# - calls a FullCalculator with the values (in the overall method)
#
# It also has a special case where it has to produce a `bp_component`.
# For that, it
#
# - takes the input hash
# - merge the given data with some standard values
# - overrides the standard value of bp with the given value
#
# The invocation could be like this-
#
# pc = PartialCalculator.new(sbp: 124, dbp: 85, hdl: 55, tcl: 120, bmi: 23)
# puts pc.overall
# puts pc.bp_component
#
# [Thanks to Healthi (https://www.healthi.in) where I tried this in interviews]
#
class PartialCalculator
StandardValues = {
sbp: 120,
dbp: 80,
hdl: 50
}
def initialize(data)
@data = data
@values = @data.merge!(StandardValues)
end
def overall
FullCalculator.new(@values).calculate
end
def bp_component
@values[:sbp] = @data[:sbp]
FullCalculator.new(@values).calculate
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment