forked from ZCW-Java8-2/Python6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemperature_utils_v2.py
More file actions
69 lines (57 loc) · 3.04 KB
/
temperature_utils_v2.py
File metadata and controls
69 lines (57 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import math
from typing import Iterable, Tuple
def convert_to_celsius(fahrenheit_temp: float) -> float:
"""
Given a float representing a temperature in fahrenheit, return the corresponding value in celsius.
:param fahrenheit_temp: A float representing a temperature in fahrenheit
:return: A float representing the corresponding value of the fahrenheit_temp parameter in celsius
"""
temp = round(((fahrenheit_temp - 32) * 5 / 9), 2)
return float('%.2f' % temp)
def convert_to_fahrenheit(celsius_temp: float) -> int:
"""
Given a float representing a temperature in celsius, return the corresponding value in fahrenheit.
:param celsius_temp: A float representing a temperature in celsius
:return: A float representing the corresponding value of the celsius_temp parameter in fahrenheit
"""
temp = round(((celsius_temp * 9 / 5) + 32), 2)
return float("%.2f" % temp)
def convert_c_to_kelvin(celsius_temp: float) -> int:
"""
Given a float representing a temperature in celsius, return the corresponding value in kelvin.
:param celsius_temp: A float representing a temperature in celsius
:return: A float representing the corresponding value of the celsius_temp parameter in kelvin
"""
temp = (celsius_temp + 273.15)
return float("%.2f" % temp)
def convert_f_to_kelvin(fahrenheit_temp: float) -> int:
"""
Given a float representing a temperature in fahrenheit, return the corresponding value in kelvin.
:param fahrenheit_temp: A float representing a temperature in fahrenheit
:return: A float representing the corresponding value of the fahrenheit_temp parameter in kelvin
"""
temp = round(((fahrenheit_temp - 32) * 5 / 9), 2) + 273.15
return float("%.2f" % temp)
def temperature_tuple(temperatures: Iterable, input_unit_of_measurement: str) -> Tuple[Tuple[float, float]]:
"""
Given a tuple or a list of temperatures, this function returns a tuple of tuples.
Each tuple contains two values. The first is the value of the temperatures parameter. The second is the the value of
the first converted to the unit of measurement specified in the input_unit_of_measurement parameter.
:param temperatures: An iterable containing temperatures
:param input_unit_of_measurement: The unit a measure to use to convert the values in the temperatures parameter
:return: A tuple of tuples
"""
temps = ()
if (input_unit_of_measurement == 'c'):
for index, element in enumerate(temperatures):
temps += (element, convert_to_fahrenheit(element)),
elif (input_unit_of_measurement == 'f'):
for index, element in enumerate(temperatures):
temps += (element, convert_to_celsius(element)),
elif (input_unit_of_measurement == 'fk'):
for index, element in enumerate(temperatures):
temps += (element, convert_f_to_kelvin(element)),
elif (input_unit_of_measurement == 'ck'):
for index, element in enumerate(temperatures):
temps += (element, convert_c_to_kelvin(element)),
return temps