From c848c3ccaaaf7a215fcea9282ca929332e1c77c4 Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Thu, 1 Oct 2020 19:04:57 +0530 Subject: [PATCH 01/12] Create evaluate_postfix_notations.py Adding in the evaluate postfix notation using Stacks one of the common use with simple stack question creating a new file for the data structure of stacks --- data_structures/evaluate_postfix_notations.py | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 data_structures/evaluate_postfix_notations.py diff --git a/data_structures/evaluate_postfix_notations.py b/data_structures/evaluate_postfix_notations.py new file mode 100644 index 000000000000..608f6418b3a6 --- /dev/null +++ b/data_structures/evaluate_postfix_notations.py @@ -0,0 +1,41 @@ +''' + +The Reverse Polish Nation also known as Polish postfix notation or simply postfix notation +https://en.wikipedia.org/wiki/Reverse_Polish_notation + +Classic examples of simple stack implementations +Valid operators are +, -, *, /. Each operand may be an integer or another expression. + +''' +def evaluate_postfix_notation(postfix_notation): + operations = {'+', '-', '*', '/'} + stack = [] + + 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) + else: + if a * b < 0 and a % b != 0: + stack.append(a // b + 1) + else: + stack.append(a // b) + else: + stack.append(int(token)) + + return stack.pop() + + +assert evaluate_postfix_notation(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]) == 22 +# (10 * (6 / ((9 + 3) * -11))) + 17) + 5 + +assert evaluate_postfix_notation(["2", "1", "+", "3", "*"]) == 9 +# ((2 + 1) * 3) = 9 + +assert evaluate_postfix_notation(["4", "13", "5", "/", "+"]) == 6 +# (4 + (13 / 5)) = 6 From 0ac661109551773b669b5c555c148086b949aa92 Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Thu, 1 Oct 2020 19:07:28 +0530 Subject: [PATCH 02/12] Create evaluate_postfix_notations.py Adding in the evaluate postfix notation using Stacks one of the common use with simple stack question creating a new file for the data structure of stacks --- .../stacks/evaluate_postfix_notations.py | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 data_structures/stacks/evaluate_postfix_notations.py diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py new file mode 100644 index 000000000000..e96c145c0a5b --- /dev/null +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -0,0 +1,38 @@ +''' +The Reverse Polish Nation also known as Polish postfix notation or simply postfix notation +https://en.wikipedia.org/wiki/Reverse_Polish_notation +Classic examples of simple stack implementations +Valid operators are +, -, *, /. Each operand may be an integer or another expression. +''' +def evaluate_postfix_notation(postfix_notation): + operations = {'+', '-', '*', '/'} + stack = [] + + 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) + else: + if a * b < 0 and a % b != 0: + stack.append(a // b + 1) + else: + stack.append(a // b) + else: + stack.append(int(token)) + + return stack.pop() + + +assert evaluate_postfix_notation(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]) == 22 +# (10 * (6 / ((9 + 3) * -11))) + 17) + 5 + +assert evaluate_postfix_notation(["2", "1", "+", "3", "*"]) == 9 +# ((2 + 1) * 3) = 9 + +assert evaluate_postfix_notation(["4", "13", "5", "/", "+"]) == 6 +# (4 + (13 / 5)) = 6 From 73f61b256bfe8c98ddd68eb87ded00a789633105 Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Thu, 1 Oct 2020 19:08:49 +0530 Subject: [PATCH 03/12] Delete evaluate_postfix_notations.py --- data_structures/evaluate_postfix_notations.py | 41 ------------------- 1 file changed, 41 deletions(-) delete mode 100644 data_structures/evaluate_postfix_notations.py diff --git a/data_structures/evaluate_postfix_notations.py b/data_structures/evaluate_postfix_notations.py deleted file mode 100644 index 608f6418b3a6..000000000000 --- a/data_structures/evaluate_postfix_notations.py +++ /dev/null @@ -1,41 +0,0 @@ -''' - -The Reverse Polish Nation also known as Polish postfix notation or simply postfix notation -https://en.wikipedia.org/wiki/Reverse_Polish_notation - -Classic examples of simple stack implementations -Valid operators are +, -, *, /. Each operand may be an integer or another expression. - -''' -def evaluate_postfix_notation(postfix_notation): - operations = {'+', '-', '*', '/'} - stack = [] - - 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) - else: - if a * b < 0 and a % b != 0: - stack.append(a // b + 1) - else: - stack.append(a // b) - else: - stack.append(int(token)) - - return stack.pop() - - -assert evaluate_postfix_notation(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]) == 22 -# (10 * (6 / ((9 + 3) * -11))) + 17) + 5 - -assert evaluate_postfix_notation(["2", "1", "+", "3", "*"]) == 9 -# ((2 + 1) * 3) = 9 - -assert evaluate_postfix_notation(["4", "13", "5", "/", "+"]) == 6 -# (4 + (13 / 5)) = 6 From e9334a10952251fd13d31ce6fdc0dd4a795181a7 Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Thu, 1 Oct 2020 22:42:14 +0530 Subject: [PATCH 04/12] Evaluate postfix expression stack clean approach Sending in the PR again as the Previous request failed in pre commit --- .../stacks/evaluate_postfix_notations.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index e96c145c0a5b..938bd78074ce 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -4,7 +4,7 @@ Classic examples of simple stack implementations Valid operators are +, -, *, /. Each operand may be an integer or another expression. ''' -def evaluate_postfix_notation(postfix_notation): +def evaluate_postfix(postfix_notation): operations = {'+', '-', '*', '/'} stack = [] @@ -24,15 +24,9 @@ def evaluate_postfix_notation(postfix_notation): stack.append(a // b) else: stack.append(int(token)) - return stack.pop() -assert evaluate_postfix_notation(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]) == 22 -# (10 * (6 / ((9 + 3) * -11))) + 17) + 5 - -assert evaluate_postfix_notation(["2", "1", "+", "3", "*"]) == 9 -# ((2 + 1) * 3) = 9 - -assert evaluate_postfix_notation(["4", "13", "5", "/", "+"]) == 6 -# (4 + (13 / 5)) = 6 +assert evaluate_postfix(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]) == 22 +assert evaluate_postfix(["2", "1", "+", "3", "*"]) == 9 +assert evaluate_postfix(["4", "13", "5", "/", "+"]) == 6 From 135112803e43a8f03860f1af005f4a04e51e1e05 Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Tue, 13 Oct 2020 19:11:50 +0530 Subject: [PATCH 05/12] Update evaluate_postfix_notations.py --- data_structures/stacks/evaluate_postfix_notations.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index 938bd78074ce..595cf1aa61d8 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -4,6 +4,8 @@ Classic examples of simple stack implementations Valid operators are +, -, *, /. Each operand may be an integer or another expression. ''' + + def evaluate_postfix(postfix_notation): operations = {'+', '-', '*', '/'} stack = [] @@ -27,6 +29,7 @@ def evaluate_postfix(postfix_notation): return stack.pop() -assert evaluate_postfix(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]) == 22 +assert evaluate_postfix( + ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]) == 22 assert evaluate_postfix(["2", "1", "+", "3", "*"]) == 9 assert evaluate_postfix(["4", "13", "5", "/", "+"]) == 6 From 7e1eb21c8d107ea2f12501961855b6b638caeef4 Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Thu, 15 Oct 2020 18:15:55 +0530 Subject: [PATCH 06/12] Update evaluate_postfix_notations.py Made changes as per the required for fixing the failing pre-commits. --- .../stacks/evaluate_postfix_notations.py | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index 595cf1aa61d8..5825e91cadf1 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -1,12 +1,24 @@ -''' -The Reverse Polish Nation also known as Polish postfix notation or simply postfix notation -https://en.wikipedia.org/wiki/Reverse_Polish_notation -Classic examples of simple stack implementations -Valid operators are +, -, *, /. Each operand may be an integer or another expression. -''' +# The Reverse Polish Nation also known as Polish postfix notation or simply postfix notation +# https://en.wikipedia.org/wiki/Reverse_Polish_notation +# Classic examples of simple stack implementations +# Valid operators are +, -, *, /. Each operand may be an integer or another expression. def evaluate_postfix(postfix_notation): + """ + >>> array = ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"] + >>> evaluate_postfix(array) + 22 + >>> array = ["2", "1", "+", "3", "*"] + >>> evaluate_postfix(array) + 9 + >>> array = ["4", "13", "5", "/", "+"] + >>> evaluate_postfix(array) + 6 + >>> array = [] + >>> evaluate_postfix(array) + 0 + """ operations = {'+', '-', '*', '/'} stack = [] @@ -26,10 +38,11 @@ def evaluate_postfix(postfix_notation): stack.append(a // b) else: stack.append(int(token)) - return stack.pop() + return stack.pop() if len(stack) != 0 else 0 -assert evaluate_postfix( - ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]) == 22 -assert evaluate_postfix(["2", "1", "+", "3", "*"]) == 9 -assert evaluate_postfix(["4", "13", "5", "/", "+"]) == 6 + +if __name__ == "__main__": + import doctest + + doctest.testmod() From e1c15e9a2245c283f688433bf871feca9ba8d2c7 Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Thu, 15 Oct 2020 19:13:31 +0530 Subject: [PATCH 07/12] Update evaluate_postfix_notations.py Made changes as suggested by @cclauss --- .../stacks/evaluate_postfix_notations.py | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index 5825e91cadf1..ff71bfdb30fb 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -1,24 +1,26 @@ -# The Reverse Polish Nation also known as Polish postfix notation or simply postfix notation -# https://en.wikipedia.org/wiki/Reverse_Polish_notation -# Classic examples of simple stack implementations -# Valid operators are +, -, *, /. Each operand may be an integer or another expression. +""" +The Reverse Polish Nation also known as Polish postfix notation or simply postfix notation +https://en.wikipedia.org/wiki/Reverse_Polish_notation +Classic examples of simple stack implementations +Valid operators are +, -, *, /. Each operand may be an integer or another expression. +""" def evaluate_postfix(postfix_notation): """ - >>> array = ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"] - >>> evaluate_postfix(array) + >>> evaluate_postfix(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]) 22 - >>> array = ["2", "1", "+", "3", "*"] - >>> evaluate_postfix(array) + >>> evaluate_postfix(["2", "1", "+", "3", "*"]) 9 - >>> array = ["4", "13", "5", "/", "+"] - >>> evaluate_postfix(array) + >>> evaluate_postfix(["4", "13", "5", "/", "+"]) 6 >>> array = [] >>> evaluate_postfix(array) 0 """ + if not postfix_notation: + return 0 + operations = {'+', '-', '*', '/'} stack = [] @@ -39,7 +41,7 @@ def evaluate_postfix(postfix_notation): else: stack.append(int(token)) - return stack.pop() if len(stack) != 0 else 0 + return stack.pop() if __name__ == "__main__": From 9e0b8cb714fe8a430679c1a1dd237466fc627f22 Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Fri, 16 Oct 2020 08:10:49 +0530 Subject: [PATCH 08/12] Update evaluate_postfix_notations.py fixed pre-commit fails --- 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 ff71bfdb30fb..92e2634e0e71 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -21,7 +21,7 @@ def evaluate_postfix(postfix_notation): if not postfix_notation: return 0 - operations = {'+', '-', '*', '/'} + operations = {"+", "-", "*", "/"} stack = [] for token in postfix_notation: From 3668d564dc69f0bfca517bb05c5cbb6f468d311f Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Fri, 16 Oct 2020 08:16:56 +0530 Subject: [PATCH 09/12] Update evaluate_postfix_notations.py fixing pre-commit fails --- data_structures/stacks/evaluate_postfix_notations.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index 92e2634e0e71..be89eda5ce12 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -1,15 +1,15 @@ """ -The Reverse Polish Nation also known as Polish postfix notation or simply postfix notation +The Reverse Polish Nation also known as Polish postfix notation +or simply postfix notation. https://en.wikipedia.org/wiki/Reverse_Polish_notation Classic examples of simple stack implementations -Valid operators are +, -, *, /. Each operand may be an integer or another expression. +Valid operators are +, -, *, /. +Each operand may be an integer or another expression. """ def evaluate_postfix(postfix_notation): """ - >>> evaluate_postfix(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]) - 22 >>> evaluate_postfix(["2", "1", "+", "3", "*"]) 9 >>> evaluate_postfix(["4", "13", "5", "/", "+"]) From 2f63309958b418a6c16f7843d4c6e9e3d800bc69 Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Fri, 16 Oct 2020 15:00:22 +0530 Subject: [PATCH 10/12] Update evaluate_postfix_notations.py Deleted trailing white spaces causing pre-commits to fail --- data_structures/stacks/evaluate_postfix_notations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index be89eda5ce12..55ab0afccb92 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -1,9 +1,9 @@ """ The Reverse Polish Nation also known as Polish postfix notation -or simply postfix notation. +or simply postfix notation. https://en.wikipedia.org/wiki/Reverse_Polish_notation Classic examples of simple stack implementations -Valid operators are +, -, *, /. +Valid operators are +, -, *, /. Each operand may be an integer or another expression. """ From 29530ed4f40b6a4f050f68c39e8e20110011c645 Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Fri, 16 Oct 2020 16:47:32 +0530 Subject: [PATCH 11/12] Update data_structures/stacks/evaluate_postfix_notations.py Co-authored-by: Christian Clauss --- 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 55ab0afccb92..2dbdb82fe2e4 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -8,7 +8,7 @@ """ -def evaluate_postfix(postfix_notation): +def evaluate_postfix(postfix_notation: list) -> int: """ >>> evaluate_postfix(["2", "1", "+", "3", "*"]) 9 From 22393cf9ec165dc32000acb14a9938c75becdb28 Mon Sep 17 00:00:00 2001 From: Akash G Krishnan Date: Fri, 16 Oct 2020 16:47:45 +0530 Subject: [PATCH 12/12] Update data_structures/stacks/evaluate_postfix_notations.py Co-authored-by: Christian Clauss --- data_structures/stacks/evaluate_postfix_notations.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index 2dbdb82fe2e4..a03cb43bb020 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -14,8 +14,7 @@ def evaluate_postfix(postfix_notation: list) -> int: 9 >>> evaluate_postfix(["4", "13", "5", "/", "+"]) 6 - >>> array = [] - >>> evaluate_postfix(array) + >>> evaluate_postfix([]) 0 """ if not postfix_notation: