// You are building an educational website and want to create a simple
// calculator for students to use. The calculator will only allow
// addition and subtraction of positive integers.
//
// We also want to allow parentheses in our input. Given an expression
// string using the "+", "-", "(", and ")" operators like "5+(16-2)",
// write a function to parse the string and evaluate the result.
// Sample input:
// expression1 = "5+16-((9-6)-(4-2))"
// expression2 = "22+(2-4)"
// Sample output:
// evaluate(expression1) => 20
// evaluate(expression2) => 20
Recently, I did a live session where I was challenged to implement some code in a short period of time. I got flustered, and I diverged from my usually strict principles of code etiquette and wrote some messy code that never ended up working by the end of the session. I had an elegant solution in mind, but I was worried about time and ended up with this mess.
Continue reading…