-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
134 lines (109 loc) · 4.06 KB
/
test.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
# Fraction class
def gcd(m,n):
while m%n != 0:
oldm = m
oldn = n
m = oldn
n = oldm%oldn
return n
class Fraction:
# #2 solution for exercise 2 from 1.7 module chap-1
def __init__(self, top, bottom):
# #5 solution for exercise 5 from 1.7 module - chap-1
if not isinstance(top, int):
valErr = ValueError("{} is not integer".format(top))
raise valErr
if not isinstance(bottom, int):
valErr = ValueError("{} is not integer".format(bottom))
raise valErr
# #6 solution for exercise 6 from 1.7 module - chap-1
if top < 0 and bottom < 0:
top = abs(top)
bottom = abs(bottom)
elif bottom < 0:
top = -top
bottom = abs(bottom)
common = gcd(abs(top), abs(bottom))
self.num = top // common
self.den = bottom // common
def __str__(self):
return str(self.num) + "/" + str(self.den)
# #9 solution for exercise 9 from 1.7 module chap-1
def __repr__(self):
return '%s(%r)' % (self.__class__, self.__str__())
def show(self):
print (self.num, "/", self.den)
# #1 solution for programming exercise 1 from 1.7 modude of chap1
def get_num(self):
return self.num
# #1 solution for programming exercise 1 from 1.7 modude of chap1
def get_den(self):
return self.den
# #2 solution for exercise 2 from 1.7 module chap-1
def __add__(self, other):
new_num = (self.num * other.den) + (self.den * other.num)
new_den = self.den * other.den
return Fraction(new_num, new_den)
# common = gcd(new_num, new_den)
# return Fraction(new_num // common, new_den // common)
# #7 solution for exercise 7 from 1.7 module chap-1
def __radd__(self, other):
if other == 0:
return self
else:
other = Fraction(other, 1)
return self.__add__(other)
# #8 solution for exercise 7 from 1.7 module chap-1
def __iadd__(self, other):
if isinstance(other, int):
other = Fraction(other, 1)
return self.__add__(other)
def __eq__(self, other):
first_num = self.num * other.den
second_num = self.den * other.num
return first_num == second_num
# #4 solution for problem 4 for 1.7 module chap-1
def __ne__(self, other):
first_num = self.num * other.den
second_num = self.den * other.num
return first_num != second_num
# #4 solution for problem 4 for 1.7 module chap-1
def __gt__(self, other):
first_num = self.num * other.den
second_num = self.den * other.num
return first_num > second_num
# #4 solution for problem 4 for 1.7 module chap-1
def __ge__(self, other):
first_num = self.num * other.den
second_num = self.den * other.num
return first_num >= second_num
# #4 solution for problem 4 for 1.7 module chap-1
def __lt__(self, other):
first_num = self.num * other.den
second_num = self.den * other.num
return first_num < second_num
# #4 solution for problem 4 for 1.7 module chap-1
def __le__(self, other):
first_num = self.num * other.den
second_num = self.den * other.num
return first_num <= second_num
# #3 solution for exercise 3 from 1.7 module chap-1
def __mul__(self, other):
num = self.num * other.num
den = self.den * other.den
return Fraction(num, den)
# #3 solution for exercise 3 from 1.7 module chap-1
def __truediv__(self, other):
num = self.num * other.den
den = self.den * other.num
common = gcd(abs(num), abs(den))
return Fraction(num // common, den // common)
# #3 solution for exercise 3 from 1.7 module chap-1
def __sub__(self, other):
num = (self.num * other.den) - (self.den * other.num)
den = self.den * other.den
common = gcd(abs(num), abs(den))
return Fraction(num // common, den // common)
x = Fraction(a, 3)
y = Fraction(2, 3)
print(x + y)