-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fractions.py
221 lines (176 loc) · 6.4 KB
/
Fractions.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
""" a custom fractions class """
class Fraction:
""" a class for defining fractions """
def __init__(self, top, bottom):
""" top is numerator and bottom is denominator """
self.top = top
if bottom == 0:
raise ZeroDivisionError
self.bottom = bottom
# overloads
def __str__(self):
""" prints fraction """
self.simplify()
if self.bottom == 1:
return f"{int(self.top)}"
return f"{int(self.top)}/{int(self.bottom)}"
def __add__(self, rhs):
""" overloads + """
if isinstance(rhs, int):
rhs = Fraction(rhs, 1)
elif isinstance(rhs, float):
return self.to_float() + rhs
lcm = int(self.lcm(self.bottom, rhs.bottom))
self.lcm_apply(lcm, self, rhs)
return Fraction(self.top + rhs.top, self.bottom)
def __radd__(self, lhs):
""" overloads + but backwards b/c of commutativity """
return self.__add__(lhs)
def __sub__(self, rhs):
""" overloads - """
if isinstance(rhs, int):
rhs = Fraction(rhs, 1)
elif isinstance(rhs, float):
return self.to_float() - rhs
lcm = self.lcm( self.bottom, rhs.bottom)
self.lcm_apply(lcm, self, rhs)
return Fraction(self.top - rhs.top, self.bottom)
def __rsub__(self, lhs):
""" overloads - but opposite """
if isinstance(lhs, int):
lhs = Fraction(lhs, 1)
elif isinstance(lhs, float):
return lhs - self.to_float()
lcm = self.lcm( self.bottom, lhs.bottom)
self.lcm_apply(lcm, self, lhs)
return Fraction(lhs.top - self.top, self.bottom)
def __mul__(self, rhs):
""" overloads * """
if isinstance(rhs, int):
rhs = Fraction(rhs, 1)
elif isinstance(rhs, float):
return self.to_float() * rhs
return Fraction(self.top * rhs.top, self.bottom * rhs.bottom)
def __rmul__(self, lhs):
""" overloads * but backwards """
return self.__mul__(lhs)
def __truediv__(self, rhs):
""" overloads / """
if isinstance(rhs, int):
rhs = Fraction(rhs, 1)
elif isinstance(rhs, float):
return self.to_float() / rhs
return Fraction(self.top * rhs.bottom, self.bottom * rhs.top)
def __rtruediv__(self, lhs):
""" overloads / but backwards """
if isinstance(lhs, int):
lhs = Fraction(lhs, 1)
elif isinstance(lhs, float):
return lhs / self.to_float()
return Fraction(self.bottom * lhs.top, self.top * lhs.bottom)
def __pow__(self, rhs):
""" overloads ** """
return self.to_float() ** rhs
def __rpow__(self, lhs):
""" overloads ** but backwards """
return self.__pow__(lhs)
# comparisons
def __lt__(self, other):
if isinstance(other, Fraction):
return self.to_float() < other.to_float()
return self.to_float() < other
def __rlt__(self, lhs):
if isinstance(lhs, Fraction):
return self.to_float() > lhs.to_float()
return self.to_float() > lhs
def __le__(self, other):
if isinstance(other, Fraction):
return self.to_float() <= other.to_float()
return self.to_float() <= other
def __rle__(self, lhs):
if isinstance(lhs, Fraction):
return self.to_float() >= lhs.to_float()
return self.to_float() >= lhs
def __gt__(self, other):
if isinstance(other, Fraction):
return self.to_float() > other.to_float()
return self.to_float() > other
def __rgt__(self, lhs):
if isinstance(lhs, Fraction):
return self.to_float() < lhs.to_float()
return self.to_float() < lhs
def __ge__(self, other):
if isinstance(other, Fraction):
return self.to_float() >= other.to_float()
return self.to_float() >= other
def __rge__(self, lhs):
if isinstance(lhs, Fraction):
return self.to_float() <= lhs.to_float()
return self.to_float() <= lhs
def __eq__(self, other):
if isinstance(other, Fraction):
return self.to_float() == other.to_float()
return self.to_float() == other
def __req__(self, lhs):
return self.__eq__(lhs)
def __ne__(self, other):
if isinstance(other, Fraction):
return self.to_float() != other.to_float()
return self.to_float() != other
def __rne__(self, lhs):
return self.__ne__(lhs)
# end of overloads
def simplify(self):
""" simplifies the fraction """
gcd = self.gcd(self.top, self.bottom)
if not gcd == max(abs(self.top), abs(self.bottom)):
self.top = int(self.top/gcd)
self.bottom = int(self.bottom/gcd)
def lcm(self, a, b):
""" calculates lcm """
return (a*b)/self.gcd(a,b)
def gcd(self, first, second):
""" gets gcd from first and second number """
if second == 0:
return first
return self.gcd(second, first % second)
def lcm_apply(self, lcm, *argv):
""" applies the lcm """
for fraction in argv:
if lcm == fraction.bottom:
continue
fraction.top *= int(lcm/fraction.bottom)
fraction.bottom = lcm
def to_float(self):
""" converts fraction to decimal """
return self.top/self.bottom
def strip_mult(*args):
""" converts a variable amount of args to be striped of whitespace """
temp = []
for element in args:
temp.append(element.strip())
return temp
def parse(*args):
""" parses through a set of strings and returns the right type """
message = 0
inputs = []
for index, input_ in enumerate(args):
if input_.isdigit():
inputs.append(int(input_))
message = 1
else:
try:
inputs.append(float(input_))
message = 1
except ValueError:
fraction = []
input_ = input_.split("/")
if input_[1] == 0:
return "zero d", 0, 0
for number in input_:
fraction.append(int(number.strip()))
inputs.append(Fraction(fraction[0], fraction[1]))
message = 1
else:
message = str(index)
return message, inputs[0], inputs[1]