|
26 | 26 |
|
27 | 27 | from_to = namedtuple("from_to", "from_ to")
|
28 | 28 |
|
| 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 | + |
29 | 42 | 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), |
39 | 51 | }
|
40 | 52 |
|
41 | 53 |
|
42 | 54 | def length_conversion(value: float, from_type: str, to_type: str) -> float:
|
43 | 55 | """
|
44 | 56 | Conversion between length units.
|
45 | 57 |
|
46 |
| - >>> length_conversion(4, "meter", "feet") |
| 58 | + >>> length_conversion(4, "METER", "FEET") |
| 59 | + 13.12336 |
| 60 | + >>> length_conversion(4, "M", "FT") |
47 | 61 | 13.12336
|
48 | 62 | >>> length_conversion(1, "meter", "kilometer")
|
49 | 63 | 0.001
|
@@ -73,29 +87,33 @@ def length_conversion(value: float, from_type: str, to_type: str) -> float:
|
73 | 87 | 36.00001944
|
74 | 88 | >>> length_conversion(4, "mile", "kilometer")
|
75 | 89 | 6.43736
|
76 |
| - >>> length_conversion(2, "mile", "inch") |
| 90 | + >>> length_conversion(2, "miles", "InChEs") |
77 | 91 | 126719.753468
|
78 | 92 | >>> length_conversion(3, "millimeter", "centimeter")
|
79 | 93 | 0.3
|
80 |
| - >>> length_conversion(3, "millimeter", "inch") |
| 94 | + >>> length_conversion(3, "mm", "in") |
81 | 95 | 0.1181103
|
82 | 96 | >>> length_conversion(4, "wrongUnit", "inch")
|
83 | 97 | Traceback (most recent call last):
|
84 | 98 | ...
|
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 |
87 | 101 | """
|
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: |
89 | 107 | 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)}" |
92 | 110 | )
|
93 |
| - if to_type not in METRIC_CONVERSION: |
| 111 | + if new_to not in METRIC_CONVERSION: |
94 | 112 | 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)}" |
97 | 115 | )
|
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 |
99 | 117 |
|
100 | 118 |
|
101 | 119 | if __name__ == "__main__":
|
|
0 commit comments