Question
I want the solution code for this function class Fraction We will implement a simple class representing a fractional data type that is immutable and
I want the solution code for this function class Fraction We will implement a simple class representing a fractional data type that is immutable and that supports basic arithmetic operations, simplification, conversion to real numbers, and string representation. An illustration of the usage of the class is shown below: >> f1, f2, f3, f4 = Fraction (14, -25), Fraction (7,9), Fraction (1,3), Fraction (6,5) >>> print (fl.get_fraction ()) (14, -25) >> print (f1) 14/-25 >> f5 = - f1 * (f2 + f3) / f4; print (f5) -2100.0/-4050.0 > f5.simplify(); print (5) 14.0/27.0 >> 5 () 0.5185185185185185 Note that Fraction class must raise Python's built-in exception ZeroDivisionError whenever the denom- inator is or becomes 0. Also note that the numerators and denominators are always integers 1. A fraction is represented internally as a numerator and denominator pair. The initialization method _init_(self, numerator, denominator) simply stores the numerator and the denominator for future use. It also raise an exception when denominator is zero. This method is implemented and provided for you. f = Fraction (2,4) >>> f.numerator. f. denominator (2, 4) > g = Fraction (3, 0) Traceback (most recent call last): File "", line 1, in module> g = Fraction (3, 0) File "/home/441/hwi.py", line 272, in _init_ raise ZeroDivisionError ('/' .format (numerator, denominator)) ZeroDivisionError: 3/0 2. The method._str_(self) returns a string representation of the fraction. This method will be calledby Python when str () function is called on a fraction or when a fraction is printed. No simplification should be performed on self. This method is implemented and provided for you. >> f, g = Fraction (8,12), Fraction (6,5) > str(f); str(f*g) '8/12 48/60' >> print (f); print (f*g) 8/12 48/60 3. Write a method get fraction (self) that returns a tuple of the numerator and the denominator. No simplification should be performed. > f = Fraction (2,4); f.get fraction () ; (2, 4) >> f = Fraction (2); f.get_fraction () (2, 1) >> f = Fraction (2, -3) ; f.get_fraction () (2, -3) 4. Write a protected method god(self, m, n) that calculates and returns the greatest common divisor (GCD) of the two integers m and n. Euclidean algorithm for CD is a simple recursive algorithm. For two integers a and b such that a b: if a is a multiple of b, then GCD of a and b is b otherwise, GCD of a and b is simply CD of b and a%b. This private method will be used by the method simplify (self) below. > f = Fraction (2, 4) ; f._ged(2, 4) f = Fraction (-2100, -4050) ; f._ged(-2100, -4050) -150 > f = Fraction (2000, -4050); f. ged (2000, -4050) -50 5. Write a method simplify (self) that replaces the fraction's internal representation with an equivalent but simplified represenation. You can simplify a fraction by dividing both numerator and denominator by GCD of them. Unlike other methods, simplify (self) does not return a new fraction, but rather changes the internal representation of self. Since the value of the fraction is not changing, we do not consider this as a violation on the notion of immutable data > f, g = Fraction (8, 12), Fraction (6,5) >> f.simplify (); f.get_fraction () (2.0, 3.0) > h = f * g; h.get fraction 0) (12.0, 15.0) > h.simplify(); h.get_fraction() (4.0, 5.0) 6. Write a method _neg-_(self) that returns a new fraction that is a negation of self. This method will be called by Python for unary negation. No simplification should be performed on the result. > f = Fraction (2,3) >> f.get _fraction ( (2,3) >>> g = -f; g. get fraction () (-2, 3) >>> f. get fraction () 2,3) >>> g = - (-f) ; g. get_fraction () (2, 3) 7. Write a method _add_(self, other) that returns a new fraction that is equal to the sum of self and other. Also write a method _sub_(self, other) that returns a new fraction that is equal to the difference between self and other. These methods will be called by Python for addition and subtraction.No simplification should be performed on the result. >> f, g = Fraction (2, 6), Fraction (2, 3) >> h = f + f; h.get_fraction () (4, 6) > h = f - g; h. get_fraction () (-6, 18) >>> h = g - g; h.get_fraction () (0, 3) 8. Write a method _mul_(self, other) that returns a new fraction that is equal to the product of self and other. Also write a method _truediv_(self, other) that returns a new fraction that is equal to the division of self by other. These methods will be called by Python for multiplication and division.No simplification should be performed on the result. > f, g, h = Fraction (2, 6), Fraction (2, 3), Fraction (0, 5) >> k = f * f; k.get fraction O (4,36) >>> k = f * g; k.get fraction () (4.18) >> k = f / f; k.get_fraction () (1212) >> k = f / g; k.get fraction O (6, 12) > k = f / h; k.get fraction () Traceback (most recent call last): File "Spyshell#158>", line 1, in k = f / h; k.get_fraction () File "/home/441/hwi.py", line 302, in _truediv_ return Fraction (self.numerator * other. denominator, File "/home/441/hw1.py", line 272, in _init_ raise ZeroDivisionError ('/' .format (numerator, denominator)) ZeroDivisionError: 10/0 9. Write a method _eq,-(self, other) that returns True if self is equal to other and False otherwise.Also write a method It__(self, other) that returns True if self is less than other and False otherwise. These methods will be called by Python for equality and less-than boolean operators. No simplification should be performed on the result. >> f, g = Fraction (1, 2), Fraction (2, 3) > f == f True >>>==g False > f < g True >>> g < f False 10. Write a method _call__(self) that returns the real number by evaluating self (ie., the result of dividing the numerator by the denominator. This method will be called by Python when a fraction is called as a function. No simplification should be performed on self. >> f, g, h = Fraction (2, 6), Fraction (2, 3), Fraction(0, 5) >> f0 0.3333333333333333 > g() 0.6666666666666666 >>> h() 0.0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started