-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathFraction.py
97 lines (68 loc) · 2.07 KB
/
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class Fraction:
"""docstring fos Fraction"""
def __init__(self, up,down):
x = gcd(up,down)
self.num = up/x
self.den = down/x
def show(self):
#print('%d/%d'%(self.num,self.den))
print self.num,"/",self.den
def __str__(self):
return str(self.num)+"/"+str(self.den)
def __add__(self,other):
sum_num = self.num*other.den + self.den*other.num
sum_den = self.den*other.den
temp = gcd(sum_num,sum_den)
return str(sum_num/temp)+'/'+str(sum_den/temp)
def __sub__(self,other):
sum_num = self.num*other.den - self.den*other.num
sum_den = self.den*other.den
temp = gcd(sum_num,sum_den)
return str(sum_num/temp)+'/'+str(sum_den/temp)
def __mul__(self,other):
sum_num = self.num*other.num
sum_den = self.den*other.den
temp = gcd(sum_num,sum_den)
return str(sum_num/temp)+'/'+str(sum_den/temp)
def __div__(self,other):
sum_num = self.num*other.den
sum_den = self.den*other.num
temp = gcd(sum_num,sum_den)
return str(sum_num/temp)+'/'+str(sum_den/temp)
def __eq__(self,other):
#to check deep equality I have overriden the __eq__ fn so that
#instead of checking reference it compares values and
#returns True if both matches
first_num = self.num*other.den
second_num = self.den*other.num
return first_num == second_num
def __lt__(self,other):
first_num = self.num*other.den
second_num = self.den*other.num
return first_num < second_num
def __le__(self,other):
first_num = self.num*other.den
second_num = self.den*other.num
return first_num <= second_num
def __gt__(self,other):
first_num = self.num*other.den
second_num = self.den*other.num
return first_num > second_num
def __ge__(self,other):
first_num = self.num*other.den
second_num = self.den*other.num
return first_num >= second_num
def __ne__(self,other):
first_num = self.num*other.den
second_num = self.den*other.num
return first_num != second_num
def get_num(self):
return(self.num)
def get_den(self):
return(self.den)
def gcd(m,n):
while (m % n != 0):
temp = m % n
m = n
n = temp
return n