#=======================================================================
# Author: Isai Damier
# Title: Addition of Fractions
# Project: geekviewpoint
# Package: algorithms
#
# Statement:
# Add the two given positive fractions
#
# Sample Input: [5,6], [7,8])
# Sample Output: [41,24]
#
# Technical Details:
# This is the same algorithm you learned in grade school:
# 1) find the lowest common denominator, which is the same as finding
# the lowest common multiplier of the given denominators.
# 2) Convert the given fractions into equivalent fractions with the
# new denominator
# 3) add the numerators
# 4) reduce the resulting fraction by dividing the GCD (greatest
# common divisor) of its numerator and denominator.
#=======================================================================
def addingFractions( a, b ):
denominator = LCM( a[1], b[1] )
numA = denominator / a[1] * a[0]
numB = denominator / b[1] * b[0]
numerator = numA + numB
gcd = GCD( numerator, denominator )
return [numerator / gcd, denominator / gcd]
import unittest
from algorithms import numbers as algorithm
class Test( unittest.TestCase ):
def testAddingFractions( self ):
a = [5, 6]
b = [7, 8]
expResult = [41, 24]
result = algorithm.addingFractions( a, b )
self.assertEquals( expResult, result )