Skip to content

Commit 99b40e2

Browse files
authored
add Rankine scale (TheAlgorithms#2232)
* add Rankine scale black formatting * add Wikipedia links * add optional rounding, default to 2 digits * fix variable name * fixed variable name; helps to stage before commiting
1 parent a823a86 commit 99b40e2

File tree

1 file changed

+199
-17
lines changed

1 file changed

+199
-17
lines changed

conversions/temperature_conversions.py

+199-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
""" Convert between different units of temperature """
22

33

4-
def celsius_to_fahrenheit(celsius: float) -> float:
4+
def celsius_to_fahrenheit(celsius: float, ndigits: int = 2) -> float:
55
"""
66
Convert a given value from Celsius to Fahrenheit and round it to 2 decimal places.
7+
Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
8+
Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
79
810
>>> celsius_to_fahrenheit(-40.0)
911
-40.0
@@ -20,12 +22,54 @@ def celsius_to_fahrenheit(celsius: float) -> float:
2022
...
2123
ValueError: could not convert string to float: 'celsius'
2224
"""
23-
return round((float(celsius) * 9 / 5) + 32, 2)
25+
return round((float(celsius) * 9 / 5) + 32, ndigits)
2426

2527

26-
def fahrenheit_to_celsius(fahrenheit: float) -> float:
28+
def celsius_to_kelvin(celsius: float, ndigits: int = 2) -> float:
29+
"""
30+
Convert a given value from Celsius to Kelvin and round it to 2 decimal places.
31+
Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
32+
Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
33+
34+
>>> celsius_to_kelvin(0)
35+
273.15
36+
>>> celsius_to_kelvin(20.0)
37+
293.15
38+
>>> celsius_to_kelvin("40")
39+
313.15
40+
>>> celsius_to_kelvin("celsius")
41+
Traceback (most recent call last):
42+
...
43+
ValueError: could not convert string to float: 'celsius'
44+
"""
45+
return round(float(celsius) + 273.15, ndigits)
46+
47+
48+
def celsius_to_rankine(celsius: float, ndigits: int = 2) -> float:
49+
"""
50+
Convert a given value from Celsius to Rankine and round it to 2 decimal places.
51+
Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
52+
Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
53+
54+
>>> celsius_to_rankine(0)
55+
491.67
56+
>>> celsius_to_rankine(20.0)
57+
527.67
58+
>>> celsius_to_rankine("40")
59+
563.67
60+
>>> celsius_to_rankine("celsius")
61+
Traceback (most recent call last):
62+
...
63+
ValueError: could not convert string to float: 'celsius'
64+
"""
65+
return round((float(celsius) * 9 / 5) + 491.67, ndigits)
66+
67+
68+
def fahrenheit_to_celsius(fahrenheit: float, ndigits: int = 2) -> float:
2769
"""
2870
Convert a given value from Fahrenheit to Celsius and round it to 2 decimal places.
71+
Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
72+
Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
2973
3074
>>> fahrenheit_to_celsius(0)
3175
-17.78
@@ -44,30 +88,66 @@ def fahrenheit_to_celsius(fahrenheit: float) -> float:
4488
...
4589
ValueError: could not convert string to float: 'fahrenheit'
4690
"""
47-
return round((float(fahrenheit) - 32) * 5 / 9, 2)
91+
return round((float(fahrenheit) - 32) * 5 / 9, ndigits)
4892

4993

50-
def celsius_to_kelvin(celsius: float) -> float:
94+
def fahrenheit_to_kelvin(fahrenheit: float, ndigits: int = 2) -> float:
5195
"""
52-
Convert a given value from Celsius to Kelvin and round it to 2 decimal places.
96+
Convert a given value from Fahrenheit to Kelvin and round it to 2 decimal places.
97+
Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
98+
Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
5399
54-
>>> celsius_to_kelvin(0)
55-
273.15
56-
>>> celsius_to_kelvin(20.0)
57-
293.15
58-
>>> celsius_to_kelvin("40")
59-
313.15
60-
>>> celsius_to_kelvin("celsius")
100+
>>> fahrenheit_to_kelvin(0)
101+
255.37
102+
>>> fahrenheit_to_kelvin(20.0)
103+
266.48
104+
>>> fahrenheit_to_kelvin(40.0)
105+
277.59
106+
>>> fahrenheit_to_kelvin(60)
107+
288.71
108+
>>> fahrenheit_to_kelvin(80)
109+
299.82
110+
>>> fahrenheit_to_kelvin("100")
111+
310.93
112+
>>> fahrenheit_to_kelvin("fahrenheit")
61113
Traceback (most recent call last):
62114
...
63-
ValueError: could not convert string to float: 'celsius'
115+
ValueError: could not convert string to float: 'fahrenheit'
116+
"""
117+
return round(((float(fahrenheit) - 32) * 5 / 9) + 273.15, ndigits)
118+
119+
120+
def fahrenheit_to_rankine(fahrenheit: float, ndigits: int = 2) -> float:
121+
"""
122+
Convert a given value from Fahrenheit to Rankine and round it to 2 decimal places.
123+
Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
124+
Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
125+
126+
>>> fahrenheit_to_rankine(0)
127+
459.67
128+
>>> fahrenheit_to_rankine(20.0)
129+
479.67
130+
>>> fahrenheit_to_rankine(40.0)
131+
499.67
132+
>>> fahrenheit_to_rankine(60)
133+
519.67
134+
>>> fahrenheit_to_rankine(80)
135+
539.67
136+
>>> fahrenheit_to_rankine("100")
137+
559.67
138+
>>> fahrenheit_to_rankine("fahrenheit")
139+
Traceback (most recent call last):
140+
...
141+
ValueError: could not convert string to float: 'fahrenheit'
64142
"""
65-
return round(float(celsius) + 273.15, 2)
143+
return round(float(fahrenheit) + 459.67, ndigits)
66144

67145

68-
def kelvin_to_celsius(kelvin: float) -> float:
146+
def kelvin_to_celsius(kelvin: float, ndigits: int = 2) -> float:
69147
"""
70148
Convert a given value from Kelvin to Celsius and round it to 2 decimal places.
149+
Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
150+
Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
71151
72152
>>> kelvin_to_celsius(273.15)
73153
0.0
@@ -80,9 +160,111 @@ def kelvin_to_celsius(kelvin: float) -> float:
80160
...
81161
ValueError: could not convert string to float: 'kelvin'
82162
"""
83-
return round(float(kelvin) - 273.15, 2)
163+
return round(float(kelvin) - 273.15, ndigits)
164+
165+
166+
def kelvin_to_fahrenheit(kelvin: float, ndigits: int = 2) -> float:
167+
"""
168+
Convert a given value from Kelvin to Fahrenheit and round it to 2 decimal places.
169+
Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
170+
Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
171+
172+
>>> kelvin_to_fahrenheit(273.15)
173+
32.0
174+
>>> kelvin_to_fahrenheit(300)
175+
80.33
176+
>>> kelvin_to_fahrenheit("315.5")
177+
108.23
178+
>>> kelvin_to_fahrenheit("kelvin")
179+
Traceback (most recent call last):
180+
...
181+
ValueError: could not convert string to float: 'kelvin'
182+
"""
183+
return round(((float(kelvin) - 273.15) * 9 / 5) + 32, ndigits)
184+
185+
186+
def kelvin_to_rankine(kelvin: float, ndigits: int = 2) -> float:
187+
"""
188+
Convert a given value from Kelvin to Rankine and round it to 2 decimal places.
189+
Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
190+
Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
191+
192+
>>> kelvin_to_rankine(0)
193+
0.0
194+
>>> kelvin_to_rankine(20.0)
195+
36.0
196+
>>> kelvin_to_rankine("40")
197+
72.0
198+
>>> kelvin_to_rankine("kelvin")
199+
Traceback (most recent call last):
200+
...
201+
ValueError: could not convert string to float: 'kelvin'
202+
"""
203+
return round((float(kelvin) * 9 / 5), ndigits)
204+
205+
206+
def rankine_to_celsius(rankine: float, ndigits: int = 2) -> float:
207+
"""
208+
Convert a given value from Rankine to Celsius and round it to 2 decimal places.
209+
Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
210+
Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
211+
212+
>>> rankine_to_celsius(273.15)
213+
-121.4
214+
>>> rankine_to_celsius(300)
215+
-106.48
216+
>>> rankine_to_celsius("315.5")
217+
-97.87
218+
>>> rankine_to_celsius("rankine")
219+
Traceback (most recent call last):
220+
...
221+
ValueError: could not convert string to float: 'rankine'
222+
"""
223+
return round((float(rankine) - 491.67) * 5 / 9, ndigits)
224+
225+
226+
def rankine_to_fahrenheit(rankine: float, ndigits: int = 2) -> float:
227+
"""
228+
Convert a given value from Rankine to Fahrenheit and round it to 2 decimal places.
229+
Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
230+
Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
231+
232+
>>> rankine_to_fahrenheit(273.15)
233+
-186.52
234+
>>> rankine_to_fahrenheit(300)
235+
-159.67
236+
>>> rankine_to_fahrenheit("315.5")
237+
-144.17
238+
>>> rankine_to_fahrenheit("rankine")
239+
Traceback (most recent call last):
240+
...
241+
ValueError: could not convert string to float: 'rankine'
242+
"""
243+
return round(float(rankine) - 459.67, ndigits)
244+
245+
246+
def rankine_to_kelvin(rankine: float, ndigits: int = 2) -> float:
247+
"""
248+
Convert a given value from Rankine to Kelvin and round it to 2 decimal places.
249+
Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
250+
Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
251+
252+
>>> rankine_to_kelvin(0)
253+
0.0
254+
>>> rankine_to_kelvin(20.0)
255+
11.11
256+
>>> rankine_to_kelvin("40")
257+
22.22
258+
>>> rankine_to_kelvin("rankine")
259+
Traceback (most recent call last):
260+
...
261+
ValueError: could not convert string to float: 'rankine'
262+
"""
263+
return round((float(rankine) * 5 / 9), ndigits)
84264

85265

86266
if __name__ == "__main__":
267+
87268
import doctest
269+
88270
doctest.testmod()

0 commit comments

Comments
 (0)