Skip to content

Commit 9d561ba

Browse files
committed
feat(day 120): add pounds-to-kilograms converter with grammatical handling and float-safe comparison
1 parent 6d50708 commit 9d561ba

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""
2+
Pounds to Kilograms
3+
Given a weight in pounds as a number, return the string "(lbs) pounds equals (kgs) kilograms.".
4+
5+
Replace "(lbs)" with the input number.
6+
Replace "(kgs)" with the input converted to kilograms, rounded to two decimals and always include two decimal places in the value.
7+
1 pound equals 0.453592 kilograms.
8+
If the input is 1, use "pound" instead of "pounds".
9+
If the converted value is 1, use "kilogram" instead of "kilograms".
10+
"""
11+
12+
import unittest
13+
14+
class PoundsToKilograms(unittest.TestCase):
15+
16+
def test1(self):
17+
self.assertEqual(convert_to_kgs(1), "1 pound equals 0.45 kilograms.")
18+
19+
def test2(self):
20+
self.assertEqual(convert_to_kgs(0),"0 pounds equals 0.00 kilograms.")
21+
22+
def test3(self):
23+
self.assertEqual(convert_to_kgs(100),"100 pounds equals 45.36 kilograms.")
24+
25+
def test4(self):
26+
self.assertEqual(convert_to_kgs(2.5), "2.5 pounds equals 1.13 kilograms.")
27+
28+
def test5(self):
29+
self.assertEqual(convert_to_kgs(2.20462),"2.20462 pounds equals 1.00 kilograms.")
30+
31+
32+
"""
33+
Subtle issue with this approach
34+
35+
The condition:
36+
if kilograms == 1:
37+
ddelimiter = "kilogram"
38+
39+
is fragile because kilograms is a float.
40+
41+
lbs = 2.20462
42+
kilograms = round(lbs * 0.453592, 2) # 1.0
43+
44+
Here kilograms == 1 is true, so you get "1.00 kilogram". That works
45+
46+
But if floating - point rounding gave 0.9999999 -> 1.0, it's fine.
47+
However, if you ever compare floats directly, it can be risky. A safer approach is to compare the formatted string.
48+
refining the below code.
49+
50+
"""
51+
def convert_to_kgs(lbs):
52+
53+
kilograms = round (lbs * 0.453592,2)
54+
55+
pdelimiter = "pounds"
56+
ddelimiter = "kilograms"
57+
58+
if lbs == 1:
59+
pdelimiter = "pound"
60+
if kilograms == 1:
61+
ddelimiter = "kilogram"
62+
63+
return f"{lbs} {pdelimiter} equals {kilograms:.2f} {ddelimiter}"
64+
65+
66+
def convert_to_kgs_refined(lbs):
67+
68+
kilograms = lbs * 0.453592
69+
kgs_str = f"{kilograms:.2f}"
70+
71+
pdelimiter = "pound" if lbs == 1 else "pounds"
72+
ddelimiter = "kilogram" if kgs_str == "1.00" else "kilograms"
73+
74+
return f"{lbs} {pdelimiter} equals {kgs_str} {ddelimiter}."
75+
76+
def convert_to_kgs(lbs):
77+
78+
kgs = lbs * 0.453592
79+
kgs_str = f"{kgs:.2f}" # always 2 decimals
80+
81+
pound_word = "pound" if lbs == 1 else "pounds"
82+
kilogram_word = "kilogram" if kgs == "1.00" else "kilograms"
83+
84+
return f"{lbs} {pound_word} equals {kgs_str} {kilogram_word}."
85+
86+
87+
if __name__ == "__main__":
88+
print(convert_to_kgs(50))
89+
unittest.main()

0 commit comments

Comments
 (0)