-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfraction copy.py
More file actions
54 lines (44 loc) · 1.35 KB
/
fraction copy.py
File metadata and controls
54 lines (44 loc) · 1.35 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Fraction:
def __init__(self, numerator=0, denumerator=1):
if (not isinstance(numerator, int) or
not isinstance(denumerator, int)):
raise TypeError("The numerator & denumerator must be integers")
if denumerator == 0:
raise ZeroDivisionError("Zero denumerator")
if numerator == 0:
self._denumerator = 1
self._numerator = 0
else:
if(numerator < 0 and denumerator >= 0 or
numerator >=0 and denumerator < 0):
sign = -1
else:
sign = 1
# Euclid Algorithm
a = abs(numerator)
b = abs(denumerator)
while a % b != 0:
tempA = a
tempB = b
a = tempB
b = tempA % tempB
self._numerator = abs(numerator) // b * sign
self._denumerator = abs(denumerator)//b
def __repr__(self):
return (f'{self._numerator}/{self._denumerator}')
def __eq__(self, rhsValue):
return (self._numerator == rhsValue._numerator and
self._denumerator == rhsValue._denumerator)
def __add__(self, rhsValue):
if isinstance(rhsValue,int):
rhsFrac = Fraction(rhsValue, 1)
elif isinstance(rhsValue, Fraction):
rhsFrac = rhsValue
else:
raise TypeError("augment must be int or fraction")
num = (self._numerator * rhsFrac._denumerator +
self._denumerator*rhsFrac._numerator)
den = self._denumerator*rhsFrac._denumerator
return Fraction(num, den)
frac1 = Fraction(1,3)
print(frac1._numerator)