Question: I need to create class and function that use GCD on fraction immediately And this is how much I did so far def gcd(m, n):

I need to create class and function that use GCD on fraction immediately

And this is how much I did so far

def gcd(m, n): while m % n != 0: m, n = n, m % n return n

class Fraction: def __init__(self, top, bottom): self.num = top self.den = bottom

def __str__(self): return "{:d}/{:d}".format(self.num, self.den)

def __eq__(self, other_fraction): first_num = self.num * other_fraction.den second_num = other_fraction.num * self.den

return first_num == second_num

def __add__(self, other_fraction): new_num = self.num * other_fraction.den \ + self.den * other_fraction.num new_den = self.den * other_fraction.den cmmn = gcd(new_num, new_den) return Fraction(new_num // cmmn, new_den // cmmn)

def show(self): print("{:d}/{:d}".format(self.num, self.den)) def get_num(self): return self.num def get_den(self): return self.den

I am just hard stuck at how to add function that use gcd function and reduce top and bottom by gcd immediately before it prints out the answer.

So for example, if I have value that is 3/6, I need this thing to be 1/2 when I run z = Fraction(3,6) print(z)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!