-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoop.py
169 lines (120 loc) · 4.33 KB
/
oop.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding: utf-8 -*-
"""OOP.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/
"""
# Fraction Class
class Fraction:
def __init__(self, numerator, denominator):
self.__numerator = numerator
self.__denominator = denominator
def __str__(self):
return str(self.__numerator) + '/' + str(self.__denominator)
def __repr__(self):
return str(self.__numerator) + '/' + str(self.__denominator)
def getNumerator(self):
return self.__numerator
def getDenominator(self):
return self.__denominator
def get(self):
return (self.getNumerator(), self.getDenominator())
def setNumerator(self, value):
self.__numerator = value
def setDenominator(self, value):
if value == 0:
raise ValueError('Divide by Zero Error')
self.__denominator = value
def set(self, numer, denom):
self.setNumerator(numer)
self.setDenominator(denom)
def reduce(self):
gcd = self.__calcGCD()
self.__numerator = self.__numerator // gcd
self.__denominator = self.__denominator // gcd
def __calcGCD(self):
a = max(abs(self.__numerator), abs(self.__denominator))
b = min(abs(self.__numerator), abs(self.__denominator))
while b != 0:
temp = b
b = a % b
a = temp
return a
def __adjust(self, factor):
self.setNumerator(self.getNumerator() * factor)
self.setDenominator(self.getDenominator() * factor)
def copy(self):
new_frac = Fraction(self.__numerator, self.__denominator)
return new_frac
def __neg__(self):
return Fraction(-self.__numerator, self.__denominator)
def __add__(self, rfraction):
numer = self.__numerator * rfraction.getDenominator() + \
rfraction.getNumerator() * self.__denominator
denom = self.__denominator * rfraction.getDenominator()
resultFrac = Fraction(numer, denom)
resultFrac.reduce()
return resultFrac
def __sub__(self, rfraction):
return self + (-rfraction)
def __mul__(self, rfraction):
numer = self.__numerator * rfraction.getNumerator()
denom = self.__denominator * rfraction.getDenominator()
resultFrac = Fraction(numer, denom)
resultFrac.reduce()
return resultFrac
def __eq__(self, rfraction):
temp_frac1 = self.copy()
temp_frac2 = rfraction.copy()
temp_frac1.reduce()
temp_frac2.reduce()
if temp_frac1.getNumerator() == temp_frac2.getNumerator() and \
temp_frac1.getDenominator() == temp_frac2.getDenominator():
return True
else:
return False
def __neq__(self, rfraction):
return not self.__eq__(rfraction)
def __lt__(self, rfraction):
lessthan = False
temp_frac1 = self.copy()
temp_frac2 = rfraction.copy()
temp_frac1.__adjust(temp_frac2.getDenominator())
temp_frac2.__adjust(temp_frac1.getDenominator())
if temp_frac1.getNumerator() < temp_frac2.getNumerator():
lessthan = True
return lessthan
def __le__(self, rfraction):
if self == rfraction or self < rfraction:
return True
else:
return False
def __gt__(self, rfraction):
if not(self <= rfraction):
return True
else:
return False
def __ge__(self, rfraction):
if not(self < rfraction):
return True
else:
return False
#Write the code that use every built-in function of fraction at least once.
frac1 = Fraction(3,4)
frac2 = Fraction(5,7)
print(frac1, frac2) #__str__
print(frac1.get()) #getNumerator, getDenominator, get
frac2.set(5,15) #setNumberator, setDenominator, set
frac2.reduce() #reduce, __calcGCD
print(frac2)
print(-frac1) #__neg__
print(frac1+frac2) #__add__
print(frac1-frac2) #__sub__
print(frac1*frac2) #__mul__
print(frac1==frac2) #__eq__, copy
print(frac1!=frac2) #__neq__
print(frac1<frac2) #__lt__, __adjust
print(frac1<=frac2) #__le__
print(frac1>frac2) #__gt__
print(frac1>=frac2) #__ge__
frac1, frac2 #__repr__