#!/usr/bin/env python3 """ fraction.py contains the Fraction class. @author @version """ class Fraction(): """ This class defines a fraction object in terms of its numerator and denominator. It also provides the ability to add fractions, and express them in reduced form (based on Greatest Common Denominator calculations). Woo-hoo! """ def __init__(self, numerator, denominator): self.num = numerator self.den = denominator if self.den < 0: self.den *= -1 self.num *= -1 def getNum(self): """Returns the numerator""" return self.num def getDen(self): """Returns the denominator""" return self.den def __str__(self): """Returns a string representation of the Fraction object""" return str(self.num) + "/" + str(self.den) def gcd(self, x, y): """ Gets greatest common divisor by Euclid's algorithm (iterated). """ while x % y != 0: x_initial = x y_initial = y x = y_initial y = x_initial % y_initial return y def __add__(self, other): """ a c ad bc ad + bc - + - = -- + -- = ------- b d bd bd bd Adds two fractions by this process: 1. Find a common denominator by multiplying 2. Add the numerators, and keep the common denominator 1. find the greatest common divisor of the numerator and denominator 2. divide both numerator and denominator by that gcd 3. return a Fraction object with that resulting value """ newNum = (self.num * other.den ) + (self.den * other.num) newDen = (self.den * other.den) the_gcd = self.gcd(newNum, newDen) # integer division below so fraction will be made of integers return Fraction(newNum // the_gcd, newDen // the_gcd) def __eq__(self, other): """ Returns True if the two fractions have the same value (reduced). """ def __sub__(self, other): """ Subtracts one fraction from the other using a similar process to addition. """ def __mul__(self, other): """Multiplying fractions is relatively easy...! """ def __truediv__(self, other): """"True division" returns a full result of the division (as opposed to "floor division" which returns an int result). Use the "invert and multiply" strategy. """ def main(): print("Testing the Fraction class") tests_passed = 0 try: f1 = Fraction(5,1) tests_passed += 1 print("Test passed: successfully created Fraction object.") except: print("Test failed: couldn't create Fraction object.") try: if f1.getNum() == 5 and f1.getDen() == 1: print("Test passed: getNum() and getDen() successful") tests_passed +=1 else: print("Test failed: getNum() and getDem() incorrectly implemented") except: print("Test failed: getNum() and getDen() not working") try: if f1.__str__() == "5/1": print("Test passed: string representation correct") tests_passed += 1 else: print("Test failed: __str__ found but incorrect result.") except: print("Test failed. No string representation.") try: f2 = Fraction(-2,3) f3 = Fraction(1,4) except: print("Couldn't create other fractions") try: f4 = Fraction(3,6) f5 = Fraction(1,4) if str(f4+f5) == "3/4": print("Test passed: 3/6 - 1/4 = 3/4") tests_passed += 1 else: print("Test failed: addition found, but incorrect result") except: print("Test failed: __add__ method not found or some other issue!") try: if str(f1-f2) == "17/3": print("Test passed: 5/1 - -2/3 = 17/3") tests_passed += 1 else: print("Test failed: subtraction found, but incorrect result") except: print("Test failed: __sub__ method not found") try: if str(f1 * f2) == "-10/3": print("Test passed: 5/1 * -2/3 = -10/3") tests_passed += 1 else: print("Test failed: multiplication found, but incorrect result") except: print("Test failed: __mul__ method not found") try: if str(f1 / f2) == "-15/2": print("Test passed: 5/1 / -2/3 = -15/2") tests_passed += 1 else: print("Test failed: division found, but incorrect result") except: print("Test failed: __truediv__ method not found") try: if Fraction(1,2) == Fraction(5,10): print("Test passed: 1/2 = 5/10") tests_passed += 1 else: print("Test failed: __eq__ found, but incorrect result") except: print("Test failed: __eq__ comparison not found") print("Tests passed = " + str(tests_passed) + "/8") if __name__ == "__main__": main()