Basic Calculator


Identifying Problem Isomorphism
“Basic Calculator” can be approximately mapped to “Evaluate Reverse Polish Notation” (#150).
“Basic Calculator” asks you to implement a basic calculator that evaluates an arithmetic expression composed of ‘+’, ‘’, ‘(’, and ‘)’.
“Evaluate Reverse Polish Notation” involves evaluating an expression in Reverse Polish notation, where an operator follows its operands, and parentheses are not required due to this postfix notation.
The isomorphism is in the evaluation of arithmetic expressions. Both problems ask to parse an input string representing an arithmetic expression and to calculate the result.
However, the problems differ in the format of the input (infix notation with parentheses in “Basic Calculator” versus postfix notation in “Evaluate Reverse Polish Notation”) and the set of allowed operators. Moreover, “Basic Calculator” requires dealing with operator precedence and using a data structure like a stack to deal with parentheses, while “Evaluate Reverse Polish Notation” does not have these considerations due to the nature of postfix notation.
This is why this is an approximate, rather than an exact, mapping.
“Basic Calculator” requires understanding of stack data structure and the principles of expression evaluation. Here are 10 problems to solve as a preparation:
“Valid Parentheses” (LeetCode Problem #20): This problem will give you a basic understanding of using a stack data structure to handle brackets, which is a basic requirement for handling expressions.
“Min Stack” (LeetCode Problem #155): This problem will help you get a deeper understanding of stack operations.
“Implement Queue using Stacks” (LeetCode Problem #232): This problem will help you understand how to use stacks to simulate other data structures.
“Remove All Adjacent Duplicates In String” (LeetCode Problem #1047): This problem helps you understand how to use a stack to process strings.
“Evaluate Reverse Polish Notation” (LeetCode Problem #150): This problem directly involves calculating expressions using stack, which is a good warmup for the “Basic Calculator” problem.
“Remove Outermost Parentheses” (LeetCode Problem #1021): This problem is a simple exercise in using stacks to handle nested parentheses.
“Remove Invalid Parentheses” (LeetCode Problem #301): A bit more complex, this problem helps further develop the skill of handling parentheses using a stack.
“Daily Temperatures” (LeetCode Problem #739): This problem deals with the monotonic stack which is a frequent use case of the stack data structure.
“Decode String” (LeetCode Problem #394): This problem, while not directly relevant, also involves processing string with stack, and may help develop a better understanding of the stack data structure and string manipulation.
“Next Greater Element I” (LeetCode Problem #496): This problem demonstrates how a stack can be used to keep track of information (in this case, the next greater element) in an array.
These cover stack operations and its applications in string and expression manipulation, preparing you to tackle the “Basic Calculator” problem.
Clarification Questions
What are the clarification questions we can ask about this problem?
Identifying Problem Isomorphism
Can you help me with finding the isomorphism for this problem?
Which problem does this problem map to the corresponding isomorphic problem on Leetcode ?


Language Agnostic Coding Drills
Looping over a sequence: The ability to iterate over each character in a given string or other sequences. In this problem, we are iterating over each character in the input string.
Checking data types: Understanding how to check if a character is a digit or a specific character. In this problem, we are checking if a character is a digit or one of the specific characters ‘+’, ‘’, ‘(’ and ‘)’.
String to integer conversion: Conversion of characters to integers is required when dealing with numerical values. Here, we convert each character to an integer when we determine it’s a digit.
Arithmetic operations: We perform basic arithmetic operations like addition, subtraction, and multiplication in this problem. For example, we calculate the numerical value of numbers with more than one digit, determine the sign of numbers, and calculate the running total.
Conditionals: We use ‘ifelif’ conditionals to check the value of each character and decide what action to take.
Working with stacks: The stack is a data structure that follows the LastInFirstOut (LIFO) principle. We use a stack in this problem to handle parentheses.
The problemsolving approach for this problem involves maintaining a running total of the expression’s value and using a stack to handle parentheses. We iterate over the input string, and when we encounter a digit, we calculate the number’s value. If we encounter a ‘+’ or ‘’, we add the previously calculated number to our result and update the sign. If we encounter an opening parenthesis, we store the current result and sign on the stack and reset them. If we encounter a closing parenthesis, we first add the current number to our result, then we pop the stack twice to get the result and sign before the opening parenthesis, and add the result within the parenthesis to the result before the parenthesis.
Targeted Drills in Python
Here are the corresponding coding drills for the steps listed in Python:
 Looping over a sequence: Write a simple code to loop over a string and print each character.


 Checking data types: Write a code that checks if a character is a digit or a specific character and print a corresponding message.


 String to integer conversion: Write a code that converts a string of digits to an integer.


 Arithmetic operations: Write a code that performs basic arithmetic operations.


 Conditionals: Write a code that checks if a number is positive, negative, or zero.


 Working with stacks: Write a code that demonstrates the basic operations with a stack: pushing, popping, and checking the top element.


These are the individual drills that represent separate skills required for the problem. Practicing these drills will help improve your understanding of these concepts, and combining them will help you solve the problem.