In this question, we need to implement a naive caculator, but thing will never be easu for the rookie like me, so lets go through it.
Description
Given a nested list of integers represented as a string, implement a parser to deserialize it.
Each element is either an integer, or a list – whose elements may also be integers or other lists.
Note: You may assume that the string is well-formed:
- String is non-empty.
- String does not contain white spaces.
- String contains only digits
0-9
,[
,-
,
,]
.
Discuss
From the textbook we learn that the data structure Stack will be of great help to get the value from expression. The following code just implement it with the given rules.
1 | import pdb |
Detail
In fact , this implementation can extended to support more operators, life exponential, and the another question on leetcode Basic Caculator IV is more considering the parentheses.