class Fraction: """ A basic mathematical fraction Instance variables: numerator denominator """ def __init__(self,top,bottom): self._numerator = top self._denominator = bottom def __str__(self): return str(self._numerator)+"/"+str(self._denominator) def __repr__(self): return "" def __add__(self,otherFraction): if self._denominator==otherFraction._denominator: newNumerator = self._numerator + otherFraction._numerator newDenominator = self._denominator else: newNumerator = self._numerator * otherFraction._denominator + \ otherFraction._numerator * self._denominator newDenominator = self._denominator*otherFraction._denominator return Fraction(newNumerator,newDenominator) def simplify(self): #Find the Greatest Common Divisor of the two numbers v1 = self._numerator v2 = self._denominator while v1%v2!=0: temp1 = v1 temp2 = v2 v1 = temp2 v2 = temp1%temp2 gcd = v2 #Now simply the two terms self._numerator = int(self._numerator/gcd) self._denominator = int(self._denominator/gcd) def __lt__(self,otherFraction): t1 = self._numerator*otherFraction._denominator t2 = otherFraction._numerator*self._denominator return t1t2 def __eq__(self,otherFraction): t1 = self._numerator*otherFraction._denominator t2 = otherFraction._numerator*self._denominator return t1==t2 def __le__(self,otherFraction): return not self>otherFraction def __ge__(self,otherFraction): return not self