-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds_fraction.py
74 lines (65 loc) · 2.24 KB
/
ds_fraction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from __future__ import print_function
from alg_gcd import compute_gcd
class Fraction(object):
"""Fraction class with print, show, add, subtract,
multiplication, division and equality.
W extend some of the above methods.
"""
def __init__(self, num, den):
gcd = compute_gcd(num, den)
self.num = num / gcd
self.den = den / gcd
def __str__(self):
return str(self.num) + '/' + str(self.den)
def show(self):
print(self.num, '/', self.den)
def __add__(self, other_frac):
new_num = self.num * other_frac.den + \
self.den * other_frac.num
new_den = self.den * other_frac.den
gcd = compute_gcd(new_num, new_den)
new_num = new_num // gcd
new_den = new_den // gcd
if new_num == new_den:
fracs_add = 1
else:
fracs_add = Fraction(new_num, new_den)
return fracs_add
def __sub__(self, other_frac):
new_num = self.num * other_frac.den - \
self.den * other_frac.num
new_den = self.den * other_frac.den
gcd = compute_gcd(new_num, new_den)
new_num = new_num // gcd
new_den = new_den // gcd
if new_num == new_den:
fracs_sub = 1
else:
fracs_sub = Fraction(new_num, new_den)
return fracs_sub
def __mul__(self, other_frac):
new_num = self.num * other_frac.num
new_den = self.den * other_frac.den
gcd = compute_gcd(new_num, new_den)
new_num = new_num // gcd
new_den = new_den // gcd
if new_num == new_den:
fracs_div = 1
else:
fracs_div = Fraction(new_num, new_den)
return fracs_div
def __div__(self, other_frac):
new_num = self.num * other_frac.den
new_den = self.den * other_frac.num
gcd = compute_gcd(new_num, new_den)
new_num = new_num // gcd
new_den = new_den // gcd
if new_num == new_den:
fracs_div = 1
else:
fracs_div = Fraction(new_num, new_den)
return fracs_div
def __eq__(self, other_frac):
first_num = self.num * other_frac.den
second_num = other_frac.num * self.den
return first_num == second_num