From d503bd86faefc858adbdd42ad61a23753392d7db Mon Sep 17 00:00:00 2001 From: hollowcrust <72879387+hollowcrust@users.noreply.github.com> Date: Wed, 11 Oct 2023 11:32:01 +0800 Subject: [PATCH 1/6] Fix bug and edit doctests --- .../stacks/infix_to_prefix_conversion.py | 53 +++++++++++++++---- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/data_structures/stacks/infix_to_prefix_conversion.py b/data_structures/stacks/infix_to_prefix_conversion.py index 1127211d59f9..82e9924b8ce7 100644 --- a/data_structures/stacks/infix_to_prefix_conversion.py +++ b/data_structures/stacks/infix_to_prefix_conversion.py @@ -14,7 +14,6 @@ a+b^c (Infix) -> +a^bc (Prefix) """ - def infix_2_postfix(infix): """ >>> infix_2_postfix("a+b^c") # doctest: +NORMALIZE_WHITESPACE @@ -28,14 +27,30 @@ def infix_2_postfix(infix): | + | abc^ | | abc^+ 'abc^+' - >>> infix_2_postfix("1*((-a)*2+b)") - Traceback (most recent call last): - ... - KeyError: '(' + + >>> infix_2_postfix("1*((-a)*2+b)") # doctest: +NORMALIZE_WHITESPACE + Symbol | Stack | Postfix + ------------------------------------------- + 1 | | 1 + * | * | 1 + ( | *( | 1 + ( | *(( | 1 + - | *((- | 1 + a | *((- | 1a + ) | *( | 1a- + * | *(* | 1a- + 2 | *(* | 1a-2 + + | *(+ | 1a-2* + b | *(+ | 1a-2*b + ) | * | 1a-2*b+ + | | 1a-2*b+* + '1a-2*b+*' + >>> infix_2_postfix("") Symbol | Stack | Postfix ---------------------------- '' + >>> infix_2_postfix("(()") # doctest: +NORMALIZE_WHITESPACE Symbol | Stack | Postfix ---------------------------- @@ -44,6 +59,7 @@ def infix_2_postfix(infix): ) | ( | | | ( '(' + >>> infix_2_postfix("())") Traceback (most recent call last): ... @@ -83,7 +99,11 @@ def infix_2_postfix(infix): if len(stack) == 0: stack.append(x) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack - while len(stack) > 0 and priority[x] <= priority[stack[-1]]: + while ( + len(stack) > 0 + and stack[-1] != "(" + and priority[x] <= priority[stack[-1]] + ): post_fix.append(stack.pop()) # pop stack & add to Postfix stack.append(x) # push x to stack @@ -119,10 +139,23 @@ def infix_2_prefix(infix): | | cb^a+ '+a^bc' - >>> infix_2_prefix("1*((-a)*2+b)") - Traceback (most recent call last): - ... - KeyError: '(' + >>> infix_2_prefix("1*((-a)*2+b)") # doctest: +NORMALIZE_WHITESPACE + Symbol | Stack | Postfix + ------------------------------------------- + ( | ( | + b | ( | b + + | (+ | b + 2 | (+ | b2 + * | (+* | b2 + ( | (+*( | b2 + a | (+*( | b2a + - | (+*(- | b2a + ) | (+* | b2a- + ) | | b2a-*+ + * | * | b2a-*+ + 1 | * | b2a-*+1 + | | b2a-*+1* + '*1+*-a2b' >>> infix_2_prefix('') Symbol | Stack | Postfix From da1e8699d76e5d67df06f92090ff40054cf32dad Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Oct 2023 03:36:36 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/stacks/infix_to_prefix_conversion.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/data_structures/stacks/infix_to_prefix_conversion.py b/data_structures/stacks/infix_to_prefix_conversion.py index 82e9924b8ce7..28f022fc775c 100644 --- a/data_structures/stacks/infix_to_prefix_conversion.py +++ b/data_structures/stacks/infix_to_prefix_conversion.py @@ -14,6 +14,7 @@ a+b^c (Infix) -> +a^bc (Prefix) """ + def infix_2_postfix(infix): """ >>> infix_2_postfix("a+b^c") # doctest: +NORMALIZE_WHITESPACE @@ -28,7 +29,7 @@ def infix_2_postfix(infix): | | abc^+ 'abc^+' - >>> infix_2_postfix("1*((-a)*2+b)") # doctest: +NORMALIZE_WHITESPACE + >>> infix_2_postfix("1*((-a)*2+b)") # doctest: +NORMALIZE_WHITESPACE Symbol | Stack | Postfix ------------------------------------------- 1 | | 1 From 2e8259e8be946a4aaf661156d1ef898a833aafc6 Mon Sep 17 00:00:00 2001 From: hollowcrust <72879387+hollowcrust@users.noreply.github.com> Date: Wed, 11 Oct 2023 16:43:15 +0800 Subject: [PATCH 3/6] Add type hints, raiseError and other minor adjustments --- .../stacks/infix_to_prefix_conversion.py | 52 +++++++++---------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/data_structures/stacks/infix_to_prefix_conversion.py b/data_structures/stacks/infix_to_prefix_conversion.py index 28f022fc775c..cf64d96f2baf 100644 --- a/data_structures/stacks/infix_to_prefix_conversion.py +++ b/data_structures/stacks/infix_to_prefix_conversion.py @@ -15,7 +15,7 @@ """ -def infix_2_postfix(infix): +def infix_2_postfix(infix: str) -> str: """ >>> infix_2_postfix("a+b^c") # doctest: +NORMALIZE_WHITESPACE Symbol | Stack | Postfix @@ -52,14 +52,10 @@ def infix_2_postfix(infix): ---------------------------- '' - >>> infix_2_postfix("(()") # doctest: +NORMALIZE_WHITESPACE - Symbol | Stack | Postfix - ---------------------------- - ( | ( | - ( | (( | - ) | ( | - | | ( - '(' + >>> infix_2_postfix("(()") + Traceback (most recent call last): + ... + ValueError: invalid expression >>> infix_2_postfix("())") Traceback (most recent call last): @@ -76,7 +72,7 @@ def infix_2_postfix(infix): "+": 1, "-": 1, } # Priority of each operator - print_width = len(infix) if (len(infix) > 7) else 7 + print_width = max(len(infix), 7) # Print table header for output print( @@ -93,6 +89,9 @@ def infix_2_postfix(infix): elif x == "(": stack.append(x) # if x is "(" push to Stack elif x == ")": # if x is ")" pop stack until "(" is encountered + if len(stack) == 0: # close bracket without open bracket + raise IndexError("list index out of range") + while stack[-1] != "(": post_fix.append(stack.pop()) # Pop stack & add the content to Postfix stack.pop() @@ -101,7 +100,7 @@ def infix_2_postfix(infix): stack.append(x) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack while ( - len(stack) > 0 + stack and stack[-1] != "(" and priority[x] <= priority[stack[-1]] ): @@ -116,6 +115,9 @@ def infix_2_postfix(infix): ) # Output in tabular format while len(stack) > 0: # while stack is not empty + if stack[-1] == "(": # open bracket with no close bracket + raise ValueError("invalid expression") + post_fix.append(stack.pop()) # pop stack & add to Postfix print( " ".center(8), @@ -127,7 +129,7 @@ def infix_2_postfix(infix): return "".join(post_fix) # return Postfix as str -def infix_2_prefix(infix): +def infix_2_prefix(infix: str) -> str: """ >>> infix_2_prefix("a+b^c") # doctest: +NORMALIZE_WHITESPACE Symbol | Stack | Postfix @@ -168,24 +170,20 @@ def infix_2_prefix(infix): ... IndexError: list index out of range - >>> infix_2_prefix('())') # doctest: +NORMALIZE_WHITESPACE - Symbol | Stack | Postfix - ---------------------------- - ( | ( | - ( | (( | - ) | ( | - | | ( - '(' + >>> infix_2_prefix('())') + Traceback (most recent call last): + ... + ValueError: invalid expression """ - infix = list(infix[::-1]) # reverse the infix equation + new_infix = list(infix[::-1]) # reverse the infix equation - for i in range(len(infix)): - if infix[i] == "(": - infix[i] = ")" # change "(" to ")" - elif infix[i] == ")": - infix[i] = "(" # change ")" to "(" + for i in range(len(new_infix)): + if new_infix[i] == "(": + new_infix[i] = ")" # change "(" to ")" + elif new_infix[i] == ")": + new_infix[i] = "(" # change ")" to "(" - return (infix_2_postfix("".join(infix)))[ + return (infix_2_postfix("".join(new_infix)))[ ::-1 ] # call infix_2_postfix on Infix, return reverse of Postfix From ae64862d7abe6bb05da4b25a2287117d6038086a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Oct 2023 08:45:02 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/stacks/infix_to_prefix_conversion.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/data_structures/stacks/infix_to_prefix_conversion.py b/data_structures/stacks/infix_to_prefix_conversion.py index cf64d96f2baf..f9cdae3a037f 100644 --- a/data_structures/stacks/infix_to_prefix_conversion.py +++ b/data_structures/stacks/infix_to_prefix_conversion.py @@ -99,11 +99,7 @@ def infix_2_postfix(infix: str) -> str: if len(stack) == 0: stack.append(x) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack - while ( - stack - and stack[-1] != "(" - and priority[x] <= priority[stack[-1]] - ): + while stack and stack[-1] != "(" and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop()) # pop stack & add to Postfix stack.append(x) # push x to stack @@ -117,7 +113,7 @@ def infix_2_postfix(infix: str) -> str: while len(stack) > 0: # while stack is not empty if stack[-1] == "(": # open bracket with no close bracket raise ValueError("invalid expression") - + post_fix.append(stack.pop()) # pop stack & add to Postfix print( " ".center(8), From f24493676eacfe7417fb0a5dd458dcf2d610eb61 Mon Sep 17 00:00:00 2001 From: hollowcrust <72879387+hollowcrust@users.noreply.github.com> Date: Wed, 11 Oct 2023 16:53:29 +0800 Subject: [PATCH 5/6] Cleaning code --- .../stacks/infix_to_prefix_conversion.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/data_structures/stacks/infix_to_prefix_conversion.py b/data_structures/stacks/infix_to_prefix_conversion.py index f9cdae3a037f..dc6769d2dafa 100644 --- a/data_structures/stacks/infix_to_prefix_conversion.py +++ b/data_structures/stacks/infix_to_prefix_conversion.py @@ -171,17 +171,16 @@ def infix_2_prefix(infix: str) -> str: ... ValueError: invalid expression """ - new_infix = list(infix[::-1]) # reverse the infix equation + reversed_infix = list(infix[::-1]) # reverse the infix equation - for i in range(len(new_infix)): - if new_infix[i] == "(": - new_infix[i] = ")" # change "(" to ")" - elif new_infix[i] == ")": - new_infix[i] = "(" # change ")" to "(" + for i in range(len(reversed_infix)): + if reversed_infix[i] == "(": + reversed_infix[i] = ")" # change "(" to ")" + elif reversed_infix[i] == ")": + reversed_infix[i] = "(" # change ")" to "(" - return (infix_2_postfix("".join(new_infix)))[ - ::-1 - ] # call infix_2_postfix on Infix, return reverse of Postfix + # call infix_2_postfix on Infix, return reverse of Postfix + return (infix_2_postfix("".join(reversed_infix)))[::-1] if __name__ == "__main__": From 574f83b76a8614474b3a5b4a4ae5fa37f54d28a2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Oct 2023 08:54:02 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/stacks/infix_to_prefix_conversion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/stacks/infix_to_prefix_conversion.py b/data_structures/stacks/infix_to_prefix_conversion.py index dc6769d2dafa..beff421c0cfa 100644 --- a/data_structures/stacks/infix_to_prefix_conversion.py +++ b/data_structures/stacks/infix_to_prefix_conversion.py @@ -180,7 +180,7 @@ def infix_2_prefix(infix: str) -> str: reversed_infix[i] = "(" # change ")" to "(" # call infix_2_postfix on Infix, return reverse of Postfix - return (infix_2_postfix("".join(reversed_infix)))[::-1] + return (infix_2_postfix("".join(reversed_infix)))[::-1] if __name__ == "__main__":