-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFractions-class.py
More file actions
30 lines (25 loc) · 1.05 KB
/
Copy pathFractions-class.py
File metadata and controls
30 lines (25 loc) · 1.05 KB
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
def ident_denoms(num1, num2):
min_num = min(num1, num2)
while min_num:
if num1 % min_num == 0 and num2 % min_num == 0 and min_num > 1:
num1 = num1 / min_num
num2 = num2 / min_num
min_num = min(num1, num2)
else:
min_num -= 1
return int(num1), int(num2)
class Fraction(object):
def __init__(self, numerator, denominator):
numerator, denominator = ident_denoms(numerator, denominator)
self.numerator = numerator
self.denominator = denominator
def __repr__(self):
return '%s/%s' % (self.numerator, self.denominator)
def __add__(self, other):
numerator_new = self.numerator * other.denominator + other.numerator * self.denominator
denominator_new = self.denominator * other.denominator
return Fraction(numerator_new, denominator_new)
def __eq__(self, other):
check1 = self.numerator * other.denominator
check2 = other.numerator * self.denominator
return check1 == check2