j'ai fait de la grosse merde!

This commit is contained in:
Lafrite 2013-10-28 14:49:31 +01:00
parent 0423412e6b
commit 5c35ec9bae
2 changed files with 66 additions and 25 deletions

View File

@ -86,12 +86,15 @@ def computePostfixBis(postfixExp):
:returns: the result of the expression :returns: the result of the expression
""" """
print(postfixExp)
print(postfixToInfix(postfixExp)) print(postfixToInfix(postfixExp))
# where to save numbers or # where to save numbers or
operandeStack = Stack() operandeStack = Stack()
tokenList = postfixExp.split(" ") tokenList = postfixExp.split(" ")
steps = []
# On fait le calcul jusqu'à n'avoir plus qu'un élément # On fait le calcul jusqu'à n'avoir plus qu'un élément
while len(tokenList) > 1: while len(tokenList) > 1:
tmpTokenList = [] tmpTokenList = []
@ -114,10 +117,15 @@ def computePostfixBis(postfixExp):
del tokenList[0] del tokenList[0]
tmpTokenList += tokenList tmpTokenList += tokenList
tokenList = expand_list(tmpTokenList) steps += expand_list(tmpTokenList)
print(postfixToInfix(" ".join(tokenList))) #print("----- Steps -----")
#print(steps)
return tokenList[0] tokenList = steps[-1]
#print(postfixToInfix(tokenList))
return steps
def isNumber(exp): def isNumber(exp):
"""Check if the expression can be a number """Check if the expression can be a number
@ -126,11 +134,16 @@ def isNumber(exp):
:returns: True if the expression can be a number and false otherwise :returns: True if the expression can be a number and false otherwise
""" """
tokenList = exp.split(" ") return type(exp) == int or type(exp) == "Fraction"
if len(tokenList) != 1:
# si l'expression est trop longue pour être un chiffre def isOperation(exp):
return 0 """Check if the expression is an opération in "+-*/"
return exp.isdigit() or (exp[0] == "-" and exp[1:].isdigit())
:param exp: an expression
:returns: boolean
"""
return (type(exp) == str and exp in "+-*/")
def doMath(op, op1, op2): def doMath(op, op1, op2):
@ -183,12 +196,14 @@ def needPar(operande, operator, posi = "after"):
""" """
priority = {"*" : 3, "/": 3, "+": 2, "-":2} priority = {"*" : 3, "/": 3, "+": 2, "-":2}
if isNumber(operande) and "-" in operande: if type(operande)==int and operande < 0:
return 1 return 1
elif not isNumber(operande): elif not isNumber(operande):
# Si c'est une grande expression ou un chiffre négatif # Si c'est une grande expression ou un chiffre négatif
stand_alone = get_main_op(operande) stand_alone = get_main_op(operande)
# Si la priorité de l'operande est plus faible que celle de l'opérateur # 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] minor_priority = priority[get_main_op(operande)] < priority[operator]
# Si l'opérateur est -/ pour after ou juste / pour before # Si l'opérateur est -/ pour after ou juste / pour before
special = (operator in "-/" and posi == "after") or (operator in "/" and posi == "before") special = (operator in "-/" and posi == "after") or (operator in "/" and posi == "before")
@ -238,7 +253,7 @@ def expand_list(list_list):
>>> expand_list([1,2,[3,4],5,[6,7,8]]) >>> 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]] [[1, 2, 3, 5, 6], [1, 2, 4, 5, 7], [1, 2, 4, 5, 8]]
>>> expand_list([1,2,4,5,6,7,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() list_in_list = [i for i in list_list if type(i) == list].copy()
@ -253,10 +268,21 @@ def expand_list(list_list):
ans[i][j] = e[min(i,len(e)-1)] ans[i][j] = e[min(i,len(e)-1)]
# S'il n'y a pas eut d'étapes intermédiaires (2e exemple) # S'il n'y a pas eut d'étapes intermédiaires (2e exemple)
except ValueError: except ValueError:
ans = list_list ans = [list_list]
return ans return ans
def print_steps(steps):
"""Juste print a list
:param steps: @todo
:returns: @todo
"""
print("{first} \t = {sec}".format(first = steps[-1], sec = steps[-2]))
for i in steps[-2:0:-1]:
print("\t\t = ",i)
def debug_var(name, var): def debug_var(name, var):
"""print the name of the variable and the value """print the name of the variable and the value
@ -277,14 +303,15 @@ def test(exp):
#print("Postfix " , postfix) #print("Postfix " , postfix)
#print(computePostfix(postfix)) #print(computePostfix(postfix))
#print("Bis") #print("Bis")
print(computePostfixBis(postfix)) steps = computePostfixBis(postfix)
print_steps(steps)
#print(postfixToInfix(postfix)) #print(postfixToInfix(postfix))
#print(get_main_op(exp)) #print(get_main_op(exp))
if __name__ == '__main__': if __name__ == '__main__':
#exp = "1 + 3 * 5" exp = "1 + 3 * 5"
#test(exp) test(exp)
#exp = "2 * 3 * 3 * 5" #exp = "2 * 3 * 3 * 5"
#test(exp) #test(exp)
@ -304,16 +331,25 @@ if __name__ == '__main__':
#exp = "2 * ( 2 - ( 3 + 4 ) ) + 5 * ( 3 - 4 )" #exp = "2 * ( 2 - ( 3 + 4 ) ) + 5 * ( 3 - 4 )"
#test(exp) #test(exp)
# #
exp = "2 + 5 * ( 3 - 4 )" #exp = "2 + 5 * ( 3 - 4 )"
test(exp) #test(exp)
#
#exp = "( 2 + 5 ) * ( 3 - 4 )"
#test(exp)
#
#exp = "( 2 + 5 ) * ( 3 * 4 )"
#test(exp)
exp = "( 2 + 5 ) * ( 3 - 4 )" exp = "( 2 + 5 - 1 ) / ( 3 * 4 )"
test(exp) test(exp)
exp = "( 2 + 5 ) * ( 3 * 4 )" exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12"
test(exp) test(exp)
exp = "( 2 + 5 ) / ( 3 * 4 )" exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 2"
test(exp)
exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12 + 5 * 5"
test(exp) test(exp)
#print(expand_list([1,2,['a','b','c'], 3, ['d','e']])) #print(expand_list([1,2,['a','b','c'], 3, ['d','e']]))

View File

@ -22,23 +22,25 @@ class Fraction(object):
:returns: steps to simplify the fraction or the fraction if there is nothing to do :returns: steps to simplify the fraction or the fraction if there is nothing to do
""" """
steps = [str(self)] steps = [self]
if self._denom < 0: if self._denom < 0:
self._num = - self._num self._num = - self._num
self._denom = - self._denom self._denom = - self._denom
steps += [str(self)] steps += [self]
gcd_ = gcd(abs(self._num), abs(self._denom)) gcd_ = gcd(abs(self._num), abs(self._denom))
if self._num == self._denom: if self._num == self._denom:
self._num = 1 self._num = 1
self._denom = 1 self._denom = 1
steps += [str(self)] steps += [self]
elif gcd_ != 1: elif gcd_ != 1:
self._num, self._denom = self._num // gcd_ , self._denom // gcd_ 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 += ["( {reste1} * {gcd} ) / ( {reste2} * {gcd} )".format(reste1 = self._num, reste2 = self._denom, gcd = gcd_)]
steps += [str(self)]
# 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 += [self]
return steps return steps
@ -47,6 +49,9 @@ class Fraction(object):
return str(self._num) return str(self._num)
else: else:
return str(self._num) + " / " + str(self._denom) return str(self._num) + " / " + str(self._denom)
#def __repr__(self):
# return self.__str__()
def __add__(self, other): def __add__(self, other):
if type(other) == Fraction: if type(other) == Fraction: