Browse Source

added another testcase for addition of fractions with different denominators and updated code accordingly

remotes/origin/feature/feature-fractions
fdai7764 11 months ago
parent
commit
602fa8fb6b
  1. 10
      src/main/py/fractions.py
  2. 3
      src/test/py/test_fractions.py

10
src/main/py/fractions.py

@ -1,4 +1,4 @@
from src.main.py.more_advanced_calculations import gcd
from src.main.py.more_advanced_calculations import gcd, lcm
def frac_to_dec(num, denom):
return num/denom
@ -10,5 +10,13 @@ def reduceFraction(num, denom):
return (num, denom)
def addFractions(num1, denom1, num2, denom2):
if denom1 != denom2:
fractionExpansionFactor = lcm(denom1, denom2)
frac1Offset = fractionExpansionFactor / denom1
frac2Offset = fractionExpansionFactor /denom2
num1 *= frac1Offset
denom1 *= frac1Offset
num2 *= frac2Offset
denom2 *= frac2Offset
result_num = num1 + num2
return (result_num, denom1)

3
src/test/py/test_fractions.py

@ -21,6 +21,9 @@ class fractions(unittest.TestCase):
def test_add_fractions_1_by_5_and_2_by_5_should_be_3_by_5(self):
self.assertEqual(addFractions(1,5,2,5), (3,5))
def test_add_fractions_1_by_10_and_1_by_5_should_be_3_by_10(self):
self.assertEqual(addFractions(1,10,1,5), (3,10))
if __name__ == '__main__':
unittest.main()
Loading…
Cancel
Save