Skip to content

Commit 23f43af

Browse files
Added volume conversions (TheAlgorithms#5607)
* Added volume conversions This is a file which has relevant function which helps in conversion between volume units. Available Units:- Cubic metre,Litre,KiloLitre,Gallon,Cubic yard,Cubic foot,cup The file is also written in a way that , adding a new unit can easily be done by modifying tuple available in the source code * Formatted file The file was formatted to follow the syntax formatting rules of the repo * Formatted file further
1 parent 6fcefc0 commit 23f43af

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

conversions/volume_conversions.py

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""
2+
Conversion of volume units.
3+
Available Units:- Cubic metre,Litre,KiloLitre,Gallon,Cubic yard,Cubic foot,cup
4+
USAGE :
5+
-> Import this file into their respective project.
6+
-> Use the function length_conversion() for conversion of volume units.
7+
-> Parameters :
8+
-> value : The number of from units you want to convert
9+
-> from_type : From which type you want to convert
10+
-> to_type : To which type you want to convert
11+
REFERENCES :
12+
-> Wikipedia reference: https://en.wikipedia.org/wiki/Cubic_metre
13+
-> Wikipedia reference: https://en.wikipedia.org/wiki/Litre
14+
-> Wikipedia reference: https://en.wiktionary.org/wiki/kilolitre
15+
-> Wikipedia reference: https://en.wikipedia.org/wiki/Gallon
16+
-> Wikipedia reference: https://en.wikipedia.org/wiki/Cubic_yard
17+
-> Wikipedia reference: https://en.wikipedia.org/wiki/Cubic_foot
18+
-> Wikipedia reference: https://en.wikipedia.org/wiki/Cup_(unit)
19+
"""
20+
21+
from collections import namedtuple
22+
23+
from_to = namedtuple("from_to", "from_ to")
24+
25+
METRIC_CONVERSION = {
26+
"cubicmeter": from_to(1, 1),
27+
"litre": from_to(0.001, 1000),
28+
"kilolitre": from_to(1, 1),
29+
"gallon": from_to(0.00454, 264.172),
30+
"cubicyard": from_to(0.76455, 1.30795),
31+
"cubicfoot": from_to(0.028, 35.3147),
32+
"cup": from_to(0.000236588, 4226.75),
33+
}
34+
35+
36+
def volume_conversion(value: float, from_type: str, to_type: str) -> float:
37+
"""
38+
Conversion between volume units.
39+
>>> volume_conversion(4, "cubicmeter", "litre")
40+
4000
41+
>>> volume_conversion(1, "litre", "gallon")
42+
0.264172
43+
>>> volume_conversion(1, "kilolitre", "cubicmeter")
44+
1
45+
>>> volume_conversion(3, "gallon", "cubicyard")
46+
0.017814279
47+
>>> volume_conversion(2, "cubicyard", "litre")
48+
1529.1
49+
>>> volume_conversion(4, "cubicfoot", "cup")
50+
473.396
51+
>>> volume_conversion(1, "cup", "kilolitre")
52+
0.000236588
53+
>>> volume_conversion(4, "wrongUnit", "litre")
54+
Traceback (most recent call last):
55+
File "/usr/lib/python3.8/doctest.py", line 1336, in __run
56+
exec(compile(example.source, filename, "single",
57+
File "<doctest __main__.volume_conversion[7]>", line 1, in <module>
58+
volume_conversion(4, "wrongUnit", "litre")
59+
File "<string>", line 62, in volume_conversion
60+
ValueError: Invalid 'from_type' value: 'wrongUnit' Supported values are:
61+
cubicmeter, litre, kilolitre, gallon, cubicyard, cubicfoot, cup
62+
"""
63+
if from_type not in METRIC_CONVERSION:
64+
raise ValueError(
65+
f"Invalid 'from_type' value: {from_type!r} Supported values are:\n"
66+
+ ", ".join(METRIC_CONVERSION)
67+
)
68+
if to_type not in METRIC_CONVERSION:
69+
raise ValueError(
70+
f"Invalid 'to_type' value: {to_type!r}. Supported values are:\n"
71+
+ ", ".join(METRIC_CONVERSION)
72+
)
73+
return value * METRIC_CONVERSION[from_type].from_ * METRIC_CONVERSION[to_type].to
74+
75+
76+
if __name__ == "__main__":
77+
import doctest
78+
79+
doctest.testmod()

0 commit comments

Comments
 (0)