0

Im working on the code the finding the maximum evaluation (ignoring precedence) such as

2

5+ 3*2

-1 + -4 * -3

Output

16

40000000000

I have the following code which returns the following error:

ValueError: invalid literal for int() with base 10: '-' m[i][i] = int(d[i]) File

Note: I have looked through the other questions with a similar error, but this is different from each of them. Im using Python 3and it docent work.

def get_maximum_value(expression):
    expression="".join(expression.split())
    op = expression[1:len(expression):2]
    d = expression[0:len(expression)+1:2]
    n = len(d)
    m = [[0 for i in range(n)] for j in range(n)]
    M = [[0 for i in range(n)] for j in range(n)]
    for i in range(n):
        m[i][i] = int(d[i])
        M[i][i] = int(d[i])   
    for s in range(1,n):
        for i in range(n-s):
            j = i + s
            m[i][j], M[i][j] = MaxValue(i,j,op,m,M)
        return M[0][n-1]


num=int(input())
for i in range(num):
   expression=input()
   print (get_maximum_value(expression))

However, it shows ValueError: invalid literal for int() with base 10: '-' m[i][i] = int(d[i]).

I tried to change the int to float but it still doesnt work.

Community
  • 1
  • 1
Emily Liu
  • 13
  • 1
  • 4
  • For example if you use this expression: `1 - 2` then by using `split()` you'll end with a list with this form `['1', '-' , '2']` Then by walking through the last list and using `int()` or `float()` you'll ending at applying : `int('-')` which is an invalid literal for int and for float. – Chiheb Nexus Oct 02 '17 at 01:48
  • what should i change to make it work? – Emily Liu Oct 02 '17 at 02:19
  • Enter valid expression which contains only integers. – ksai Oct 02 '17 at 07:00

1 Answers1

0

There is a similar question asked here and I found the answer by SethMMorton useful by using a function to force conversation of float numbers in string type directly to int. Give it a try.

Dutse I
  • 31
  • 2