2013-07-18 14:30:45 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
|
2013-07-18 14:49:38 +00:00
|
|
|
from generic import Stack
|
2013-10-21 15:52:11 +00:00
|
|
|
from fraction import Fraction
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
def str2tokens(exp):
|
|
|
|
"""Convert an expression into a list of tokens
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
:param exp: The expression
|
|
|
|
:returns: the list of tokens
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
>>> str2tokens("1 + 2")
|
2013-10-21 15:52:11 +00:00
|
|
|
[1, '+', 2]
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
"""
|
2013-10-21 15:52:11 +00:00
|
|
|
tokens = exp.split(" ")
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-10-21 15:52:11 +00:00
|
|
|
for (i,t) in enumerate(tokens):
|
|
|
|
try:
|
|
|
|
tokens[i] = int(t)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return tokens
|
2013-08-06 06:01:40 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
def infixToPostfix(infixTokens):
|
|
|
|
"""Transform an infix list of tokens into postfix tokens
|
2013-08-06 06:01:40 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
:param infixTokens: an infix list of tokens
|
|
|
|
:returns: the corresponding postfix list of tokens
|
|
|
|
|
|
|
|
:Example:
|
2013-08-06 06:01:40 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
>>> infixToPostfix([1, "+", 2])
|
|
|
|
[1, 2, '+']
|
2013-08-06 06:01:40 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
>>> infixToPostfix([1, "*", 2, "+", 3])
|
|
|
|
[1, 2, '*', 3, '+']
|
2013-08-07 23:12:11 +00:00
|
|
|
"""
|
2013-10-19 18:33:46 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
priority = {"*" : 3, "/": 3, "+": 2, "-":2, "(": 1}
|
|
|
|
|
|
|
|
opStack = Stack()
|
|
|
|
postfixList = []
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
#infixTokens = infixExp.split(" ")
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
for token in infixTokens:
|
2013-08-07 23:12:11 +00:00
|
|
|
if token == "(":
|
|
|
|
opStack.push(token)
|
|
|
|
elif token == ")":
|
|
|
|
topToken = opStack.pop()
|
|
|
|
while topToken != "(":
|
|
|
|
postfixList.append(topToken)
|
|
|
|
topToken = opStack.pop()
|
2013-10-21 15:52:11 +00:00
|
|
|
elif isOperation(token):
|
2013-10-19 18:33:46 +00:00
|
|
|
# On doit ajouter la condition == str sinon python ne veut pas tester l'appartenance à la chaine de caractère.
|
2013-08-07 23:12:11 +00:00
|
|
|
while (not opStack.isEmpty()) and (priority[opStack.peek()] >= priority[token]):
|
|
|
|
postfixList.append(opStack.pop())
|
|
|
|
opStack.push(token)
|
|
|
|
else:
|
|
|
|
postfixList.append(token)
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
while not opStack.isEmpty():
|
|
|
|
postfixList.append(opStack.pop())
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
return postfixList
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
def computePostfix(postfixTokens):
|
|
|
|
"""Compute a postfix list of tokens
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
:param postfixTokens: a postfix list of tokens
|
|
|
|
:returns: the result of the calculus
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
"""
|
2013-10-19 18:33:46 +00:00
|
|
|
#print(postfixToInfix(postfixExp))
|
2013-08-07 23:12:11 +00:00
|
|
|
# where to save numbers or
|
|
|
|
operandeStack = Stack()
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
#tokenList = postfixExp.split(" ")
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
for (i,token) in enumerate(postfixTokens):
|
2013-10-21 15:52:11 +00:00
|
|
|
if isOperation(token):
|
2013-08-07 23:12:11 +00:00
|
|
|
op2 = operandeStack.pop()
|
|
|
|
op1 = operandeStack.pop()
|
|
|
|
res = doMath(token, op1, op2)
|
|
|
|
operandeStack.push(res)
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
#print("Operation: {op1} {op} {op2}".format(op1 = op1, op = token, op2 = op2))
|
|
|
|
#print(operandeStack)
|
2013-10-19 18:33:46 +00:00
|
|
|
#print(postfixTokens[i+1:])
|
|
|
|
newPostfix = " ".join(operandeStack + postfixTokens[i+1:])
|
2013-10-28 13:49:31 +00:00
|
|
|
#print(postfixToInfix(newPostfix))
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
else:
|
|
|
|
operandeStack.push(token)
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
return operandeStack.pop()
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
def computePostfixBis(postfixTokens):
|
|
|
|
"""Compute a postfix list of tokens like a good student
|
2013-07-18 17:43:36 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
:param postfixTokens: a postfix list of tokens
|
2013-08-07 23:12:11 +00:00
|
|
|
:returns: the result of the expression
|
2013-07-18 17:43:36 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
"""
|
|
|
|
# where to save numbers or
|
|
|
|
operandeStack = Stack()
|
2013-07-18 17:43:36 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
#tokenList = postfixExp.split(" ")
|
|
|
|
tokenList = postfixTokens.copy()
|
2013-07-18 17:43:36 +00:00
|
|
|
|
2013-10-28 13:49:31 +00:00
|
|
|
steps = []
|
2013-07-18 17:43:36 +00:00
|
|
|
|
2013-10-28 13:49:31 +00:00
|
|
|
steps = []
|
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
# On fait le calcul jusqu'à n'avoir plus qu'un élément
|
|
|
|
while len(tokenList) > 1:
|
|
|
|
tmpTokenList = []
|
|
|
|
# on va chercher les motifs du genre A B + pour les calculer
|
|
|
|
while len(tokenList) > 2:
|
2013-10-21 15:52:11 +00:00
|
|
|
if isNumber(tokenList[0]) and isNumber(tokenList[1]) and isOperation(tokenList[2]):
|
2013-08-07 23:12:11 +00:00
|
|
|
# S'il y a une opération à faire
|
|
|
|
op1 = tokenList[0]
|
|
|
|
op2 = tokenList[1]
|
|
|
|
token = tokenList[2]
|
2013-10-28 13:49:31 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
res = doMath(token, op1, op2)
|
2013-07-18 17:43:36 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
tmpTokenList.append(res)
|
2013-07-18 17:43:36 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
# Comme on vient de faire le calcul, on peut détruire aussi les deux prochains termes
|
|
|
|
del tokenList[0:3]
|
|
|
|
else:
|
|
|
|
tmpTokenList.append(tokenList[0])
|
2013-07-18 17:43:36 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
del tokenList[0]
|
|
|
|
tmpTokenList += tokenList
|
2013-07-18 17:43:36 +00:00
|
|
|
|
2013-10-28 13:49:31 +00:00
|
|
|
steps += expand_list(tmpTokenList)
|
|
|
|
|
2013-10-28 13:49:31 +00:00
|
|
|
tokenList = steps[-1].copy()
|
2013-07-18 17:43:36 +00:00
|
|
|
|
2013-10-28 13:49:31 +00:00
|
|
|
|
|
|
|
return steps
|
2013-07-18 17:43:36 +00:00
|
|
|
|
2013-07-18 22:04:13 +00:00
|
|
|
def isNumber(exp):
|
2013-08-07 23:12:11 +00:00
|
|
|
"""Check if the expression can be a number
|
2013-07-18 22:04:13 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
:param exp: an expression
|
|
|
|
:returns: True if the expression can be a number and false otherwise
|
2013-07-18 22:04:13 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
"""
|
2013-10-28 13:49:31 +00:00
|
|
|
return type(exp) == int or type(exp) == Fraction
|
2013-10-28 13:49:31 +00:00
|
|
|
|
|
|
|
def isOperation(exp):
|
|
|
|
"""Check if the expression is an opération in "+-*/"
|
|
|
|
|
|
|
|
:param exp: an expression
|
|
|
|
:returns: boolean
|
|
|
|
|
|
|
|
"""
|
|
|
|
return (type(exp) == str and exp in "+-*/")
|
2013-07-18 22:04:13 +00:00
|
|
|
|
|
|
|
|
2013-07-18 14:30:45 +00:00
|
|
|
def doMath(op, op1, op2):
|
2013-08-07 23:12:11 +00:00
|
|
|
"""Compute "op1 op op2"
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
:param op: operator
|
|
|
|
:param op1: first operande
|
|
|
|
:param op2: second operande
|
|
|
|
:returns: string representing the result
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
"""
|
2013-10-21 15:52:11 +00:00
|
|
|
operations = {"+": "__add__", "-": "__sub__", "*": "__mul__"}
|
|
|
|
if op == "/":
|
2013-10-28 13:49:31 +00:00
|
|
|
ans = [Fraction(op1, op2)]
|
|
|
|
ans += ans[0].simplify()
|
|
|
|
return ans
|
2013-10-21 15:52:11 +00:00
|
|
|
else:
|
|
|
|
return getattr(op1,operations[op])(op2)
|
|
|
|
|
2013-07-18 14:30:45 +00:00
|
|
|
|
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
def postfixToInfix(postfixTokens):
|
2013-10-28 13:49:31 +00:00
|
|
|
"""Transforms postfix list of tokens into infix string
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
:param postfixTokens: a postfix list of tokens
|
2013-10-28 13:49:31 +00:00
|
|
|
:returns: the corresponding infix string
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
"""
|
|
|
|
operandeStack = Stack()
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
#print(postfixTokens)
|
|
|
|
|
|
|
|
#tokenList = postfixExp.split(" ")
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
for (i,token) in enumerate(postfixTokens):
|
2013-10-21 15:52:11 +00:00
|
|
|
if isOperation(token):
|
2013-08-07 23:12:11 +00:00
|
|
|
op2 = operandeStack.pop()
|
|
|
|
if needPar(op2, token, "after"):
|
2013-10-21 15:52:11 +00:00
|
|
|
op2 = "( " + str(op2) + " )"
|
2013-08-07 23:12:11 +00:00
|
|
|
op1 = operandeStack.pop()
|
|
|
|
if needPar(op1, token, "before"):
|
2013-10-21 15:52:11 +00:00
|
|
|
op1 = "( " + str(op1) + " )"
|
2013-08-07 23:12:11 +00:00
|
|
|
res = "{op1} {op} {op2}".format(op1 = op1, op = token, op2 = op2)
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
operandeStack.push(res)
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
else:
|
|
|
|
operandeStack.push(token)
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
return operandeStack.pop()
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-07-18 22:04:13 +00:00
|
|
|
def needPar(operande, operator, posi = "after"):
|
2013-08-07 23:12:11 +00:00
|
|
|
"""Says whether or not the operande needs parenthesis
|
|
|
|
|
|
|
|
:param operande: the operande
|
|
|
|
:param operator: the operator
|
|
|
|
:param posi: "after"(default) if the operande will be after the operator, "before" othewise
|
|
|
|
:returns: bollean
|
|
|
|
"""
|
2013-10-19 18:33:46 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
priority = {"*" : 3, "/": 3, "+": 2, "-":2}
|
|
|
|
|
2013-10-21 15:52:11 +00:00
|
|
|
if isNumber(operande) and operande < 0:
|
2013-08-07 23:12:11 +00:00
|
|
|
return 1
|
|
|
|
elif not isNumber(operande):
|
|
|
|
# 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
|
2013-10-28 13:49:31 +00:00
|
|
|
#debug_var("stand_alone",stand_alone)
|
|
|
|
#debug_var("operande", type(operande))
|
2013-08-07 23:12:11 +00:00
|
|
|
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")
|
|
|
|
|
|
|
|
return stand_alone and (minor_priority or special)
|
|
|
|
else:
|
|
|
|
return 0
|
2013-07-18 22:04:13 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
def get_main_op(tokens):
|
|
|
|
"""Getting the main operation of the list of tokens
|
2013-07-18 21:19:55 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
:param exp: the list of tokens
|
2013-08-07 23:12:11 +00:00
|
|
|
:returns: the main operation (+, -, * or /) or 0 if the expression is only one element
|
2013-07-18 21:19:55 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
"""
|
2013-07-18 21:19:55 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
priority = {"*" : 3, "/": 3, "+": 2, "-":2}
|
2013-07-18 21:19:55 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
parStack = Stack()
|
2013-10-19 18:33:46 +00:00
|
|
|
#tokenList = exp.split(" ")
|
2013-07-18 21:19:55 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
if len(tokens) == 1:
|
2013-08-07 23:12:11 +00:00
|
|
|
# Si l'expression n'est qu'un élément
|
|
|
|
return 0
|
2013-07-18 21:19:55 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
main_op = []
|
2013-07-18 21:19:55 +00:00
|
|
|
|
2013-10-19 18:33:46 +00:00
|
|
|
for token in tokens:
|
2013-08-07 23:12:11 +00:00
|
|
|
if token == "(":
|
|
|
|
parStack.push(token)
|
|
|
|
elif token == ")":
|
|
|
|
parStack.pop()
|
2013-10-21 15:52:11 +00:00
|
|
|
elif isOperation(token) and parStack.isEmpty():
|
2013-08-07 23:12:11 +00:00
|
|
|
main_op.append(token)
|
2013-07-18 21:19:55 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
return min(main_op, key = lambda s: priority[s])
|
2013-07-18 21:19:55 +00:00
|
|
|
|
|
|
|
|
2013-07-19 11:20:57 +00:00
|
|
|
def expand_list(list_list):
|
2013-08-07 23:12:11 +00:00
|
|
|
"""Expand list of list
|
2013-07-19 11:20:57 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
:param list: the list to expande
|
|
|
|
:returns: list of expanded lists
|
2013-07-19 11:20:57 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
:Example:
|
2013-08-06 06:01:40 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
>>> 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]]
|
2013-10-19 09:42:05 +00:00
|
|
|
>>> expand_list([1,2,4,5,6,7,8])
|
2013-10-28 13:49:31 +00:00
|
|
|
[[1, 2, 4, 5, 6, 7, 8]]
|
2013-08-06 06:01:40 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
"""
|
|
|
|
list_in_list = [i for i in list_list if type(i) == list].copy()
|
2013-07-19 11:20:57 +00:00
|
|
|
|
2013-10-19 09:42:05 +00:00
|
|
|
try:
|
|
|
|
nbr_ans_list = max([len(i) for i in list_in_list])
|
|
|
|
|
|
|
|
ans = [list_list.copy() for i in range(nbr_ans_list)]
|
|
|
|
for (i,l) in enumerate(ans):
|
|
|
|
for (j,e) in enumerate(l):
|
|
|
|
if type(e) == 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:
|
2013-10-28 13:49:31 +00:00
|
|
|
ans = [list_list]
|
2013-07-19 11:20:57 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
return ans
|
2013-07-19 11:20:57 +00:00
|
|
|
|
2013-10-28 13:49:31 +00:00
|
|
|
def print_steps(steps):
|
|
|
|
"""Juste print a list
|
|
|
|
|
|
|
|
:param steps: @todo
|
|
|
|
:returns: @todo
|
|
|
|
|
|
|
|
"""
|
2013-10-28 13:49:31 +00:00
|
|
|
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
|
2013-10-28 13:49:31 +00:00
|
|
|
|
2013-07-19 11:20:57 +00:00
|
|
|
|
|
|
|
def debug_var(name, var):
|
2013-08-07 23:12:11 +00:00
|
|
|
"""print the name of the variable and the value
|
2013-07-19 11:20:57 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
:param name: the name of the variable
|
|
|
|
:param var: the variable we want information
|
|
|
|
"""
|
|
|
|
print(name, ": ", var)
|
2013-07-19 11:20:57 +00:00
|
|
|
|
2013-07-18 14:30:45 +00:00
|
|
|
|
|
|
|
def test(exp):
|
2013-08-07 23:12:11 +00:00
|
|
|
"""Make various test on an expression
|
2013-07-18 14:30:45 +00:00
|
|
|
|
2013-08-07 23:12:11 +00:00
|
|
|
"""
|
|
|
|
print("-------------")
|
|
|
|
print("Expression ",exp)
|
2013-10-19 18:33:46 +00:00
|
|
|
tokens = str2tokens(exp)
|
|
|
|
postfix = infixToPostfix(tokens)
|
2013-08-07 23:12:11 +00:00
|
|
|
#print("Postfix " , postfix)
|
|
|
|
#print(computePostfix(postfix))
|
|
|
|
#print("Bis")
|
2013-10-28 13:49:31 +00:00
|
|
|
steps = [postfix]
|
|
|
|
steps += computePostfixBis(postfix)
|
2013-10-28 13:49:31 +00:00
|
|
|
print_steps(steps)
|
2013-08-07 23:12:11 +00:00
|
|
|
#print(postfixToInfix(postfix))
|
|
|
|
#print(get_main_op(exp))
|
2013-07-18 14:30:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2013-10-28 13:49:31 +00:00
|
|
|
exp = "1 + 3 * 5"
|
|
|
|
test(exp)
|
2013-08-07 23:12:11 +00:00
|
|
|
|
|
|
|
#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)
|
|
|
|
#
|
2013-10-28 13:49:31 +00:00
|
|
|
#exp = "2 + 5 * ( 3 - 4 )"
|
|
|
|
#test(exp)
|
|
|
|
#
|
|
|
|
#exp = "( 2 + 5 ) * ( 3 - 4 )"
|
|
|
|
#test(exp)
|
|
|
|
#
|
|
|
|
#exp = "( 2 + 5 ) * ( 3 * 4 )"
|
|
|
|
#test(exp)
|
2013-10-19 09:42:05 +00:00
|
|
|
|
2013-10-28 13:49:31 +00:00
|
|
|
#exp = "( 2 + 5 - 1 ) / ( 3 * 4 )"
|
|
|
|
#test(exp)
|
2013-10-28 13:49:31 +00:00
|
|
|
|
|
|
|
exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12"
|
2013-10-19 09:42:05 +00:00
|
|
|
test(exp)
|
2013-10-28 13:49:31 +00:00
|
|
|
|
|
|
|
exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 2"
|
|
|
|
test(exp)
|
|
|
|
|
|
|
|
exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12 + 5 * 5"
|
2013-10-19 09:42:05 +00:00
|
|
|
test(exp)
|
2013-08-07 23:12:11 +00:00
|
|
|
|
|
|
|
#print(expand_list([1,2,['a','b','c'], 3, ['d','e']]))
|
|
|
|
|
|
|
|
## Ce denier pose un soucis. Pour le faire marcher il faudrai implémenter le calcul avec les fractions
|
|
|
|
#exp = "( 2 + 5 ) / 3 * 4"
|
|
|
|
#test(exp)
|
2013-10-19 18:33:46 +00:00
|
|
|
import doctest
|
|
|
|
doctest.testmod()
|
2013-07-18 14:30:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|