|
1 | 1 | 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 |
8 | 9 |
|
9 | 10 | def __repr__(self): |
10 | 11 | if self.num > self.den: |
11 | 12 | retWhole = self.num / self.den |
12 | 13 | retNum = self.num - (retWhole * self.den) |
13 | | - return str(retWhole) + " " + str(retNum)+"/"+str(self.den) |
| 14 | + return str(retWhole) + " " + str(retNum) + "/" + str(self.den) |
14 | 15 | else: |
15 | | - return str(self.num)+"/"+str(self.den) |
| 16 | + return str(self.num) + "/" + str(self.den) |
16 | 17 |
|
17 | 18 | def show(self): |
18 | | - print(self.num,"/",self.den) |
| 19 | + print(self.num, "/", self.den) |
19 | 20 |
|
20 | | - def __add__(self,otherfraction): |
| 21 | + def __add__(self, other): |
21 | 22 | # 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 |
45 | 39 | if num1 < num2: |
46 | | - return -1 |
| 40 | + return -1 |
47 | 41 | 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) |
56 | 50 | elif isinstance(n, float): |
57 | 51 | wholePart = int(n) |
58 | 52 | fracPart = n - wholePart |
59 | 53 | # convert to 100ths??? |
60 | 54 | fracNum = int(fracPart * 100) |
61 | 55 | 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 |
65 | 59 | else: |
66 | 60 | print("Error: cannot add a fraction to a ", type(n)) |
67 | 61 | return None |
68 | | - return otherfraction |
| 62 | + return other |
69 | 63 |
|
70 | | -#gcd is a helper function for Fraction |
71 | 64 |
|
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: |
74 | 70 | oldm = m |
75 | 71 | oldn = n |
76 | | - |
77 | 72 | m = oldn |
78 | | - n = oldm%oldn |
79 | | - |
| 73 | + n = oldm % oldn |
80 | 74 | return n |
0 commit comments