change form of steps. from str to postfix list

This commit is contained in:
lafrite 2013-11-16 20:50:42 +01:00
parent 438d242b48
commit c3862489df
3 changed files with 23 additions and 14 deletions

View File

@ -83,7 +83,7 @@ class Expression(object):
steps = expand_list(tmpTokenList) steps = expand_list(tmpTokenList)
if len(steps[:-1]) > 0: if len(steps[:-1]) > 0:
self.steps += steps[:-1] self.steps += [flatten_list(s) for s in steps[:-1]]
self.child = Expression(steps[-1]) self.child = Expression(steps[-1])
@ -417,14 +417,14 @@ if __name__ == '__main__':
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)
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'

View File

@ -34,7 +34,8 @@ class Fraction(object):
elif gcd_ != 1: elif gcd_ != 1:
n_frac = Fraction(self._num // gcd_ , self._denom // gcd_) n_frac = Fraction(self._num // gcd_ , self._denom // gcd_)
steps.append("( {reste1} * {gcd} ) / ( {reste2} * {gcd} )".format(reste1 = n_frac._num, reste2 = n_frac._denom, gcd = gcd_)) #steps.append("( {reste1} * {gcd} ) / ( {reste2} * {gcd} )".format(reste1 = n_frac._num, reste2 = n_frac._denom, gcd = gcd_))
steps.append([n_frac._num, gcd_, '*', n_frac._denom, 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 # 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(n_frac) steps.append(n_frac)
@ -69,13 +70,17 @@ class Fraction(object):
coef1 = number._denom // gcd_denom coef1 = number._denom // gcd_denom
coef2 = self._denom // gcd_denom coef2 = self._denom // gcd_denom
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)) #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))
steps.append([self._num, coef1, "*", self._denom, coef1, "*", '/', number._num, coef2, "*", number._denom, coef2, "*", "/",'+'])
com_denom = self._denom * coef1 com_denom = self._denom * coef1
num1 = self._num * coef1 num1 = self._num * coef1
num2 = number._num * coef2 num2 = number._num * coef2
steps.append("( {num1} + {num2} ) / {denom}".format(num1 = num1, num2 = num2, denom = com_denom)) #steps.append("( {num1} + {num2} ) / {denom}".format(num1 = num1, num2 = num2, denom = com_denom))
steps.append([num1, num2, '+', com_denom, '/'])
num = num1 + num2 num = num1 + num2
@ -104,13 +109,15 @@ class Fraction(object):
coef1 = number._denom // gcd_denom coef1 = number._denom // gcd_denom
coef2 = self._denom // gcd_denom coef2 = self._denom // gcd_denom
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)) #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))
steps.append([self._num, coef1, "*", self._denom, coef1, "*", '/', number._num, coef2, "*", number._denom, coef2, "*", "/",'-'])
com_denom = self._denom * coef1 com_denom = self._denom * coef1
num1 = self._num * coef1 num1 = self._num * coef1
num2 = number._num * coef2 num2 = number._num * coef2
steps.append("( {num1} - {num2} ) / {denom}".format(num1 = num1, num2 = num2, denom = com_denom)) #steps.append("( {num1} - {num2} ) / {denom}".format(num1 = num1, num2 = num2, denom = com_denom))
steps.append([num1, num2, '-', com_denom, '/'])
num = num1 - num2 num = num1 - num2
@ -128,7 +135,9 @@ class Fraction(object):
number = Fraction(other) number = Fraction(other)
steps = [] steps = []
steps.append("( {num1} * {num2} ) / ( {denom1} * {denom2} )".format(num1 = self._num, num2 = number._num, denom1 = self._denom, denom2 = number._denom)) #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, '*', '/'])
num = self._num * number._num num = self._num * number._num
denom = self._denom * number._denom denom = self._denom * number._denom

View File

@ -94,7 +94,7 @@ def expand_list(list_list):
for (j,e) in enumerate(l): for (j,e) in enumerate(l):
if type(e) == list: if type(e) == 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 de liste dans la liste (2e exemple)
except ValueError: except ValueError:
ans = [list_list] ans = [list_list]