From c4f248dd10440e80633574cbfcfbf1f189b4da94 Mon Sep 17 00:00:00 2001 From: Isidro Arias Date: Mon, 31 Mar 2025 11:58:11 +0200 Subject: [PATCH 1/7] prefix_evaluation: Add alternative recursive implementation --- data_structures/stacks/prefix_evaluation.py | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/data_structures/stacks/prefix_evaluation.py b/data_structures/stacks/prefix_evaluation.py index f48eca23d7b5..7b72b5c6c405 100644 --- a/data_structures/stacks/prefix_evaluation.py +++ b/data_structures/stacks/prefix_evaluation.py @@ -50,6 +50,30 @@ def evaluate(expression): return stack.pop() +def evaluate_recursive(expression: list): + """ + Alternative implementation + + >>> evaluate_recursive(['2']) + 2.0 + >>> expression = ['+', '*', '2', '3', '/', '8', 4] + >>> evaluate_recursive(expression) + 8.0 + >>> expression + [] + """ + + op = expression.pop(0) + if op not in calc: + return float(op) + + operation = calc[op] + + a = evaluate_recursive(expression) + b = evaluate_recursive(expression) + return operation(a, b) + + # Driver code if __name__ == "__main__": test_expression = "+ 9 * 2 6" From 128fc1ea9977e6043ed3689911800fc8b4e12e9d Mon Sep 17 00:00:00 2001 From: Isidro Arias Date: Mon, 31 Mar 2025 11:59:54 +0200 Subject: [PATCH 2/7] improve doc --- data_structures/stacks/prefix_evaluation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/data_structures/stacks/prefix_evaluation.py b/data_structures/stacks/prefix_evaluation.py index 7b72b5c6c405..57ab2c7349c3 100644 --- a/data_structures/stacks/prefix_evaluation.py +++ b/data_structures/stacks/prefix_evaluation.py @@ -1,5 +1,6 @@ """ -Python3 program to evaluate a prefix expression. +Program to evaluate a prefix expression. +https://en.wikipedia.org/wiki/Polish_notation """ calc = { From d80a17ef65004d05efa141a6e9eb6183b184261b Mon Sep 17 00:00:00 2001 From: Isidro Arias Date: Mon, 31 Mar 2025 12:00:37 +0200 Subject: [PATCH 3/7] better variable name calc->operators --- data_structures/stacks/prefix_evaluation.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/data_structures/stacks/prefix_evaluation.py b/data_structures/stacks/prefix_evaluation.py index 57ab2c7349c3..d99b69ec9848 100644 --- a/data_structures/stacks/prefix_evaluation.py +++ b/data_structures/stacks/prefix_evaluation.py @@ -3,7 +3,7 @@ https://en.wikipedia.org/wiki/Polish_notation """ -calc = { +operators = { "+": lambda x, y: x + y, "-": lambda x, y: x - y, "*": lambda x, y: x * y, @@ -42,11 +42,11 @@ def evaluate(expression): stack.append(int(c)) else: - # pop values from stack can calculate the result + # pop values from stack can operatorsulate the result # push the result onto the stack again o1 = stack.pop() o2 = stack.pop() - stack.append(calc[c](o1, o2)) + stack.append(operators[c](o1, o2)) return stack.pop() @@ -65,10 +65,10 @@ def evaluate_recursive(expression: list): """ op = expression.pop(0) - if op not in calc: + if op not in operators: return float(op) - operation = calc[op] + operation = operators[op] a = evaluate_recursive(expression) b = evaluate_recursive(expression) From 220a70573a62be672db974d4a8bf9ed12f87ae0a Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 31 Mar 2025 23:53:36 +0300 Subject: [PATCH 4/7] Update prefix_evaluation.py --- data_structures/stacks/prefix_evaluation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/stacks/prefix_evaluation.py b/data_structures/stacks/prefix_evaluation.py index d99b69ec9848..03552e9c3008 100644 --- a/data_structures/stacks/prefix_evaluation.py +++ b/data_structures/stacks/prefix_evaluation.py @@ -42,7 +42,7 @@ def evaluate(expression): stack.append(int(c)) else: - # pop values from stack can operatorsulate the result + # pop values from stack can calculate the result # push the result onto the stack again o1 = stack.pop() o2 = stack.pop() From 7d24573934b8a6e2da1e0304bb385059c5cdc405 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 1 Apr 2025 00:01:46 +0300 Subject: [PATCH 5/7] Update prefix_evaluation.py --- data_structures/stacks/prefix_evaluation.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/data_structures/stacks/prefix_evaluation.py b/data_structures/stacks/prefix_evaluation.py index 03552e9c3008..20d9d63bb8e9 100644 --- a/data_structures/stacks/prefix_evaluation.py +++ b/data_structures/stacks/prefix_evaluation.py @@ -32,6 +32,10 @@ def evaluate(expression): 21 >>> evaluate("/ * 10 2 + 4 1 ") 4.0 + >>> evaluate("2") + 2 + >>> evaluate("+ * 2 3 / 8 4") + 8.0 """ stack = [] @@ -51,22 +55,26 @@ def evaluate(expression): return stack.pop() -def evaluate_recursive(expression: list): +def evaluate_recursive(expression: list[str]): """ Alternative implementation >>> evaluate_recursive(['2']) - 2.0 - >>> expression = ['+', '*', '2', '3', '/', '8', 4] + 2 + >>> expression = ['+', '*', '2', '3', '/', '8', '4'] >>> evaluate_recursive(expression) 8.0 >>> expression [] + >>> evaluate(['+', '9', '*', '2', '6']) + 21 + >>> evaluate(['/', '*', '10', '2', '+', '4', '1']) + 4.0 """ op = expression.pop(0) - if op not in operators: - return float(op) + if is_operand(op): + return int(op) operation = operators[op] From 82d946ac6cf2c8adf6f874458f78617dcfaf47ba Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 1 Apr 2025 00:03:52 +0300 Subject: [PATCH 6/7] Update prefix_evaluation.py --- data_structures/stacks/prefix_evaluation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/stacks/prefix_evaluation.py b/data_structures/stacks/prefix_evaluation.py index 20d9d63bb8e9..c0d9e0440cfe 100644 --- a/data_structures/stacks/prefix_evaluation.py +++ b/data_structures/stacks/prefix_evaluation.py @@ -66,9 +66,9 @@ def evaluate_recursive(expression: list[str]): 8.0 >>> expression [] - >>> evaluate(['+', '9', '*', '2', '6']) + >>> evaluate_recursive(['+', '9', '*', '2', '6']) 21 - >>> evaluate(['/', '*', '10', '2', '+', '4', '1']) + >>> evaluate_recursive(['/', '*', '10', '2', '+', '4', '1']) 4.0 """ From 75ccb733d95b4a80919fdf2887d34ee8e9c76f88 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 1 Apr 2025 00:04:52 +0300 Subject: [PATCH 7/7] Update prefix_evaluation.py --- data_structures/stacks/prefix_evaluation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/stacks/prefix_evaluation.py b/data_structures/stacks/prefix_evaluation.py index c0d9e0440cfe..03a70d884725 100644 --- a/data_structures/stacks/prefix_evaluation.py +++ b/data_structures/stacks/prefix_evaluation.py @@ -57,7 +57,7 @@ def evaluate(expression): def evaluate_recursive(expression: list[str]): """ - Alternative implementation + Alternative recursive implementation >>> evaluate_recursive(['2']) 2