Ask your own question, for FREE!
Lists & Dictionaries 8 Online
OpenStudy (anonymous):

how do you convert a string such as (2+3)*4+(4+5) to a list such as [[2,"+",3],"*",4,"+",[4,"+",5]] and evaluate the value of the expression??

OpenStudy (e.mccormick):

You would need to examine the string using some sort of a loop and find all the parenthesis first. This would allow you to build the order of operations into your evaluation system. In fact, working with the order of operations would be the real key. That would let you place things in the list in the order they need to be evaluated to get an answer. Also, your evaluation routine would need to loop or recurs based on if it finds a list within the list.

OpenStudy (anonymous):

use a stack..

OpenStudy (e.mccormick):

@A.Avinash_Goutham He posted in the MIT 6.189 area, which is an Intro to Python class. In Python, the list can be used as a stack. With append() and pop() it becomes a LIFO data structure.

OpenStudy (anonymous):

i got the logic. But the problem is i am not being able to write the proper code.

OpenStudy (e.mccormick):

Well, what did you come up with?

OpenStudy (anonymous):

@sidd607 , while not my solution, here's what google brings up - http://stackoverflow.com/questions/2371436/evaluating-a-mathematical-expression-in-a-string in other words - there is no simple solution to this as you have to parse numerical strings that can get pretty hairy...

OpenStudy (goformit100):

A warm Welcome to OpenStudy. I can help guide you through this useful site. You can ask your questions to me or you can message me. Please use the chat for off topic questions. Remember to give the person who helped you a medal by clicking on "Best Answer". We follow a code of conduct, ( http://openstudy.com/code-of-conduct ). Please take a moment to read it.q

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!