new methods for Fraction __radd__ __rsub__ __r*__
This commit is contained in:
parent
9dfc0b9c47
commit
ceab817c28
@ -94,6 +94,42 @@ class Fraction(object):
|
||||
|
||||
return steps
|
||||
|
||||
def __radd__(self, other):
|
||||
if type(other) == Fraction:
|
||||
#cool
|
||||
number = other
|
||||
else:
|
||||
number = Fraction(other)
|
||||
|
||||
steps = []
|
||||
|
||||
if number._denom == self._denom:
|
||||
com_denom = number._denom
|
||||
num1 = number._num
|
||||
num2 = self._num
|
||||
|
||||
else:
|
||||
gcd_denom = gcd(number._denom, self._denom)
|
||||
coef1 = self._denom // gcd_denom
|
||||
coef2 = number._denom // gcd_denom
|
||||
|
||||
steps.append([number._num, coef1, "*", number._denom, coef1, "*", '/', self._num, coef2, "*", self._denom, coef2, "*", "/",'+'])
|
||||
|
||||
com_denom = number._denom * coef1
|
||||
num1 = number._num * coef1
|
||||
num2 = self._num * coef2
|
||||
|
||||
steps.append([num1, num2, '+', com_denom, '/'])
|
||||
|
||||
num = num1 + num2
|
||||
|
||||
ans_frac = Fraction(num, com_denom)
|
||||
steps.append(ans_frac)
|
||||
steps += ans_frac.simplify()
|
||||
|
||||
return steps
|
||||
|
||||
|
||||
def __sub__(self, other):
|
||||
if type(other) == Fraction:
|
||||
#cool
|
||||
@ -129,6 +165,41 @@ class Fraction(object):
|
||||
|
||||
return steps
|
||||
|
||||
def __rsub__(self, other):
|
||||
if type(other) == Fraction:
|
||||
#cool
|
||||
number = other
|
||||
else:
|
||||
number = Fraction(other)
|
||||
|
||||
steps = []
|
||||
|
||||
if number._denom == self._denom:
|
||||
com_denom = number._denom
|
||||
num1 = number._num
|
||||
num2 = self._num
|
||||
|
||||
else:
|
||||
gcd_denom = gcd(number._denom, self._denom)
|
||||
coef1 = self._denom // gcd_denom
|
||||
coef2 = number._denom // gcd_denom
|
||||
|
||||
steps.append([number._num, coef1, "*", number._denom, coef1, "*", '/', self._num, coef2, "*", self._denom, coef2, "*", "/",'-'])
|
||||
|
||||
com_denom = number._denom * coef1
|
||||
num1 = number._num * coef1
|
||||
num2 = self._num * coef2
|
||||
|
||||
steps.append([num1, num2, '-', com_denom, '/'])
|
||||
|
||||
num = num1 - num2
|
||||
|
||||
ans_frac = Fraction(num, com_denom)
|
||||
steps.append(ans_frac)
|
||||
steps += ans_frac.simplify()
|
||||
|
||||
return steps
|
||||
|
||||
def __neg__(self):
|
||||
return [Fraction(-self._num,self._denom)]
|
||||
|
||||
@ -140,7 +211,6 @@ class Fraction(object):
|
||||
number = Fraction(other)
|
||||
|
||||
steps = []
|
||||
#steps.append("( {num1} * {num2} ) / ( {denom1} * {denom2} )".format(num1 = self._num, num2 = number._num, denom1 = self._denom, denom2 = number._denom))
|
||||
|
||||
steps.append([self._num, number._num, '*', self._denom, number._denom, '*', '/'])
|
||||
|
||||
@ -153,6 +223,26 @@ class Fraction(object):
|
||||
|
||||
return steps
|
||||
|
||||
def __rmul__(self, other):
|
||||
if type(other) == Fraction:
|
||||
#cool
|
||||
number = other
|
||||
else:
|
||||
number = Fraction(other)
|
||||
|
||||
steps = []
|
||||
|
||||
steps.append([number._num, self._num, '*', number._denom, self._denom, '*', '/'])
|
||||
|
||||
num = self._num * number._num
|
||||
denom = self._denom * number._denom
|
||||
|
||||
ans_frac = Fraction(num, denom)
|
||||
steps.append(ans_frac)
|
||||
steps += ans_frac.simplify()
|
||||
|
||||
return steps
|
||||
|
||||
def __truediv__(self, other):
|
||||
if type(other) == Fraction:
|
||||
#cool
|
||||
@ -162,10 +252,25 @@ class Fraction(object):
|
||||
|
||||
steps = []
|
||||
number = Fraction(number._denom, number._num)
|
||||
steps.append([self, number, "/"])
|
||||
steps += self * number
|
||||
|
||||
return steps
|
||||
|
||||
def __rtruediv__(self, other):
|
||||
if type(other) == Fraction:
|
||||
#cool
|
||||
number = other
|
||||
else:
|
||||
number = Fraction(other)
|
||||
|
||||
steps = []
|
||||
self_inv = Fraction(self._denom, self._num)
|
||||
steps.append([number, self_inv, "/"])
|
||||
steps += number * self_inv
|
||||
|
||||
return steps
|
||||
|
||||
def __eq__(self, other):
|
||||
""" == """
|
||||
if type(other) == Fraction:
|
||||
@ -197,10 +302,10 @@ if __name__ == '__main__':
|
||||
h = Fraction(-1,5)
|
||||
t = Fraction(-4,5)
|
||||
print("---------")
|
||||
for i in (f - 1):
|
||||
for i in (f / t):
|
||||
print(i)
|
||||
print("---------")
|
||||
for i in (f + 1):
|
||||
for i in (f / h):
|
||||
print(i)
|
||||
print("---------")
|
||||
for i in (f - g):
|
||||
|
@ -20,8 +20,9 @@ class TestFraction(unittest.TestCase):
|
||||
|
||||
def test_add(self):
|
||||
ans = [[Fraction(2, 3), 1, Fraction(17, 15), 0, 0, Fraction(4,3)], \
|
||||
[Fraction(4,3), Fraction(5,3), Fraction(9,3), Fraction(2,3), Fraction(2,3), 0] \
|
||||
[Fraction(4,3), Fraction(5,3), Fraction(9,5), Fraction(2,3), Fraction(2,3), 2] \
|
||||
]
|
||||
# TODO: Bug pour 1 + 1/-3 |sam. févr. 22 07:01:29 CET 2014
|
||||
|
||||
for (i, f1) in enumerate(self.listFrom):
|
||||
for (j, f2) in enumerate(self.listAgainst):
|
||||
@ -32,12 +33,17 @@ class TestFraction(unittest.TestCase):
|
||||
#print("f2 : ", f2)
|
||||
#print(res)
|
||||
|
||||
# On est obligé de faire ça pour gérer le cas de 1+1 qui ne passe pas par la classe Fraction
|
||||
if type(res) == list:
|
||||
self.assertEqual(res[-1], ans[i][j])
|
||||
else:
|
||||
self.assertEqual(res, ans[i][j])
|
||||
|
||||
def test_sub(self):
|
||||
ans = [[0, Fraction(-1,3), Fraction(-7, 15), Fraction(2,3), Fraction(2,3), Fraction(-2,3)], \
|
||||
[Fraction(2,3), Fraction(1,3), Fraction(1,5), Fraction(4,3), Fraction(4,3), 0] \
|
||||
]
|
||||
# TODO: bug pour 1 - 1/-3 |sam. févr. 22 07:05:15 CET 2014
|
||||
|
||||
for (i, f1) in enumerate(self.listFrom):
|
||||
for (j, f2) in enumerate(self.listAgainst):
|
||||
@ -48,7 +54,11 @@ class TestFraction(unittest.TestCase):
|
||||
#print("f2 : ", f2)
|
||||
#print(res)
|
||||
|
||||
# On est obligé de faire ça pour gérer le cas de 1-1 qui ne passe pas par la classe Fraction
|
||||
if type(res) == list:
|
||||
self.assertEqual(res[-1], ans[i][j])
|
||||
else:
|
||||
self.assertEqual(res, ans[i][j])
|
||||
|
||||
def test_neg(self):
|
||||
pass
|
||||
@ -67,7 +77,11 @@ class TestFraction(unittest.TestCase):
|
||||
#print("f2 : ", f2)
|
||||
#print(res)
|
||||
|
||||
# On est obligé de faire ça pour gérer le cas de 1*1 qui ne passe pas par la classe Fraction
|
||||
if type(res) == list:
|
||||
self.assertEqual(res[-1], ans[i][j])
|
||||
else:
|
||||
self.assertEqual(res, ans[i][j])
|
||||
|
||||
def test_truediv(self):
|
||||
ans = [[1, Fraction(1,2), Fraction(5, 12), -1, -1, Fraction(1,3)], \
|
||||
@ -83,7 +97,11 @@ class TestFraction(unittest.TestCase):
|
||||
#print("f2 : ", f2)
|
||||
#print(res)
|
||||
|
||||
# On est obligé de faire ça pour gérer le cas de 1/1 qui ne passe pas par la classe Fraction
|
||||
if type(res) == list:
|
||||
self.assertEqual(res[-1], ans[i][j])
|
||||
else:
|
||||
self.assertEqual(res, ans[i][j])
|
||||
|
||||
def test_lt(self):
|
||||
pass
|
||||
|
Loading…
Reference in New Issue
Block a user