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.

3
  • 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. Commented Oct 2, 2017 at 1:48
  • what should i change to make it work? Commented Oct 2, 2017 at 2:19
  • Enter valid expression which contains only integers. Commented Oct 2, 2017 at 7:00

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.