Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use Python file Complete #1 through #5 for Introduction > Exercises Start

ID: 3697259 • Letter: P

Question

Please use Python file

Complete #1 through #5 for Introduction > Exercises

Start with this version of Fraction: Fraction.py (right click on name and save as, or save link as)

This file includes test code to test the Assignments Steps 1 to 5, make sure to read the comments in the file.

Upload the Fraction.py file here when you pass all the tests and exercise 5 code causes a exception that you throw.

Here is a copy of the book instructions 1 to 5:

Implement the simple methods getNum and getDen that will return the numerator and denominator of a fraction.

In many ways it would be better if all fractions were maintained in lowest terms right from the start. Modify the constructor for the Fraction class so that GCD is used to reduce fractions immediately. Notice that this means the __add__ function no longer needs to reduce. Make the necessary modifications.

Implement the remaining simple arithmetic operators (__sub__, __mul__, and __truediv__ for /).

Implement the remaining relational operators (__gt__, __ge__ , __lt__ , __le__, and __ne__)

Modify the constructor for the fraction class so that it checks to make sure that the numerator and denominator are both integers. If either is not an integer the constructor should raise an exception.

To find out more about the methods that start and end with two underscores, see the magic methods link in this weeks material.

Upload the Fraction.py file from above with all you code inserted using the submit area below when ready for grading.

Fraction.py

# add you name up here

# modify the fraction shown below

# NOTE, you should indent by using 4 spaces for each indent, and not tab

# and the gcd is different below than in the book you will need to call it as in the __add__ method

# we have made the gcd method part of the class so it is now called as self.gcd(n1,n2)

class Fraction:

  

def __init__(self,n,d):

self.num = n

self.den = d

  

def __repr__(self):

return str(self.num) + "/" + str(self.den)

def gcd(self,m,n):

while m % n != 0:

oldm = m

oldn = n

m = oldn

n = oldm % oldn

return n

def __add__(self,otherfraction):

newnum = self.num*otherfraction.den + self.den*otherfraction.num

newden = self.den * otherfraction.den

cd = self.gcd(newnum,newden) #### when you use gcd in __init__ you need to use self.gcd like here

return Fraction(newnum//cd,newden//cd)

  

def __eq__(self,otherFraction):

# note this only works if fractions are in lowest terms

return( self.num==otherFraction.num and self.den==otherFraction.den)

# note you can cut out all the test code below and move to a TestFraction.py file

# which will allow you to add your own tests to Fraction.py and still run the automated

# from the other file

# The code below will test each feature using unit testing

# the name of each test tells you what it is testing

# if a test fails you should se a trace dump. You will need to

# use the debugger or write print statements to debug you code.

#

# if everything passes you should see this printout

# .............

# ----------------------------------------------------------------------

# Ran 13 tests in 0.001s

#

# OK

import unittest

class TestFraction(unittest.TestCase):

  

def setUp(self):

self.f1123 = Fraction(11, 23)

self.f_43 = Fraction(-4, 3)

self.x = Fraction(1,2)

self.y = Fraction(3,4)

  

def test_eq_add_repr(self):

sum = self.f1123+self.f_43

self.assertTrue( str(sum) == "-59/69", "expected '-59/69' after add 11/23 + 4/3, found " + str(sum))

self.assertTrue( self.f1123 == Fraction(11, 23), "__eq__ failed here")

  

def test_get_num_den(self):

z = Fraction(1,3) # 1/3

self.assertTrue( z.getNum() == 1,"expected 1, found " + str(z.getNum()) )

self.assertTrue( z.getDen() == 3, "expected 3, found " + str(z.getDen()) )

  

def test_repr_works(self):

self.assertTrue( str(self.f1123) == "11/23", "expected 11/23 __repr__ returned " + str(self.f1123) )

self.assertTrue( str(self.f_43) == "-4/3", "expected -4/3 __repr__ returned " + str(self.f_43) )

  

def test_getNum(self):

self.assertTrue( self.f1123.getNum() == 11,"expected 11, found " + str(self.f1123.getNum()))

self.assertTrue( self.f_43.getNum() == -4, "expected -4, found "+ str(self.f1123.getNum()))

  

def test_getDen(self):

self.assertTrue( self.f1123.getDen() == 23, "expected 23, found " + str(self.f1123.getDen()))

self.assertTrue( self.f_43.getDen() == 3, "expected 3, found " + str(self.f_43.getDen()))

  

def test_mul(self):

x = str(Fraction(1,3) * Fraction(1,4))

self.assertTrue( x == "1/12","expected 1/12 result was " + x)

  

def test_sub(self):

x1 = str(Fraction(1,2) - Fraction(1,4))

self.assertTrue(x1 == "1/4", "expected 1/4 result was " + x1)

x2 = str(Fraction(5,16) - Fraction(2,16))

self.assertTrue(str(Fraction(5,16) - Fraction(2,16)) == "3/16", "expected 3/16 result was " + x2)

  

def test_div(self):

x1 = str(Fraction(1,2) / Fraction(1,2))

self.assertTrue(x1 == "1/1", "expected 1/1 result was " + x1)

x2 = str(Fraction(1,3) / Fraction(1,2))

self.assertTrue(x2 == "2/3", "expected 2/3 result was " + x2)

x3 = str(Fraction(4,15) / Fraction(4,1))

self.assertTrue(x3 == "1/15", "expected 1/15 result was " + x3)

  

def test_gt(self):

self.assertFalse(self.x > self.y, "x > y x: %s y: %s returned False:" % (str(self.x),str(self.y)))

self.assertTrue(self.y > self.x, "y > x x: %s y: %s returned False:" % (str(self.x),str(self.y)))

  

def test_ge(self):

self.assertTrue(self.x >= self.x, "x >= x x: %s returned False:" % (str(self.x)))

self.assertFalse(self.x >= self.y, "x >= y x: %s y: %s returned False:" % (str(self.x),str(self.y)))

self.assertTrue(self.y >= self.x, "y >= x x: %s y: %s returned False:" % (str(self.x),str(self.y)))

  

def test_le(self):

self.assertTrue(self.x <= self.y, "x <= y x: %s y: %s returned False:" % (str(self.x),str(self.y)))

self.assertTrue(self.x <= self.x, "x <= x x: %s returned False:" % (str(self.x)))

self.assertFalse(self.y <= self.x, "x <= y x: %s y: %s returned False:" % (str(self.x),str(self.y)))

  

def test_ne(self):

self.assertFalse(self.x != Fraction(1,2), " x != x x: %s returned False:" % (str(self.x)))

self.assertTrue(self.x != self.y, "x != y x: %s y: %s returned False:" % (str(self.x),str(self.y)))

  

def test_init_gcd(self):

z1 = Fraction(10,20)

self.assertTrue(str(z1) == "1/2", "expected 1/2 returned " + str(z1))

z2 = Fraction(1,4)

z3 = Fraction(3,4)

self.assertTrue(str(z2+z3) == "1/1", "expected 1/1 returned " + str(z2+z3))

  

def test_raise_non_int(self):

with self.assertRaises(Exception):

Fraction(1.1,0)

with self.assertRaises(Exception):

Fraction(0,23.4)

if __name__ == '__main__':

unittest.main()

Explanation / Answer

# add you name up here

# modify the fraction shown below

# NOTE, you should indent by using 4 spaces for each indent, and not tab

#       and the gcd is different below than in the book you will need to call it as in the __add__ method

#       we have made the gcd method part of the class so it is now called as self.gcd(n1,n2)

class Fraction:
  
  
  
   def __init__(self,n,d):
      
       self.num = n
       self.den = d

   def __repr__(self):
       return str(self.num) + "/" + str(self.den)
  
   def __eq__(self,otherFraction):
  
       # note this only works if fractions are in lowest terms
  
       return( self.num==otherFraction.num and self.den==otherFraction.den)
      
      
   def gcd(self,m,n):
        while m % n != 0:
          oldm = m
          oldn = n
          m = oldn
          n = oldm % oldn
      return n

   def __add__(self,otherfraction):
       newnum = self.num*otherfraction.den + self.den*otherfraction.num
       newden = self.den * otherfraction.den
       cd = self.gcd(newnum,newden) #### when you use gcd in __init__ you need to use self.gcd like here
       return Fraction(newnum//cd,newden//cd)

   def __sub__(self,otherfraction):
       newnum = self.num*otherfraction.den - self.den*otherfraction.num
       newden = self.den * otherfraction.den
       cd = self.gcd(newnum,newden) #### when you use gcd in __init__ you need to use self.gcd like here
       return Fraction(newnum//cd,newden//cd)
  
   def __mul__(self,otherfraction):
       newnum = self.num*otherfraction.num
       newden = self.den * otherfraction.den
       cd = self.gcd(newnum,newden)
       return Fraction(newnum//cd,newden//cd)
  
   def __truediv__(self,otherfraction):
       newnum = self.num*otherfraction.den
       newden = self.den * otherfraction.num
       cd = self.gcd(newnum,newden)
       return Fraction(newnum//cd,newden//cd)
  
   def __eq__(self,otherFraction):
  
       # note this only works if fractions are in lowest terms
  
       return( self.num==otherFraction.num and self.den==otherFraction.den)


   def __gt__(self,otherFraction):
      
       return ((self.num/self.den) > (otherFraction.num/otherFraction.den))

   def __ge__(self,otherFraction):
          
       return ((self.num/self.den) >= (otherFraction.num/otherFraction.den))

   def __lt__(self,otherFraction):
  
       return ((self.num/self.den) < (otherFraction.num/otherFraction.den))

   def __le__(self,otherFraction):
      
       return ((self.num/self.den) <= (otherFraction.num/otherFraction.den))

   def __ne__(self,otherFraction):
      
       return ((self.num/self.den) != (otherFraction.num/otherFraction.den))