Skip to content

Commit f780433

Browse files
cclaussgithub-actions
and
github-actions
authored
length_conversion.py: Deal with uppercase and abbreviations (#5433)
* length_conversion.py: Deal with uppercase and abbreviations * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 4880931 commit f780433

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

DIRECTORY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
* [Decimal To Octal](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_octal.py)
114114
* [Hex To Bin](https://github.com/TheAlgorithms/Python/blob/master/conversions/hex_to_bin.py)
115115
* [Hexadecimal To Decimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/hexadecimal_to_decimal.py)
116-
* [Length Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/length_conversions.py)
116+
* [Length Conversion](https://github.com/TheAlgorithms/Python/blob/master/conversions/length_conversion.py)
117117
* [Molecular Chemistry](https://github.com/TheAlgorithms/Python/blob/master/conversions/molecular_chemistry.py)
118118
* [Octal To Decimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/octal_to_decimal.py)
119119
* [Prefix Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/prefix_conversions.py)

conversions/length_conversions.py renamed to conversions/length_conversion.py

+39-21
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,38 @@
2626

2727
from_to = namedtuple("from_to", "from_ to")
2828

29+
TYPE_CONVERSION = {
30+
"millimeter": "mm",
31+
"centimeter": "cm",
32+
"meter": "m",
33+
"kilometer": "km",
34+
"inch": "in",
35+
"inche": "in", # Trailing 's' has been stripped off
36+
"feet": "ft",
37+
"foot": "ft",
38+
"yard": "yd",
39+
"mile": "mi",
40+
}
41+
2942
METRIC_CONVERSION = {
30-
"meter": from_to(1, 1),
31-
"kilometer": from_to(1000, 0.001),
32-
"feet": from_to(0.3048, 3.28084),
33-
"inch": from_to(0.0254, 39.3701),
34-
"centimeter": from_to(0.01, 100),
35-
"yard": from_to(0.9144, 1.09361),
36-
"foot": from_to(0.3048, 3.28084),
37-
"mile": from_to(1609.34, 0.000621371),
38-
"millimeter": from_to(0.001, 1000),
43+
"mm": from_to(0.001, 1000),
44+
"cm": from_to(0.01, 100),
45+
"m": from_to(1, 1),
46+
"km": from_to(1000, 0.001),
47+
"in": from_to(0.0254, 39.3701),
48+
"ft": from_to(0.3048, 3.28084),
49+
"yd": from_to(0.9144, 1.09361),
50+
"mi": from_to(1609.34, 0.000621371),
3951
}
4052

4153

4254
def length_conversion(value: float, from_type: str, to_type: str) -> float:
4355
"""
4456
Conversion between length units.
4557
46-
>>> length_conversion(4, "meter", "feet")
58+
>>> length_conversion(4, "METER", "FEET")
59+
13.12336
60+
>>> length_conversion(4, "M", "FT")
4761
13.12336
4862
>>> length_conversion(1, "meter", "kilometer")
4963
0.001
@@ -73,29 +87,33 @@ def length_conversion(value: float, from_type: str, to_type: str) -> float:
7387
36.00001944
7488
>>> length_conversion(4, "mile", "kilometer")
7589
6.43736
76-
>>> length_conversion(2, "mile", "inch")
90+
>>> length_conversion(2, "miles", "InChEs")
7791
126719.753468
7892
>>> length_conversion(3, "millimeter", "centimeter")
7993
0.3
80-
>>> length_conversion(3, "millimeter", "inch")
94+
>>> length_conversion(3, "mm", "in")
8195
0.1181103
8296
>>> length_conversion(4, "wrongUnit", "inch")
8397
Traceback (most recent call last):
8498
...
85-
ValueError: Invalid 'from_type' value: 'wrongUnit' Supported values are:
86-
meter, kilometer, feet, inch, centimeter, yard, foot, mile, millimeter
99+
ValueError: Invalid 'from_type' value: 'wrongUnit'.
100+
Conversion abbreviations are: mm, cm, m, km, in, ft, yd, mi
87101
"""
88-
if from_type not in METRIC_CONVERSION:
102+
new_from = from_type.lower().rstrip("s")
103+
new_from = TYPE_CONVERSION.get(new_from, new_from)
104+
new_to = to_type.lower().rstrip("s")
105+
new_to = TYPE_CONVERSION.get(new_to, new_to)
106+
if new_from not in METRIC_CONVERSION:
89107
raise ValueError(
90-
f"Invalid 'from_type' value: {from_type!r} Supported values are:\n"
91-
+ ", ".join(METRIC_CONVERSION)
108+
f"Invalid 'from_type' value: {from_type!r}.\n"
109+
f"Conversion abbreviations are: {', '.join(METRIC_CONVERSION)}"
92110
)
93-
if to_type not in METRIC_CONVERSION:
111+
if new_to not in METRIC_CONVERSION:
94112
raise ValueError(
95-
f"Invalid 'to_type' value: {to_type!r}. Supported values are:\n"
96-
+ ", ".join(METRIC_CONVERSION)
113+
f"Invalid 'to_type' value: {to_type!r}.\n"
114+
f"Conversion abbreviations are: {', '.join(METRIC_CONVERSION)}"
97115
)
98-
return value * METRIC_CONVERSION[from_type].from_ * METRIC_CONVERSION[to_type].to
116+
return value * METRIC_CONVERSION[new_from].from_ * METRIC_CONVERSION[new_to].to
99117

100118

101119
if __name__ == "__main__":

0 commit comments

Comments
 (0)