From 005f9c5e396d67d77d83f7f3fba875214ba4d75d Mon Sep 17 00:00:00 2001 From: Rohan Anand <96521078+rohan472000@users.noreply.github.com> Date: Mon, 22 May 2023 09:49:57 +0530 Subject: [PATCH 01/16] Update evaluate_postfix_notations.py --- data_structures/stacks/evaluate_postfix_notations.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index 51ea353b17de..3dae958e2b89 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -17,6 +17,10 @@ def evaluate_postfix(postfix_notation: list) -> int: 9 >>> evaluate_postfix(["4", "13", "5", "/", "+"]) 6 + >>> evaluate_postfix(["2", "+"]) + 2 + >>> evaluate_postfix(["5", "-"]) + -5 >>> evaluate_postfix([]) 0 """ @@ -24,6 +28,8 @@ def evaluate_postfix(postfix_notation: list) -> int: return 0 operations = {"+", "-", "*", "/"} + unary_operations = {"+"} # Unary operator(s) + stack: list[Any] = [] for token in postfix_notation: @@ -40,6 +46,10 @@ def evaluate_postfix(postfix_notation: list) -> int: stack.append(a // b + 1) else: stack.append(a // b) + elif token in unary_operations: # Handle unary operators + operand = stack.pop() + if token == "+": + stack.append(operand) else: stack.append(int(token)) From 4eb81235d71af291591d5124a67c4fd68a837622 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 22 May 2023 04:22:06 +0000 Subject: [PATCH 02/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/stacks/evaluate_postfix_notations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index 3dae958e2b89..df0c90ed3377 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -49,7 +49,7 @@ def evaluate_postfix(postfix_notation: list) -> int: elif token in unary_operations: # Handle unary operators operand = stack.pop() if token == "+": - stack.append(operand) + stack.append(operand) else: stack.append(int(token)) From 7646a57b605020be912386d12981769a9442d2d5 Mon Sep 17 00:00:00 2001 From: Rohan Anand <96521078+rohan472000@users.noreply.github.com> Date: Mon, 22 May 2023 10:05:37 +0530 Subject: [PATCH 03/16] Update evaluate_postfix_notations.py --- data_structures/stacks/evaluate_postfix_notations.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index df0c90ed3377..2932512142b5 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -47,6 +47,8 @@ def evaluate_postfix(postfix_notation: list) -> int: else: stack.append(a // b) elif token in unary_operations: # Handle unary operators + if len(stack) < 1: # Check if there's at least one operand + raise ValueError("Insufficient operands for unary operator") operand = stack.pop() if token == "+": stack.append(operand) From de04120138dad72211311106a36dfcdc9ffe3c3b Mon Sep 17 00:00:00 2001 From: Rohan Anand <96521078+rohan472000@users.noreply.github.com> Date: Mon, 22 May 2023 10:29:14 +0530 Subject: [PATCH 04/16] Update evaluate_postfix_notations.py --- .../stacks/evaluate_postfix_notations.py | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index 2932512142b5..15af8fdba230 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -34,27 +34,32 @@ def evaluate_postfix(postfix_notation: list) -> int: for token in postfix_notation: if token in operations: - b, a = stack.pop(), stack.pop() - if token == "+": - stack.append(a + b) - elif token == "-": - stack.append(a - b) - elif token == "*": - stack.append(a * b) + if token == "-": + if len(stack) < 1: + raise ValueError("Invalid expression: insufficient operands for unary operator") + operand = stack.pop() + stack.append(-operand) else: - if a * b < 0 and a % b != 0: - stack.append(a // b + 1) - else: - stack.append(a // b) - elif token in unary_operations: # Handle unary operators - if len(stack) < 1: # Check if there's at least one operand - raise ValueError("Insufficient operands for unary operator") - operand = stack.pop() - if token == "+": - stack.append(operand) + if len(stack) < 2: + raise ValueError("Invalid expression: insufficient operands for binary operator") + b, a = stack.pop(), stack.pop() + if token == "+": + stack.append(a + b) + elif token == "-": + stack.append(a - b) + elif token == "*": + stack.append(a * b) + elif token == "/": + if b == 0: + raise ValueError("Invalid expression: division by zero") + stack.append(a / b) else: stack.append(int(token)) + if len(stack) != 1: + raise ValueError("Invalid expression: insufficient operators") + + return stack.pop() From 9a92b8268b19a07d7219ceed7940d68b018d8dc4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 22 May 2023 05:00:23 +0000 Subject: [PATCH 05/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/stacks/evaluate_postfix_notations.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index 15af8fdba230..a172e9b060e7 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -36,12 +36,16 @@ def evaluate_postfix(postfix_notation: list) -> int: if token in operations: if token == "-": if len(stack) < 1: - raise ValueError("Invalid expression: insufficient operands for unary operator") + raise ValueError( + "Invalid expression: insufficient operands for unary operator" + ) operand = stack.pop() stack.append(-operand) else: if len(stack) < 2: - raise ValueError("Invalid expression: insufficient operands for binary operator") + raise ValueError( + "Invalid expression: insufficient operands for binary operator" + ) b, a = stack.pop(), stack.pop() if token == "+": stack.append(a + b) @@ -59,7 +63,6 @@ def evaluate_postfix(postfix_notation: list) -> int: if len(stack) != 1: raise ValueError("Invalid expression: insufficient operators") - return stack.pop() From ecd9e743d0b5f60204deff59ba8c32c1ae26f21b Mon Sep 17 00:00:00 2001 From: Rohan Anand <96521078+rohan472000@users.noreply.github.com> Date: Sun, 11 Jun 2023 12:14:04 +0530 Subject: [PATCH 06/16] Update evaluate_postfix_notations.py --- .../stacks/evaluate_postfix_notations.py | 20 +++++-------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index a172e9b060e7..57c370e6f5b1 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -28,24 +28,17 @@ def evaluate_postfix(postfix_notation: list) -> int: return 0 operations = {"+", "-", "*", "/"} - unary_operations = {"+"} # Unary operator(s) - stack: list[Any] = [] for token in postfix_notation: if token in operations: - if token == "-": - if len(stack) < 1: - raise ValueError( - "Invalid expression: insufficient operands for unary operator" - ) + if token == "-" and len(stack) < 2: operand = stack.pop() stack.append(-operand) + elif (token == "+" or "/" or "*") and len(stack) < 2: + operand = stack.pop() + stack.append(operand) else: - if len(stack) < 2: - raise ValueError( - "Invalid expression: insufficient operands for binary operator" - ) b, a = stack.pop(), stack.pop() if token == "+": stack.append(a + b) @@ -56,13 +49,10 @@ def evaluate_postfix(postfix_notation: list) -> int: elif token == "/": if b == 0: raise ValueError("Invalid expression: division by zero") - stack.append(a / b) + stack.append(a // b) else: stack.append(int(token)) - if len(stack) != 1: - raise ValueError("Invalid expression: insufficient operators") - return stack.pop() From 3559a558838c8415250a7ff98708823dcc8367d8 Mon Sep 17 00:00:00 2001 From: Rohan Anand <96521078+rohan472000@users.noreply.github.com> Date: Sun, 11 Jun 2023 12:18:29 +0530 Subject: [PATCH 07/16] Update evaluate_postfix_notations.py --- data_structures/stacks/evaluate_postfix_notations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index 57c370e6f5b1..8cb1edce3a16 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -35,7 +35,7 @@ def evaluate_postfix(postfix_notation: list) -> int: if token == "-" and len(stack) < 2: operand = stack.pop() stack.append(-operand) - elif (token == "+" or "/" or "*") and len(stack) < 2: + elif (True) and len(stack) < 2: operand = stack.pop() stack.append(operand) else: From 7f6a6925360c0a68bc1074a39b87b51b39998418 Mon Sep 17 00:00:00 2001 From: Rohan Anand <96521078+rohan472000@users.noreply.github.com> Date: Mon, 12 Jun 2023 13:39:11 +0530 Subject: [PATCH 08/16] Update data_structures/stacks/evaluate_postfix_notations.py Co-authored-by: AmirSoroush <114881632+amirsoroush@users.noreply.github.com> --- data_structures/stacks/evaluate_postfix_notations.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index 8cb1edce3a16..141e0bc6cdea 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -32,11 +32,10 @@ def evaluate_postfix(postfix_notation: list) -> int: for token in postfix_notation: if token in operations: - if token == "-" and len(stack) < 2: - operand = stack.pop() - stack.append(-operand) - elif (True) and len(stack) < 2: + if len(stack) < 2: operand = stack.pop() + if token == "-": + operand = -operand stack.append(operand) else: b, a = stack.pop(), stack.pop() From 748eff518224c64b21e112b3cc4d4190d0c0c88e Mon Sep 17 00:00:00 2001 From: Rohan Anand <96521078+rohan472000@users.noreply.github.com> Date: Mon, 12 Jun 2023 17:04:03 +0530 Subject: [PATCH 09/16] Update data_structures/stacks/evaluate_postfix_notations.py Co-authored-by: Christian Clauss --- data_structures/stacks/evaluate_postfix_notations.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index 141e0bc6cdea..fdf079a50ef9 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -49,6 +49,8 @@ def evaluate_postfix(postfix_notation: list) -> int: if b == 0: raise ValueError("Invalid expression: division by zero") stack.append(a // b) + else: + raise ValueError(f"Unrecognized {token = }") else: stack.append(int(token)) From 816120b7ed60c2a62b0efc7ca252d66f49f4b7f4 Mon Sep 17 00:00:00 2001 From: Rohan Anand <96521078+rohan472000@users.noreply.github.com> Date: Mon, 12 Jun 2023 17:04:15 +0530 Subject: [PATCH 10/16] Update data_structures/stacks/evaluate_postfix_notations.py Co-authored-by: Christian Clauss --- data_structures/stacks/evaluate_postfix_notations.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index fdf079a50ef9..b0acf2d6d32f 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -36,6 +36,8 @@ def evaluate_postfix(postfix_notation: list) -> int: operand = stack.pop() if token == "-": operand = -operand + else: + raise ValueError(f"Unrecognized {token = }") stack.append(operand) else: b, a = stack.pop(), stack.pop() From 9e6f36afe0f45cea802df7fc57aa40662756d16e Mon Sep 17 00:00:00 2001 From: Rohan Anand <96521078+rohan472000@users.noreply.github.com> Date: Mon, 12 Jun 2023 17:44:36 +0530 Subject: [PATCH 11/16] Update data_structures/stacks/evaluate_postfix_notations.py Co-authored-by: Christian Clauss --- data_structures/stacks/evaluate_postfix_notations.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index b0acf2d6d32f..a57eb0316da9 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -52,7 +52,8 @@ def evaluate_postfix(postfix_notation: list) -> int: raise ValueError("Invalid expression: division by zero") stack.append(a // b) else: - raise ValueError(f"Unrecognized {token = }") + msg = f"Unrecognized {token = }" + raise ValueError(msg) else: stack.append(int(token)) From 2779cd06b3263c8a55dface924ba9bac5564d04d Mon Sep 17 00:00:00 2001 From: Rohan Anand <96521078+rohan472000@users.noreply.github.com> Date: Tue, 13 Jun 2023 00:26:49 +0530 Subject: [PATCH 12/16] Update evaluate_postfix_notations.py --- .../stacks/evaluate_postfix_notations.py | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index a57eb0316da9..48f6c7abdb84 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -23,7 +23,22 @@ def evaluate_postfix(postfix_notation: list) -> int: -5 >>> evaluate_postfix([]) 0 - """ + >>> evaluate_postfix(["5"]) + 5 + >>> evaluate_postfix(["-"]) + 0 + >>> evaluate_postfix(["5", "A"]) + Traceback (most recent call last): + ... + ValueError: invalid literal for int() with base 10: 'A' + >>> evaluate_postfix(["5", "4", "A"]) + Traceback (most recent call last): + ... + ValueError: invalid literal for int() with base 10: 'A' + >>> evaluate_postfix(["A"]) + Traceback (most recent call last): + ... + ValueError: invalid literal for int() with base 10: 'A'""" if not postfix_notation: return 0 @@ -35,10 +50,9 @@ def evaluate_postfix(postfix_notation: list) -> int: if len(stack) < 2: operand = stack.pop() if token == "-": - operand = -operand + stack.append(-operand) else: - raise ValueError(f"Unrecognized {token = }") - stack.append(operand) + stack.append(operand) else: b, a = stack.pop(), stack.pop() if token == "+": From 2bbac3728271f706b83d157e1f296fd4566192c5 Mon Sep 17 00:00:00 2001 From: Rohan Anand <96521078+rohan472000@users.noreply.github.com> Date: Tue, 13 Jun 2023 00:42:34 +0530 Subject: [PATCH 13/16] Update evaluate_postfix_notations.py --- data_structures/stacks/evaluate_postfix_notations.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index 48f6c7abdb84..40e7da2f5c0f 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -25,8 +25,6 @@ def evaluate_postfix(postfix_notation: list) -> int: 0 >>> evaluate_postfix(["5"]) 5 - >>> evaluate_postfix(["-"]) - 0 >>> evaluate_postfix(["5", "A"]) Traceback (most recent call last): ... @@ -39,6 +37,9 @@ def evaluate_postfix(postfix_notation: list) -> int: Traceback (most recent call last): ... ValueError: invalid literal for int() with base 10: 'A'""" + if len(postfix_notation) == 1 and postfix_notation[0] in {"+", "-", "*", "/"}: + raise ValueError("Invalid expression: insufficient operands") + if not postfix_notation: return 0 From c4f050e03d727e3586f669bef6de62a6d10805a9 Mon Sep 17 00:00:00 2001 From: Rohan Anand <96521078+rohan472000@users.noreply.github.com> Date: Tue, 13 Jun 2023 00:58:31 +0530 Subject: [PATCH 14/16] Update data_structures/stacks/evaluate_postfix_notations.py Co-authored-by: Tianyi Zheng --- data_structures/stacks/evaluate_postfix_notations.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index 40e7da2f5c0f..5aecad91bf28 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -37,9 +37,6 @@ def evaluate_postfix(postfix_notation: list) -> int: Traceback (most recent call last): ... ValueError: invalid literal for int() with base 10: 'A'""" - if len(postfix_notation) == 1 and postfix_notation[0] in {"+", "-", "*", "/"}: - raise ValueError("Invalid expression: insufficient operands") - if not postfix_notation: return 0 From c3a7c671d2b931aebcf566dac0c29e21a86b9e68 Mon Sep 17 00:00:00 2001 From: Rohan Anand <96521078+rohan472000@users.noreply.github.com> Date: Tue, 13 Jun 2023 00:58:55 +0530 Subject: [PATCH 15/16] Update data_structures/stacks/evaluate_postfix_notations.py Co-authored-by: Tianyi Zheng --- data_structures/stacks/evaluate_postfix_notations.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index 5aecad91bf28..5196b374248b 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -45,12 +45,12 @@ def evaluate_postfix(postfix_notation: list) -> int: for token in postfix_notation: if token in operations: - if len(stack) < 2: + if len(stack) == 0: + msg = f"{token} operator with no operand(s)" + raise ValueError(msg) + if token == "-" and len(stack) < 2: operand = stack.pop() - if token == "-": - stack.append(-operand) - else: - stack.append(operand) + stack.append(-operand) else: b, a = stack.pop(), stack.pop() if token == "+": From 29b28ecba4f6a77927c7e8bcc8ab725f7fdf044a Mon Sep 17 00:00:00 2001 From: Rohan Anand <96521078+rohan472000@users.noreply.github.com> Date: Tue, 13 Jun 2023 23:17:46 +0530 Subject: [PATCH 16/16] Update evaluate_postfix_notations.py --- .../stacks/evaluate_postfix_notations.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index 5196b374248b..40e7da2f5c0f 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -37,6 +37,9 @@ def evaluate_postfix(postfix_notation: list) -> int: Traceback (most recent call last): ... ValueError: invalid literal for int() with base 10: 'A'""" + if len(postfix_notation) == 1 and postfix_notation[0] in {"+", "-", "*", "/"}: + raise ValueError("Invalid expression: insufficient operands") + if not postfix_notation: return 0 @@ -45,12 +48,12 @@ def evaluate_postfix(postfix_notation: list) -> int: for token in postfix_notation: if token in operations: - if len(stack) == 0: - msg = f"{token} operator with no operand(s)" - raise ValueError(msg) - if token == "-" and len(stack) < 2: + if len(stack) < 2: operand = stack.pop() - stack.append(-operand) + if token == "-": + stack.append(-operand) + else: + stack.append(operand) else: b, a = stack.pop(), stack.pop() if token == "+":