Ad
Fundamentals
Numbers
Data Types
Mathematics
Algorithms
Logic
Code
Diff
  • # the affine transformations of the form "y = a*x + b"
    # from Kelvin (k) to Celsius (c), Rankine (r) or Fahrenheit (F)
    from_kelvin = {
        "c": {"a": 1.0, "b": -273.15},
        "r": {"a": 1.8, "b": 0.0},
        "f": {"a": 1.8, "b": -273.15 * 1.8 + 32.0},
        "k": {"a": 1.0, "b": 0.0},
    }
    
    
    def temperature_convert(params):
        value, src, tgt = params
        # convert source unit to Kelvins : invert from_kelvin, x = (y - b) / a
        value_in_k = (value - from_kelvin[src]["b"]) / from_kelvin[src]["a"]
        # convert Kelvins to target unit, rounded to integer
        return int(value_in_k * from_kelvin[tgt]["a"] + from_kelvin[tgt]["b"])
    • converters = {
    • 'ck': lambda value: value + 273.15,
    • 'cr': lambda value: (value * 1.8) + 491.67,
    • 'cf': lambda value: (value * 1.8) + 32,
    • 'rc': lambda value: (value - 491.67) * (5/9),
    • 'rk': lambda value: value * (5/9),
    • 'rf': lambda value: value - 459.67,
    • 'kc': lambda value: value - 273.15,
    • 'kr': lambda value: value * 1.8,
    • 'kf': lambda value: ((value - 273.15) * 1.8) + 32,
    • 'fc': lambda value: (value - 32) * (5/9),
    • 'fk': lambda value: ((value - 32) * (5/9)) + 273.15,
    • 'fr': lambda value: value + 459.67
    • # the affine transformations of the form "y = a*x + b"
    • # from Kelvin (k) to Celsius (c), Rankine (r) or Fahrenheit (F)
    • from_kelvin = {
    • "c": {"a": 1.0, "b": -273.15},
    • "r": {"a": 1.8, "b": 0.0},
    • "f": {"a": 1.8, "b": -273.15 * 1.8 + 32.0},
    • "k": {"a": 1.0, "b": 0.0},
    • }
    • def temperature_convert(temperature):
    • (value, a, b) = temperature
    • return int(converters.get(a + b, lambda value: value)(value))
    • def temperature_convert(params):
    • value, src, tgt = params
    • # convert source unit to Kelvins : invert from_kelvin, x = (y - b) / a
    • value_in_k = (value - from_kelvin[src]["b"]) / from_kelvin[src]["a"]
    • # convert Kelvins to target unit, rounded to integer
    • return int(value_in_k * from_kelvin[tgt]["a"] + from_kelvin[tgt]["b"])