
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Program to evaluate one mathematical expression without built-in functions in python
Suppose we have a string that represents a mathematical expression with (+, -, *, /) Here / is representing integer division, we have to evaluate and return the result without using any built-in function.
So, if the input is like s = "2+3*5/7", then the output will be 4, as 2 + ((3 * 5) / 7) = 4
To solve this, we will follow these steps −
- s := reverse the given string
- Define a function get_value().
- sign := 1
- if s is not empty and last element of s is same as "-", then
- delete last element from s
- sign := -1
- value := 0
- while s is not empty and last element of s is a digit, do
- value := value * 10
- value := value + numeric value of last element from s, and delete last element of s
- return sign * value
- Define a function get_term()
- term := get_value()
- while s is not empty and last element of s is either * or /, do
- op := last element of s and delete last element from s
- value := get_value()
- if op is same as "*", then
- term := term * value
- otherwise,
- term := the floor of (1.0 * term / value)
- return term
- From the main method do the following:
- ans := get_term()
- while s is not empty, do
- op := last element of s, and delete it from s
- term := get_term()
- if op is same as "+", then
- ans := ans + term
- otherwise,
- ans := ans - term
- return ans
Let us see the following implementation to get better understanding −
Example
from math import floor, trunc class Solution: def solve(self, s): s = list(s[::-1]) def get_value(): sign = 1 if s and s[-1] == "-": s.pop() sign = -1 value = 0 while s and s[-1].isdigit(): value *= 10 value += int(s.pop()) return sign * value def get_term(): term = get_value() while s and s[-1] in "*/": op = s.pop() value = get_value() if op == "*": term *= value else: term = floor(1.0 * term / value) return term ans = get_term() while s: op, term = s.pop(), get_term() if op == "+": ans += term else: ans -= term return ans ob = Solution() s = "2+3*5/7" print(ob.solve(s))
Input
"2+3*5/7"
Output
4
- Related Articles
- Mathematical Functions in Python?
- Python Program to Take in Two Strings and Display the Larger String without Using Built-in Functions
- Mathematical statistics functions in Python
- Python Mathematical Functions
- Take in Two Strings and Display the Larger String without Using Built-in Functions in Python Program
- Java Program to evaluate mathematical expressions in String
- Mathematical Functions in Python - Special Functions and Constants
- Built-in Tuple Functions in Python
- Mathematical Functions using Python
- Program to evaluate s-expression as string in Python
- Built-in List Functions & Methods in Python
- Built-in Dictionary Functions & Methods in Python
- Program to evaluate Boolean expression from a string in Python?
- Mathematical Functions in Java
- Mathematical Functions in C#

Advertisements