HELP to solve this question in Python

There is an inbuilt function in Python to evaluate Mathematical expressions. If you are just supposed to find the result of evaluation of given expression, use eval() in Python.

s = "(1+(2.4/3))"
# Output: 1.8

If you are supposed to write mentioned functions, then you might want to think :wink:
PS: You may ask again if you need to write those functions

actually I have to write the codes for those fun.
please help

Consider this. Basically you are supposed to evaluate a sub-section of expression.

  1. Implement a function that takes the two arguments and returns the tuple, (number starting at i^{th} character, index).
def readNumber(expression, ind): # Expression is the mathematical expression, ind is the index
    valid = "0123456789."
    number = ""
    while ind < len(expression) and expression[ind] in valid:
        number += expression[ind]
        ind += 1
    return (float(number), ind)
  1. Implement a function that evaluates a single parenthesised sub expression.
def evalParen(expression, ind):
    temp = ind
    count = 1 if expression[ind] == "(" else 0
    ind += 1
    while ind < len(expression) and count>0:
        if expression[ind] == "(":
            count += 1
        elif expression[ind] == ")":
            count -= 1
        ind += 1
    return (float(eval(expression[temp:ind])), ind)

I guess this should work. If it isn’t, please let me know

1 Like

yup its working
thankyou so much