mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-18 16:27:02 +00:00
Add a program to evaluate a string in prefix notation (Polish Notation) (#1675)
* Create infix_evaluation.py * fix doctests * Rename infix_evaluation.py to prefix_evaluation.py * Add prefix_evaluation.py to directory
This commit is contained in:
parent
2cb6a6523e
commit
4607cd48b6
|
@ -127,6 +127,7 @@
|
|||
* [Stock Span Problem](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stock_span_problem.py)
|
||||
* Trie
|
||||
* [Trie](https://github.com/TheAlgorithms/Python/blob/master/data_structures/trie/trie.py)
|
||||
* [Prefix Evaluation](https://github.com/TheAlgorithms/Python/blob/master/data_structures/prefix_evaluation.py)
|
||||
|
||||
## Digital Image Processing
|
||||
* [Change Contrast](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/change_contrast.py)
|
||||
|
|
60
data_structures/stacks/prefix_evaluation.py
Normal file
60
data_structures/stacks/prefix_evaluation.py
Normal file
|
@ -0,0 +1,60 @@
|
|||
"""
|
||||
Python3 program to evaluate a prefix expression.
|
||||
"""
|
||||
|
||||
calc = {
|
||||
"+": lambda x, y: x + y,
|
||||
"-": lambda x, y: x - y,
|
||||
"*": lambda x, y: x * y,
|
||||
"/": lambda x, y: x / y,
|
||||
}
|
||||
|
||||
|
||||
def is_operand(c):
|
||||
"""
|
||||
Return True if the given char c is an operand, e.g. it is a number
|
||||
|
||||
>>> is_operand("1")
|
||||
True
|
||||
>>> is_operand("+")
|
||||
False
|
||||
"""
|
||||
return c.isdigit()
|
||||
|
||||
|
||||
def evaluate(expression):
|
||||
"""
|
||||
Evaluate a given expression in prefix notation.
|
||||
Asserts that the given expression is valid.
|
||||
|
||||
>>> evaluate("+ 9 * 2 6")
|
||||
21
|
||||
>>> evaluate("/ * 10 2 + 4 1 ")
|
||||
4.0
|
||||
"""
|
||||
stack = []
|
||||
|
||||
# iterate over the string in reverse order
|
||||
for c in expression.split()[::-1]:
|
||||
|
||||
# push operand to stack
|
||||
if is_operand(c):
|
||||
stack.append(int(c))
|
||||
|
||||
else:
|
||||
# pop values from stack can calculate the result
|
||||
# push the result onto the stack again
|
||||
o1 = stack.pop()
|
||||
o2 = stack.pop()
|
||||
stack.append(calc[c](o1, o2))
|
||||
|
||||
return stack.pop()
|
||||
|
||||
|
||||
# Driver code
|
||||
if __name__ == "__main__":
|
||||
test_expression = "+ 9 * 2 6"
|
||||
print(evaluate(test_expression))
|
||||
|
||||
test_expression = "/ * 10 2 + 4 1 "
|
||||
print(evaluate(test_expression))
|
Loading…
Reference in New Issue
Block a user