Ad
Fundamentals
Numbers
Data Types
Mathematics
Algorithms
Logic
Code
Diff
  • def temperature_convert(temperature):
        
        if temperature[1] == temperature[2]:
            return temperature[0]
        
        converter = {'ck': '{} + 273.15',
                     'cr': '({} * 1.8) + 491.67',
                     'cf': '({} * 1.8) + 32',
                     'rc': '({} - 491.67) * (5/9)',
                     'rk': '{} * (5/9)',
                     'rf': '{} - 459.67',
                     'kc': '{} - 273.15',
                     'kr': '{} * 1.8',
                     'kf': '(({} - 273.15) * 1.8) + 32',
                     'fc': '({} - 32) * (5/9)',
                     'fk': '(({} - 32) * (5/9)) + 273.15',
                     'fr': '{} + 459.67'}
        
        value = temperature[0]
        formula = converter[str(temperature[1] + temperature[2])]
        
        return int(eval(formula.format(value)))
    • # Fastest way is to convert everything to celsius and then convert to final temperature
    • import math
    • def temperature_convert(temperature):
    • z = 0
    • # list of if clauses to figure out steps based on 2nd value of list
    • if temperature[1] == 'r':
    • z = (temperature[0] - 491.67) * 5 / 9
    • elif temperature[1] == 'f':
    • z = (temperature[0] - 32) * 5 / 9
    • elif temperature[1] == 'k':
    • z = temperature[0] - 273.15
    • else:
    • z = temperature[0]
    • # list of if clauses to figure out steps based on 3rd value of list
    • if temperature[2] == 'r':
    • return math.trunc((z * 9 / 5) + 491.67)
    • elif temperature[2] == 'f':
    • return math.trunc((z * 9 / 5) + 32)
    • elif temperature[2] == 'k':
    • return math.trunc(z + 273.15)
    • else:
    • return math.trunc(z)
    • if temperature[1] == temperature[2]:
    • return temperature[0]
    • converter = {'ck': '{} + 273.15',
    • 'cr': '({} * 1.8) + 491.67',
    • 'cf': '({} * 1.8) + 32',
    • 'rc': '({} - 491.67) * (5/9)',
    • 'rk': '{} * (5/9)',
    • 'rf': '{} - 459.67',
    • 'kc': '{} - 273.15',
    • 'kr': '{} * 1.8',
    • 'kf': '(({} - 273.15) * 1.8) + 32',
    • 'fc': '({} - 32) * (5/9)',
    • 'fk': '(({} - 32) * (5/9)) + 273.15',
    • 'fr': '{} + 459.67'}
    • value = temperature[0]
    • formula = converter[str(temperature[1] + temperature[2])]
    • return int(eval(formula.format(value)))