Solve bug with fraction and remove duplicates in simplify

This commit is contained in:
lafrite 2013-11-17 09:38:33 +01:00
parent af079ab584
commit 922c1213c1

View File

@ -34,6 +34,8 @@ class Expression(object):
@param render: function which render the list of token (postfix form now) @param render: function which render the list of token (postfix form now)
""" """
print("\t ---------- In simplify ---------- ")
if not self.can_go_further(): if not self.can_go_further():
yield render(self.postfix_tokens) yield render(self.postfix_tokens)
else: else:
@ -41,11 +43,12 @@ class Expression(object):
old_s = '' old_s = ''
for s in self.steps: for s in self.steps:
new_s = render(s) new_s = render(s)
# Astuce pour éviter d'avoir deux fois la même étape (par exemple pour la transfo d'une division en fraction # Astuce pour éviter d'avoir deux fois la même étape (par exemple pour la transfo d'une division en fraction)
if new_s != old_s: if new_s != old_s:
old_s = new_s old_s = new_s
yield new_s yield new_s
for s in self.child.simplify(render = render): for s in self.child.simplify(render = render):
if old_s != s:
yield s yield s
def can_go_further(self): def can_go_further(self):
@ -262,7 +265,7 @@ class Expression(object):
infix_tokens = operandeStack.pop() infix_tokens = operandeStack.pop()
if type(infix_tokens) == list: if type(infix_tokens) == list:
infix_tokens = flatten_list(infix_tokens) infix_tokens = flatten_list(infix_tokens)
elif type(infix_tokens) == int: elif self.isNumber(infix_tokens):
infix_tokens = [infix_tokens] infix_tokens = [infix_tokens]
return infix_tokens return infix_tokens
@ -407,17 +410,17 @@ if __name__ == '__main__':
#exp = "( 2 + 5 ) * ( 3 * 4 )" #exp = "( 2 + 5 ) * ( 3 * 4 )"
#test(exp) #test(exp)
#exp = "( 2 + 5 - 1 ) / ( 3 * 4 )" exp = "( 2 + 5 - 1 ) / ( 3 * 4 )"
#test(exp) test(exp)
#exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12" exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12"
#test(exp) test(exp)
#exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 2" exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 2"
#test(exp) test(exp)
#exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12 + 5 * 5" exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12 + 5 * 5"
#test(exp) test(exp)
import doctest import doctest
doctest.testmod() doctest.testmod()