#=======================================================================
# Author: Isai Damier
# Title: Division of Fractions
# Project: geekviewpoint
# Package: algorithms
#
# Statement:
# Divide the two given fractions
#
# Sample Input: [-5,6], [6,8])
# Sample Output: [-5,8]
#
# Technical Details:
# This is the same algorithm you learned in grade school:
# 1) cross multiply the numerators and the denominators
# 4) reduce the resulting fraction using the GCD (greatest common
# divisor) of its numerator and denominator.
#=======================================================================
def fractionDivision( a, b ):
numerator = a[0] * b[1]
denominator = a[1] * b[0]
num = 0 > numerator and -numerator or numerator
den = 0 > denominator and -denominator or denominator
gcd = GCD( num, den )
gcd = 0 > gcd and -gcd or gcd
return [numerator / gcd, denominator / gcd]
import unittest
from algorithms import numbers as algorithm
class Test( unittest.TestCase ):
def testFractionDivision( self ):
a = [8, 5]
b = [10, 15]
expResult = [12, 5]
result = algorithm.fractionDivision( a, b )
self.assertEquals( expResult, result )
a = [5, 6]
b = [6, 8]
expResult = [10, 9]
result = algorithm.fractionDivision( a, b )
self.assertEquals( expResult, result )