Calculate the equation given a string
<p>Given a random equation (with plus and multiply), calculate the result. </p>

<p>For example, given, "5 + 3 * 4 * 2 + 1", return 30.</p>

<p>This was a question I was given on a programming interview (a year ago) and was told to do it in at least 0(n) time. </p>

<p>I attempted to do it in python. </p>

<p>Another solution would be to convert your (infix) math expression to postfix, which is easy to evaluate using a stack. If you're interested here's a nice site describing the conversion as well as the evaluation/calcuation of a postfix-expression:</p>

<p><a href="http://interactivepython.org/runestone/static/pythonds/BasicDS/InfixPrefixandPostfixExpressions.html" rel="nofollow">http://interactivepython.org/runestone/static/pythonds/BasicDS/InfixPrefixandPostfixExpressions.html</a></p>

<p>Computational Complexity for converting infix to postfix and evaluating the expression would be about O(3*n) which in fact is still O(n).</p>

python math equation