j'ai fait de la grosse merde! mais tout est rattrapé! Reste plus qu'à
nettoyer et ranger tout ranger correctement Conflicts: calculus.py
This commit is contained in:
parent
ec3e4d34d2
commit
90d5cbee75
121
calculus.py
121
calculus.py
|
@ -92,7 +92,7 @@ def computePostfix(postfixTokens):
|
|||
#print(operandeStack)
|
||||
#print(postfixTokens[i+1:])
|
||||
newPostfix = " ".join(operandeStack + postfixTokens[i+1:])
|
||||
print(postfixToInfix(newPostfix))
|
||||
#print(postfixToInfix(newPostfix))
|
||||
|
||||
else:
|
||||
operandeStack.push(token)
|
||||
|
@ -113,6 +113,8 @@ def computePostfixBis(postfixTokens):
|
|||
#tokenList = postfixExp.split(" ")
|
||||
tokenList = postfixTokens.copy()
|
||||
|
||||
steps = []
|
||||
|
||||
# On fait le calcul jusqu'à n'avoir plus qu'un élément
|
||||
while len(tokenList) > 1:
|
||||
tmpTokenList = []
|
||||
|
@ -123,6 +125,7 @@ def computePostfixBis(postfixTokens):
|
|||
op1 = tokenList[0]
|
||||
op2 = tokenList[1]
|
||||
token = tokenList[2]
|
||||
|
||||
res = doMath(token, op1, op2)
|
||||
|
||||
tmpTokenList.append(res)
|
||||
|
@ -135,10 +138,14 @@ def computePostfixBis(postfixTokens):
|
|||
del tokenList[0]
|
||||
tmpTokenList += tokenList
|
||||
|
||||
tokenList = expand_list(tmpTokenList)
|
||||
print(postfixToInfix(tokenList))
|
||||
|
||||
return tokenList[0]
|
||||
steps += expand_list(tmpTokenList)
|
||||
|
||||
tokenList = steps[-1].copy()
|
||||
|
||||
#print(postfixToInfix(tokenList))
|
||||
|
||||
return steps
|
||||
|
||||
def isNumber(exp):
|
||||
"""Check if the expression can be a number
|
||||
|
@ -147,7 +154,7 @@ def isNumber(exp):
|
|||
:returns: True if the expression can be a number and false otherwise
|
||||
|
||||
"""
|
||||
return type(exp) == int
|
||||
return type(exp) == int or type(exp) == Fraction
|
||||
|
||||
def isOperation(exp):
|
||||
"""Check if the expression is an opération in "+-*/"
|
||||
|
@ -170,17 +177,19 @@ def doMath(op, op1, op2):
|
|||
"""
|
||||
operations = {"+": "__add__", "-": "__sub__", "*": "__mul__"}
|
||||
if op == "/":
|
||||
return Fraction(op1,op2)
|
||||
ans = [Fraction(op1, op2)]
|
||||
ans += ans[0].simplify()
|
||||
return ans
|
||||
else:
|
||||
return getattr(op1,operations[op])(op2)
|
||||
|
||||
|
||||
|
||||
def postfixToInfix(postfixTokens):
|
||||
"""Transforme postfix list of tokens into infix list of tokens
|
||||
"""Transforms postfix list of tokens into infix string
|
||||
|
||||
:param postfixTokens: a postfix list of tokens
|
||||
:returns: the corresponding infix list of tokens
|
||||
:returns: the corresponding infix string
|
||||
|
||||
"""
|
||||
operandeStack = Stack()
|
||||
|
@ -223,6 +232,8 @@ def needPar(operande, operator, posi = "after"):
|
|||
# Si c'est une grande expression ou un chiffre négatif
|
||||
stand_alone = get_main_op(operande)
|
||||
# Si la priorité de l'operande est plus faible que celle de l'opérateur
|
||||
#debug_var("stand_alone",stand_alone)
|
||||
#debug_var("operande", type(operande))
|
||||
minor_priority = priority[get_main_op(operande)] < priority[operator]
|
||||
# Si l'opérateur est -/ pour after ou juste / pour before
|
||||
special = (operator in "-/" and posi == "after") or (operator in "/" and posi == "before")
|
||||
|
@ -272,7 +283,7 @@ def expand_list(list_list):
|
|||
>>> expand_list([1,2,[3,4],5,[6,7,8]])
|
||||
[[1, 2, 3, 5, 6], [1, 2, 4, 5, 7], [1, 2, 4, 5, 8]]
|
||||
>>> expand_list([1,2,4,5,6,7,8])
|
||||
[1, 2, 4, 5, 6, 7, 8]
|
||||
[[1, 2, 4, 5, 6, 7, 8]]
|
||||
|
||||
"""
|
||||
list_in_list = [i for i in list_list if type(i) == list].copy()
|
||||
|
@ -287,10 +298,32 @@ def expand_list(list_list):
|
|||
ans[i][j] = e[min(i,len(e)-1)]
|
||||
# S'il n'y a pas eut d'étapes intermédiaires (2e exemple)
|
||||
except ValueError:
|
||||
ans = list_list
|
||||
ans = [list_list]
|
||||
|
||||
return ans
|
||||
|
||||
def print_steps(steps):
|
||||
"""Juste print a list
|
||||
|
||||
:param steps: @todo
|
||||
:returns: @todo
|
||||
|
||||
"""
|
||||
print("{first} \t = {sec}".format(first = str_from_postfix(steps[0]), sec = str_from_postfix(steps[1])))
|
||||
for i in steps[2:]:
|
||||
print("\t\t = {i}".format(i=str_from_postfix(i)))
|
||||
|
||||
|
||||
def str_from_postfix(postfix):
|
||||
"""Return the string representing the expression
|
||||
|
||||
:param postfix: a postfix ordered list of tokens
|
||||
:returns: the corresponding string expression
|
||||
|
||||
"""
|
||||
infix = postfixToInfix(postfix)
|
||||
return infix
|
||||
|
||||
|
||||
def debug_var(name, var):
|
||||
"""print the name of the variable and the value
|
||||
|
@ -312,43 +345,55 @@ def test(exp):
|
|||
#print("Postfix " , postfix)
|
||||
#print(computePostfix(postfix))
|
||||
#print("Bis")
|
||||
print(computePostfixBis(postfix))
|
||||
steps = [postfix]
|
||||
steps += computePostfixBis(postfix)
|
||||
print(steps)
|
||||
print_steps(steps)
|
||||
#print(postfixToInfix(postfix))
|
||||
#print(get_main_op(exp))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
exp = "1 + 3 * 5"
|
||||
#exp = "1 + 3 * 5"
|
||||
#test(exp)
|
||||
|
||||
#exp = "2 * 3 * 3 * 5"
|
||||
#test(exp)
|
||||
|
||||
#exp = "2 * 3 + 3 * 5"
|
||||
#test(exp)
|
||||
|
||||
#exp = "2 * ( 3 + 4 ) + 3 * 5"
|
||||
#test(exp)
|
||||
#
|
||||
#exp = "2 * ( 3 + 4 ) + ( 3 - 4 ) * 5"
|
||||
#test(exp)
|
||||
#
|
||||
#exp = "2 * ( 2 - ( 3 + 4 ) ) + ( 3 - 4 ) * 5"
|
||||
#test(exp)
|
||||
#
|
||||
#exp = "2 * ( 2 - ( 3 + 4 ) ) + 5 * ( 3 - 4 )"
|
||||
#test(exp)
|
||||
#
|
||||
#exp = "2 + 5 * ( 3 - 4 )"
|
||||
#test(exp)
|
||||
#
|
||||
#exp = "( 2 + 5 ) * ( 3 - 4 )"
|
||||
#test(exp)
|
||||
#
|
||||
#exp = "( 2 + 5 ) * ( 3 * 4 )"
|
||||
#test(exp)
|
||||
|
||||
#exp = "( 2 + 5 - 1 ) / ( 3 * 4 )"
|
||||
#test(exp)
|
||||
|
||||
exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12"
|
||||
test(exp)
|
||||
|
||||
exp = "2 * 3 * 3 * 5"
|
||||
exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 2"
|
||||
test(exp)
|
||||
|
||||
exp = "2 * 3 + 3 * 5"
|
||||
test(exp)
|
||||
|
||||
exp = "2 * ( 3 + 4 ) + 3 * 5"
|
||||
test(exp)
|
||||
|
||||
exp = "2 * ( 3 + 4 ) + ( 3 - 4 ) * 5"
|
||||
test(exp)
|
||||
|
||||
exp = "2 * ( 2 - ( 3 + 4 ) ) + ( 3 - 4 ) * 5"
|
||||
test(exp)
|
||||
|
||||
exp = "2 * ( 2 - ( 3 + 4 ) ) + 5 * ( 3 - 4 )"
|
||||
test(exp)
|
||||
|
||||
exp = "2 + 5 * ( 3 - 4 )"
|
||||
test(exp)
|
||||
|
||||
exp = "( 2 + 5 ) * ( 3 - 4 )"
|
||||
test(exp)
|
||||
|
||||
exp = "( 2 + 5 ) * ( 3 * 4 )"
|
||||
test(exp)
|
||||
|
||||
exp = "( 2 + 5 - 1 ) / ( 3 * 4 )"
|
||||
exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12 + 5 * 5"
|
||||
test(exp)
|
||||
|
||||
#print(expand_list([1,2,['a','b','c'], 3, ['d','e']]))
|
||||
|
|
87
fraction.py
87
fraction.py
|
@ -21,23 +21,25 @@ class Fraction(object):
|
|||
|
||||
:returns: steps to simplify the fraction or the fraction if there is nothing to do
|
||||
"""
|
||||
steps = [str(self)]
|
||||
steps = []
|
||||
|
||||
if self._denom < 0:
|
||||
self._num = - self._num
|
||||
self._denom = - self._denom
|
||||
steps += [str(self)]
|
||||
steps.append(self)
|
||||
|
||||
gcd_ = gcd(abs(self._num), abs(self._denom))
|
||||
if self._num == self._denom:
|
||||
self._num = 1
|
||||
self._denom = 1
|
||||
steps += [str(self)]
|
||||
steps.append(self)
|
||||
|
||||
elif gcd_ != 1:
|
||||
self._num, self._denom = self._num // gcd_ , self._denom // gcd_
|
||||
steps += ["( {reste1} * {gcd} ) / ( {reste2} * {gcd} )".format(reste1 = self._num, reste2 = self._denom, gcd = gcd_)]
|
||||
steps += [str(self)]
|
||||
steps.append("( {reste1} * {gcd} ) / ( {reste2} * {gcd} )".format(reste1 = self._num, reste2 = self._denom, gcd = gcd_))
|
||||
|
||||
# Certainement le truc le plus moche que j'ai jamais fait... On ne met que des strings dans steps puis au dernier moment on met une fraction. C'est moche de ma part
|
||||
steps.append(self)
|
||||
|
||||
return steps
|
||||
|
||||
|
@ -47,6 +49,9 @@ class Fraction(object):
|
|||
else:
|
||||
return str(self._num) + " / " + str(self._denom)
|
||||
|
||||
def __repr__(self):
|
||||
return "< Fraction " + self.__str__() + ">"
|
||||
|
||||
def __add__(self, other):
|
||||
if type(other) == Fraction:
|
||||
#cool
|
||||
|
@ -54,7 +59,7 @@ class Fraction(object):
|
|||
else:
|
||||
number = Fraction(other)
|
||||
|
||||
steps = ["{frac1} + {frac2}".format(frac1 = self, frac2 = number)]
|
||||
steps = []
|
||||
|
||||
if self._denom == number._denom:
|
||||
com_denom = self._denom
|
||||
|
@ -66,17 +71,18 @@ class Fraction(object):
|
|||
coef1 = number._denom // gcd_denom
|
||||
coef2 = self._denom // gcd_denom
|
||||
|
||||
steps += ["( {num1} * {coef1} ) / ( {den1} * {coef1} ) + ( {num2} * {coef2} ) / ( {den2} * {coef2} )".format(num1 = self._num, den1 = self._denom, coef1 = coef1, num2 = number._num, den2 = number._denom, coef2 = coef2)]
|
||||
steps.append("( {num1} * {coef1} ) / ( {den1} * {coef1} ) + ( {num2} * {coef2} ) / ( {den2} * {coef2} )".format(num1 = self._num, den1 = self._denom, coef1 = coef1, num2 = number._num, den2 = number._denom, coef2 = coef2))
|
||||
|
||||
com_denom = self._denom * coef1
|
||||
num1 = self._num * coef1
|
||||
num2 = number._num * coef2
|
||||
|
||||
steps += ["( {num1} + {num2} ) / {denom}".format(num1 = num1, num2 = num2, denom = com_denom)]
|
||||
steps.append("( {num1} + {num2} ) / {denom}".format(num1 = num1, num2 = num2, denom = com_denom))
|
||||
|
||||
num = num1 + num2
|
||||
|
||||
ans_frac = Fraction(num, com_denom)
|
||||
steps.append(ans_frac)
|
||||
steps += ans_frac.simplify()
|
||||
|
||||
return steps
|
||||
|
@ -88,7 +94,7 @@ class Fraction(object):
|
|||
else:
|
||||
number = Fraction(other)
|
||||
|
||||
steps = ["{frac1} - {frac2}".format(frac1 = self, frac2 = number)]
|
||||
steps = []
|
||||
|
||||
if self._denom == number._denom:
|
||||
com_denom = self._denom
|
||||
|
@ -100,17 +106,18 @@ class Fraction(object):
|
|||
coef1 = number._denom // gcd_denom
|
||||
coef2 = self._denom // gcd_denom
|
||||
|
||||
steps += ["( {num1} * {coef1} ) / ( {den1} * {coef1} ) - ( {num2} * {coef2} ) / ( {den2} * {coef2} )".format(num1 = self._num, den1 = self._denom, coef1 = coef1, num2 = number._num, den2 = number._denom, coef2 = coef2)]
|
||||
steps.append("( {num1} * {coef1} ) / ( {den1} * {coef1} ) - ( {num2} * {coef2} ) / ( {den2} * {coef2} )".format(num1 = self._num, den1 = self._denom, coef1 = coef1, num2 = number._num, den2 = number._denom, coef2 = coef2))
|
||||
|
||||
com_denom = self._denom * coef1
|
||||
num1 = self._num * coef1
|
||||
num2 = number._num * coef2
|
||||
|
||||
steps += ["( {num1} - {num2} ) / {denom}".format(num1 = num1, num2 = num2, denom = com_denom)]
|
||||
steps.append("( {num1} - {num2} ) / {denom}".format(num1 = num1, num2 = num2, denom = com_denom))
|
||||
|
||||
num = num1 - num2
|
||||
|
||||
ans_frac = Fraction(num, com_denom)
|
||||
steps.append(ans_frac)
|
||||
steps += ans_frac.simplify()
|
||||
|
||||
return steps
|
||||
|
@ -122,13 +129,14 @@ class Fraction(object):
|
|||
else:
|
||||
number = Fraction(other)
|
||||
|
||||
steps = ["( {frac1} ) * ( {frac2} )".format(frac1 = self, frac2 = number)]
|
||||
steps += ["( {num1} * {num2} ) / ( {denom1} * {denom2} )".format(num1 = self._num, num2 = number._num, denom1 = self._denom, denom2 = number._denom)]
|
||||
steps = []
|
||||
steps.append("( {num1} * {num2} ) / ( {denom1} * {denom2} )".format(num1 = self._num, num2 = number._num, denom1 = self._denom, denom2 = number._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
|
||||
|
@ -140,18 +148,29 @@ class Fraction(object):
|
|||
else:
|
||||
number = Fraction(other)
|
||||
|
||||
steps = ["( {frac1} ) / ( {frac2} )".format(frac1 = self, frac2 = number)]
|
||||
steps = []
|
||||
number = Fraction(number._denom, number._num)
|
||||
steps += self * number
|
||||
|
||||
return steps
|
||||
|
||||
def __lt__(self, other):
|
||||
if type(other) == Fraction:
|
||||
return (self._num / self._denom) < (other._num / other._denom)
|
||||
else:
|
||||
return (self._num / self._denom) < other
|
||||
|
||||
def __le__(self, other):
|
||||
if type(other) == Fraction:
|
||||
return (self._num / self._denom) <= (other._num / other._denom)
|
||||
else:
|
||||
return (self._num / self._denom) <= other
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
f = Fraction(34, 12)
|
||||
g = Fraction(1,5)
|
||||
f = Fraction(1, 12)
|
||||
g = Fraction(1, 12)
|
||||
h = Fraction(-1,5)
|
||||
t = Fraction(-4,5)
|
||||
print("---------")
|
||||
|
@ -163,24 +182,24 @@ if __name__ == '__main__':
|
|||
print("---------")
|
||||
for i in (f + g):
|
||||
print(i)
|
||||
print("---------")
|
||||
for i in (f - g):
|
||||
print(i)
|
||||
print("---------")
|
||||
for i in (f * g):
|
||||
print(i)
|
||||
print("---------")
|
||||
for i in (h + t):
|
||||
print(i)
|
||||
print("---------")
|
||||
for i in (h - t):
|
||||
print(i)
|
||||
print("---------")
|
||||
for i in (h * t):
|
||||
print(i)
|
||||
print("---------")
|
||||
for i in (h / t):
|
||||
print(i)
|
||||
#print("---------")
|
||||
#for i in (f - g):
|
||||
# print(i)
|
||||
#print("---------")
|
||||
#for i in (f * g):
|
||||
# print(i)
|
||||
#print("---------")
|
||||
#for i in (h + t):
|
||||
# print(i)
|
||||
#print("---------")
|
||||
#for i in (h - t):
|
||||
# print(i)
|
||||
#print("---------")
|
||||
#for i in (h * t):
|
||||
# print(i)
|
||||
#print("---------")
|
||||
#for i in (h / t):
|
||||
# print(i)
|
||||
|
||||
#print(f.simplify())
|
||||
|
||||
|
|
Loading…
Reference in New Issue