05 - Fraction class

See the general homework guidelines!

You are going to write a simple class to represent fractions in Python. Place it in the module called fraction.py Your class should:

def __init__(self, numerator, denominator):
     self.numerator = numerator # top 
     self.denominator = denominator # bottom

# the output should be in the form: numerator/denominator
f1 = Fraction(2,3)
print(f1)
>>> 2/3
 
# a reduced fraction should be printed - a fraction in which the numerator and denominator are integers that have no other common divisors than 1.
f2 = Fraction(12,18)
print(f2)
>>> 2/3
 
# print only numerator in case the denominator is 1
f3 = Fraction(8,2)
print(f3)
>>> 4
 
# print initial ''-'' in case the fraction is negative. No other representations of negative fractions will be accepted.
f4 = Fraction(-2,3)
print(f4)
>>> -2/3
f5 = Fraction(2,-3)
print(f5)
>>> -2/3
f6 = Fraction(-2,-3)
print(f6)
>>> 2/3

f1 = Fraction(12,18)
f2 = Fraction(4,9)
print("f1 = " + str(f1))
print("f2 = " + str(f2))
print("f1 + f2 = " + str(f1+f2))
print("f1 - f2 = " + str(f1 - f2))
print("f1 * f2 = " + str(f1 * f2))
print("f1 / f2 = " + str(f1 / f2))
 
>>> f1 = 2/3
>>> f2 = 4/9
>>> f1 + f2 = 10/9
>>> f1 - f2 = 2/9
>>> f1 * f2 = 8/27
>>> f1 / f2 = 3/2

Grading

Subtask Max. points
Constructor - takes 2 arguments (ints), raise error if denominator is 0 2
__str__() method 2
overloading arithmetic operators 2
overloading comparison operators 2
clean documented code (function decomposition, following naming conventions, doc strings, etc.) 4
Total 12