
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to evaluate s-expression as string in Python
Suppose we have a string s as S-expression. We have to evaluate that S-expression and return result as integer. As we know that the s-expression is an expression which is either one number, or a recursive expression wrapped in parentheses like (+ (- 3 2) (* 3 3)), which indicates (3 - 2) + (3 * 3) = 10. Here valid operators are +, -, *, and /.
So, if the input is like s = "(- (+ 3 2) 2)", then the output will be 3, as ((3 + 2) - 2) = 3.
To solve this, we will follow these steps −
stack := a new stack
remove the opening and closing parenthesis of s
a := split s using spaces and make a list of partitions
for each i in a in reverse order, do
if size of i > 1, then
if i[0] is same as "-", then
push i as integer into stack
go for next iteration
otherwise,
push i as integer into stack
otherwise when i is a digit, then
push i as integer into stack
otherwise,
if size of stack >= 2, then
num1 := pop from stack
num2 := pop from stack
if i is same as "+", then
perform num1 + num2 and push into stack
otherwise when i is same as "-", then
perform num1 - num2 and push into stack
otherwise when i is same as "*", then
perform num1 * num2 and push into stack
otherwise,
perform num1 / num2 and push into stack
return top element from stack, and remove top element
Example
Let us see the following implementation to get a better understanding −
class Solution: def solve(self, s): stack = list() s = s.replace("(", "") s = s.replace(")", "") a = s.split() for i in a[::-1]: if len(i) > 1: if i[0] == "-": stack.append(int(i)) continue else: stack.append(int(i)) elif i.isdigit(): stack.append(int(i)) else: if len(stack) >= 2: num1 = stack.pop() num2 = stack.pop() if i == "+": stack.append(int(num1 + num2)) elif i == "-": stack.append(int(num1 - num2)) elif i == "*": stack.append(int(num1 * num2)) else: stack.append(int(num1 / num2)) return stack.pop() ob = Solution() s = "(- (+ 3 2) 2)" print(ob.solve(s))
Input
s = "(- (+ 3 2) 2)"
Output
3
- Related Articles
- Program to evaluate Boolean expression from a string in Python?
- Evaluate a boolean expression represented as string in C++
- Program to build and evaluate an expression tree using Python
- Program to evaluate ternary expression in C++
- Program to evaluate one mathematical expression without built-in functions in python
- C++ Program to Evaluate an Expression using Stacks
- Java Program to evaluate mathematical expressions in String
- Program to expand string represented as n(t) format in Python
- Program to check regular expression pattern is matching with string or not in Python
- Evaluate Postfix Expression
- Evaluating a string as a mathematical expression in JavaScript
- Evaluate the lowest cost contraction order for an einsum expression in Python
- Program to count number of operations needed to make string as concatenation of same string twice in Python
- Java Program to split a string using Regular Expression
- Python Program to Construct an Expression Tree of a given Expression
