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>You can write your expression in the form</p>

<p><code>expression = term + term + ... + term</code></p>

<p>and a term as</p>

<p><code>term = value * value * ... * value</code></p>

<p>This is an informal way of specifying the <em>grammar</em> for your expression. You ought to recognise that the evaluation of an expression of this form <strong>does not</strong> require a recursive descent parser which simplifies things greatly.</p>

<p>An O(N) solution technique is then to evaluate each <code>term</code> from left to right; calling a function on each step that evaluates a particular <code>term</code>.</p>

python math equation