3. Improved Quotient Class. The function math.gcd takes two int values and returns the largest int that divides both. Fo

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

3. Improved Quotient Class. The function math.gcd takes two int values and returns the largest int that divides both. Fo

Post by answerhappygod »

3 Improved Quotient Class The Function Math Gcd Takes Two Int Values And Returns The Largest Int That Divides Both Fo 1
3 Improved Quotient Class The Function Math Gcd Takes Two Int Values And Returns The Largest Int That Divides Both Fo 1 (70.95 KiB) Viewed 24 times
3. Improved Quotient Class. The function math.gcd takes two int values and returns the largest int that divides both. For example, math.gcd (8,6) ⇒ 2, and math.gcd (9, 16) ⇒ 1. If a fraction is in lowest terms then math.gcd (a,b) 1. If not, it can be reduced to lowest terms by dividing a and b by math.gcd(a,b). For example, is in lowest terms, and math.gcd (4,3) ⇒ 1. On the other hand, is not in lowest terms, since math.gcd (8,6) ⇒ 2. But if we divide top and bottom by 2, we get, which is in lowest terms. Exercise Hint (1) Modify your Quotient class so it stores a fraction in lowest terms. For example, t = Quotient (8,6) check. expect ("Ts", str(t), "<4/3>") check.expect ("Tn", t.n. 4) check.expect ("Td", t.d, 3) a C ad + bc (2) Recalling that + b d -, implement the magic method_add_(self, other). It bd should return a new Quotient object representing the sum in lowest terms. 2 2 6 14 20 For example, since + 321 21 21 we can test: check.expect("+", Quotient (2,7) + Quotient (2,3), Quotient (20,21)) 5 13 18 3 Since 12 12 122. We can test that the answer is in lowest terms, like so: answer = Quotient (5, 12) + Quotient (13, 12) check.expect ("n", answer.n, 3) check.expect ("d", answer.d, 2) The definition of this function should start: def_add__(self, other: "Quotient") A few details: "Quotient": Make sure you always store int values, not floats. You may design your class so it only works with non-negative ints. Make sure_add returns a new Quotient object. It should not mutate self or other. After the due date for Assignment 07, we will provide a working solution which you may use as a start for this question.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply