From b61d395c2c0aebd21a14a918fa776da092d2adda Mon Sep 17 00:00:00 2001 From: Leon Morten Richter <31622033+M0r13n@users.noreply.github.com> Date: Sat, 11 Jan 2020 16:21:41 +0100 Subject: [PATCH 1/4] Create infix_evaluation.py --- data_structures/stacks/infix_evaluation.py | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 data_structures/stacks/infix_evaluation.py diff --git a/data_structures/stacks/infix_evaluation.py b/data_structures/stacks/infix_evaluation.py new file mode 100644 index 000000000000..529f5a696e2e --- /dev/null +++ b/data_structures/stacks/infix_evaluation.py @@ -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 2 ") + 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)) From bc20692a52adf8b455f22c1fc097922b401ba0de Mon Sep 17 00:00:00 2001 From: Leon Morten Richter <31622033+M0r13n@users.noreply.github.com> Date: Sat, 11 Jan 2020 16:26:41 +0100 Subject: [PATCH 2/4] fix doctests --- data_structures/stacks/infix_evaluation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/stacks/infix_evaluation.py b/data_structures/stacks/infix_evaluation.py index 529f5a696e2e..00df2c1e63b0 100644 --- a/data_structures/stacks/infix_evaluation.py +++ b/data_structures/stacks/infix_evaluation.py @@ -27,9 +27,9 @@ def evaluate(expression): Evaluate a given expression in prefix notation. Asserts that the given expression is valid. - >>> evaluate("+ 9 * 2 6") + >>> evaluate("+ 9 * 2 6") 21 - >>> evaluate("/ * 10 2 + 4 2 ") + >>> evaluate("/ * 10 2 + 4 1 ") 4.0 """ stack = [] From cd1f7f198f9a513751ab65d510c66985a7629e16 Mon Sep 17 00:00:00 2001 From: Leon Morten Richter <31622033+M0r13n@users.noreply.github.com> Date: Sat, 11 Jan 2020 16:29:37 +0100 Subject: [PATCH 3/4] Rename infix_evaluation.py to prefix_evaluation.py --- .../stacks/{infix_evaluation.py => prefix_evaluation.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename data_structures/stacks/{infix_evaluation.py => prefix_evaluation.py} (100%) diff --git a/data_structures/stacks/infix_evaluation.py b/data_structures/stacks/prefix_evaluation.py similarity index 100% rename from data_structures/stacks/infix_evaluation.py rename to data_structures/stacks/prefix_evaluation.py From a338bd9f1fc91ce32122c339d0f179ab72a1f9b8 Mon Sep 17 00:00:00 2001 From: Leon Morten Richter <31622033+M0r13n@users.noreply.github.com> Date: Sat, 11 Jan 2020 16:33:25 +0100 Subject: [PATCH 4/4] Add prefix_evaluation.py to directory --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 73ecbc36fdc4..1116e8539536 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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)