Skip to content

Commit 9fcb4a3

Browse files
committed
update the chapter1 exerices solution
1 parent df5b58e commit 9fcb4a3

File tree

1 file changed

+60
-9
lines changed

1 file changed

+60
-9
lines changed

Chapter1_Introduction/fraction.py

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
class Fraction:
22

33
def __init__(self, top, bottom):
4-
self.num = top
5-
self.den = bottom
4+
if type(top) is not int or type(bottom) is not int:
5+
raise ValueError("Please only use integers")
6+
else:
7+
common = gcd(top, bottom)
8+
self.num = top // common
9+
self.den = bottom // common
610

711
def show(self):
812
print(self.num, '/', self.den)
@@ -13,14 +17,53 @@ def __str__(self):
1317
def __add__(self, other):
1418
newnum = self.num * other.den + other.num * self.den
1519
newden = self.den * other.den
16-
common = gcd(newnum, newden)
17-
return Fraction(newnum // common, newden // common)
20+
#common = gcd(newnum, newden)
21+
#return Fraction(newnum // common, newden // common)
22+
return Fraction(newnum, newden)
23+
24+
def __sub__(self, other):
25+
newnum = self.num * other.den - other.num * self.den
26+
newden = self.den * other.den
27+
return Fraction(newnum, newden)
28+
29+
def __mul__(self, other):
30+
return Fraction(self.num * other.num, self.den * other.den)
31+
32+
def __truediv__(self, other):
33+
newnum = self.num * other.den
34+
newden = self.den * other.num
35+
return newnum / newden
1836

1937
def __eq__(self, other):
2038
firstnum = self.num * other.den
2139
secondnum = self.den * other.num
2240
return firstnum == secondnum
2341

42+
def __gt__(self, other):
43+
firstnum = self.num * other.den
44+
secondnum = self.den * other.num
45+
return firstnum > secondnum
46+
47+
def __ge__(self, other):
48+
firstnum = self.num * other.den
49+
secondnum = self.den * other.num
50+
return firstnum >= secondnum
51+
52+
def __lt__(self, other):
53+
firstnum = self.num * other.den
54+
secondnum = self.den * other.num
55+
return firstnum < secondnum
56+
57+
def __le__(self, other):
58+
firstnum = self.num * other.den
59+
secondnum = self.den * other.num
60+
return firstnum <= secondnum
61+
62+
def __ne__(self, other):
63+
firstnum = self.num * other.den
64+
secondnum = self.den * other.num
65+
return firstnum != secondnum
66+
2467
def getNum(self):
2568
return self.num
2669

@@ -40,10 +83,18 @@ def gcd(m,n):
4083
return n
4184

4285

43-
86+
#z = Fraction(1.5, 2.3)
4487
x = Fraction(1,2)
4588
y = Fraction(2,3)
46-
print(x+y)
47-
print(x == y)
48-
print(x.getNum())
49-
print(x.getDen())
89+
print("{0} grater than {1}? {2}".format(x, y, x > y))
90+
print("{0} grater or equal to {1}? {2}".format(x, y, x >= y))
91+
print("{0} less than {1}? {2}".format(x, y, x < y))
92+
print("{0} less or equal to {1}? {2}".format(x, y, x <= y))
93+
print("{0} not equal to {1}? {2}".format(x, y, x != y))
94+
print("sum is:", x+y)
95+
print("true divsion is:", x/y)
96+
print("difference is:", x-y)
97+
print("product is:", x*y)
98+
print("equality is:", x == y)
99+
print("numerator is:", x.getNum())
100+
print("denominator is:", x.getDen())

0 commit comments

Comments
 (0)