Skip to content

Commit 7aa7b0a

Browse files
committed
Beautify fraction.py with black code beautifier
1 parent 3a796d0 commit 7aa7b0a

1 file changed

Lines changed: 46 additions & 52 deletions

File tree

‎_sources/Java4Python/fraction.py‎

Lines changed: 46 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,74 @@
11
class Fraction:
2-
3-
def __init__(self,top,bottom):
4-
5-
self.num = top #the numerator is on top
6-
self.den = bottom #the denominator is on the bottom
7-
2+
def __init__(self, num, den):
3+
"""
4+
:param num: The top of the fraction
5+
:param den: The bottom of the fraction
6+
"""
7+
self.num = num
8+
self.den = den
89

910
def __repr__(self):
1011
if self.num > self.den:
1112
retWhole = self.num / self.den
1213
retNum = self.num - (retWhole * self.den)
13-
return str(retWhole) + " " + str(retNum)+"/"+str(self.den)
14+
return str(retWhole) + " " + str(retNum) + "/" + str(self.den)
1415
else:
15-
return str(self.num)+"/"+str(self.den)
16+
return str(self.num) + "/" + str(self.den)
1617

1718
def show(self):
18-
print(self.num,"/",self.den)
19+
print(self.num, "/", self.den)
1920

20-
def __add__(self,otherfraction):
21+
def __add__(self, other):
2122
# convert to a fraction
22-
otherfraction = self.toFract(otherfraction)
23-
24-
newnum = self.num*otherfraction.den + self.den*otherfraction.num
25-
newden = self.den * otherfraction.den
26-
27-
common = gcd(newnum,newden)
28-
29-
return Fraction(newnum/common,newden/common)
30-
31-
def __radd__(self,leftNum):
32-
otherfraction = self.toFract(leftNum)
33-
newnum = self.num*otherfraction.den + self.den*otherfraction.num
34-
newden = self.den * otherfraction.den
35-
36-
common = gcd(newnum,newden)
37-
38-
return Fraction(newnum/common,newden/common)
39-
40-
def __cmp__(self,otherfraction):
41-
42-
num1 = self.num*otherfraction.den
43-
num2 = self.den*otherfraction.num
44-
23+
other = self.toFract(other)
24+
newnum = self.num * other.den + self.den * other.num
25+
newden = self.den * other.den
26+
common = gcd(newnum, newden)
27+
return Fraction(newnum / common, newden / common)
28+
29+
def __radd__(self, leftNum):
30+
other = self.toFract(leftNum)
31+
newnum = self.num * other.den + self.den * other.num
32+
newden = self.den * other.den
33+
common = gcd(newnum, newden)
34+
return Fraction(newnum / common, newden / common)
35+
36+
def __cmp__(self, other):
37+
num1 = self.num * other.den
38+
num2 = self.den * other.num
4539
if num1 < num2:
46-
return -1
40+
return -1
4741
else:
48-
if num1 == num2:
49-
return 0
50-
else:
51-
return 1
52-
53-
def toFract(self,n):
54-
if isinstance(n,int):
55-
otherfraction = Fraction(n,1)
42+
if num1 == num2:
43+
return 0
44+
else:
45+
return 1
46+
47+
def toFract(self, n):
48+
if isinstance(n, int):
49+
other = Fraction(n, 1)
5650
elif isinstance(n, float):
5751
wholePart = int(n)
5852
fracPart = n - wholePart
5953
# convert to 100ths???
6054
fracNum = int(fracPart * 100)
6155
newNum = wholePart * 100 + fracNum
62-
otherfraction = Fraction(newNum,100)
63-
elif isinstance(n,Fraction):
64-
otherfraction = n
56+
other = Fraction(newNum, 100)
57+
elif isinstance(n, Fraction):
58+
other = n
6559
else:
6660
print("Error: cannot add a fraction to a ", type(n))
6761
return None
68-
return otherfraction
62+
return other
6963

70-
#gcd is a helper function for Fraction
7164

72-
def gcd(m,n):
73-
while m%n != 0:
65+
def gcd(m, n):
66+
"""
67+
A helper function for Fraction
68+
"""
69+
while m % n != 0:
7470
oldm = m
7571
oldn = n
76-
7772
m = oldn
78-
n = oldm%oldn
79-
73+
n = oldm % oldn
8074
return n

0 commit comments

Comments
 (0)