#=======================================================================
# Author: Isai Damier
# Title: Multiplication of Fractions
# Project: geekviewpoint
# Package: algorithms
#
# Statement:
# Multiply 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) multiply the numerators
# 2) multiply the denominators
# 4) reduce the resulting fraction using the GCD (greatest common
# divisor) of its numerator and denominator.
#=======================================================================
def fractionMultiplication( a, b ):
numerator = a[0] * b[0]
denominator = a[1] * b[1]
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 testFractionMultiplication( self ):
expResult = [16, 15]
result = algorithm.fractionMultiplication( [8, 5], [10, 15] )
self.assertEquals( expResult, result )
expResult = [-5, 8]
result = algorithm.fractionMultiplication( [-5, 6], [6, 8] )
self.assertEquals( expResult, result )