Quick Science Lesson:
There are four major types of temparature scales (Celsius, Kelvin, Fahrenheit, and Rankine).
To convert Celsius to Kelvin simply add 273.15
EX:
3°C = 276.15K
To convert Celsius to Fahrenheit simply multiply by 9/5 and add 32
EX:
3°C = 37.4°F
To convert Celsius to Rankine simply multiply by 9/5 and add 491.67
EX:
3°C = 497.07°R
NOTE: Remember you can convert from different if you wanted
Actual Program:
Basically you will be given a list in the format [21, 'r', 'c'], which means you will have to convert 21°R to °C and return the float temperature back.
Ex #1:
Input:
[21, 'r', 'c']
Output:
-261
Ex #2:
Input:
[234.21, 'f', 'r']
Output:
693
Ex #3:
Input:
[-122, 'k', 'c']
Output:
-395
NOTE: Make sure you return only ints
# 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)
Test.assert_equals(temperature_convert([21, 'r', 'c']), -261)
Test.assert_equals(temperature_convert([234.21, 'f', 'r']),693)
Test.assert_equals(temperature_convert([-122, 'k', 'c']), -395)
Test.assert_equals(temperature_convert([31253245, 'c', 'c']), 31253245)
Test.assert_equals(temperature_convert([531, 'f', 'c']),277)
Test.assert_equals(temperature_convert([8658, 'k', 'f']), 15124)
Test.assert_equals(temperature_convert([22, 'r', 'f']), -437)
Test.assert_equals(temperature_convert([234.21, 'f', 'k']),385)
Test.assert_equals(temperature_convert([-1222, 'k', 'k']), -1222)
Test.assert_equals(temperature_convert([431, 'r', 'r']), 431)
Test.assert_equals(temperature_convert([2324.21, 'f', 'c']),1273)
Test.assert_equals(temperature_convert([0, 'k', 'r']), 0)
Test.assert_equals(temperature_convert([2, 'r', 'r']), 2)
Test.assert_equals(temperature_convert([4, 'f', 'c']),-15)