From fdca44194f78b7d0ec9567728273b901ec56223d Mon Sep 17 00:00:00 2001 From: hollowcrust <72879387+hollowcrust@users.noreply.github.com> Date: Sun, 8 Oct 2023 10:05:52 +0800 Subject: [PATCH 01/12] Add doctests, exceptions, type hints and fix bug for infix_to_prefix_conversion.py Add doctests Add exceptions for expressions with invalid bracket positions Add type hints for functions Fix a bug on line 53 (57 in PR) --- .../stacks/infix_to_prefix_conversion.py | 64 +++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/data_structures/stacks/infix_to_prefix_conversion.py b/data_structures/stacks/infix_to_prefix_conversion.py index 6f6d5d57e2cb..ea4055a1434b 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: stack = [] post_fix = [] priority = { @@ -26,6 +26,7 @@ def infix_2_postfix(infix): "+": 1, "-": 1, } # Priority of each operator + print_width = len(infix) if (len(infix) > 7) else 7 # Print table header for output @@ -43,6 +44,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 ValueError("Invalid bracket position(s)") + while stack[-1] != "(": post_fix.append(stack.pop()) # Pop stack & add the content to Postfix stack.pop() @@ -50,7 +54,8 @@ 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 @@ -62,6 +67,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 bracket position(s)") + post_fix.append(stack.pop()) # pop stack & add to Postfix print( " ".center(8), @@ -73,9 +81,54 @@ def infix_2_postfix(infix): return "".join(post_fix) # return Postfix as str -def infix_2_prefix(infix): - infix = list(infix[::-1]) # reverse the infix equation +def infix_2_prefix(infix: str) -> str: + """ + >>> infix_2_prefix('a+b^c') + Symbol | Stack | Postfix + ---------------------------- + c | | c + ^ | ^ | c + b | ^ | cb + + | + | cb^ + a | + | cb^a + | | cb^a+ + '+a^bc' + + >>> infix_2_prefix('1*((-a)*2+b)') + 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 + ---------------------------- + '' + + >>> infix_2_prefix('(()') + Traceback (most recent call last): + ... + ValueError: Invalid bracket position(s) + >>> infix_2_prefix('())') + Traceback (most recent call last): + ... + ValueError: Invalid bracket position(s) + """ + infix = list(infix[::-1]) # reverse the infix equation + for i in range(len(infix)): if infix[i] == "(": infix[i] = ")" # change "(" to ")" @@ -88,6 +141,9 @@ def infix_2_prefix(infix): if __name__ == "__main__": + from doctest import testmod + testmod() + Infix = input("\nEnter an Infix Equation = ") # Input an Infix equation Infix = "".join(Infix.split()) # Remove spaces from the input print("\n\t", Infix, "(Infix) -> ", infix_2_prefix(Infix), "(Prefix)") From 72d19e8dbdfb3dd789d4c3a9e7b0089564a311f7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 8 Oct 2023 02:14:30 +0000 Subject: [PATCH 02/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../stacks/infix_to_prefix_conversion.py | 66 ++++++++++--------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/data_structures/stacks/infix_to_prefix_conversion.py b/data_structures/stacks/infix_to_prefix_conversion.py index ea4055a1434b..badf5399768a 100644 --- a/data_structures/stacks/infix_to_prefix_conversion.py +++ b/data_structures/stacks/infix_to_prefix_conversion.py @@ -26,7 +26,7 @@ def infix_2_postfix(infix: str) -> str: "+": 1, "-": 1, } # Priority of each operator - + print_width = len(infix) if (len(infix) > 7) else 7 # Print table header for output @@ -44,9 +44,9 @@ def infix_2_postfix(infix: str) -> str: 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 + if len(stack) == 0: # close bracket without open bracket raise ValueError("Invalid bracket position(s)") - + while stack[-1] != "(": post_fix.append(stack.pop()) # Pop stack & add the content to Postfix stack.pop() @@ -54,8 +54,11 @@ 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 len(stack) > 0 and stack[-1] != '(' \ - 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 @@ -67,9 +70,9 @@ def infix_2_postfix(infix: str) -> str: ) # Output in tabular format while len(stack) > 0: # while stack is not empty - if stack[-1] == '(': # open bracket with no close bracket + if stack[-1] == "(": # open bracket with no close bracket raise ValueError("Invalid bracket position(s)") - + post_fix.append(stack.pop()) # pop stack & add to Postfix print( " ".center(8), @@ -86,32 +89,32 @@ def infix_2_prefix(infix: str) -> str: >>> infix_2_prefix('a+b^c') Symbol | Stack | Postfix ---------------------------- - c | | c - ^ | ^ | c - b | ^ | cb - + | + | cb^ - a | + | cb^a - | | cb^a+ + c | | c + ^ | ^ | c + b | ^ | cb + + | + | cb^ + a | + | cb^a + | | cb^a+ '+a^bc' - + >>> infix_2_prefix('1*((-a)*2+b)') - Symbol | Stack | Postfix + Symbol | Stack | Postfix ------------------------------------------- - ( | ( | - b | ( | b - + | (+ | b - 2 | (+ | b2 - * | (+* | b2 - ( | (+*( | b2 - a | (+*( | b2a - - | (+*(- | b2a - ) | (+* | b2a- - ) | | b2a-*+ - * | * | b2a-*+ - 1 | * | b2a-*+1 - | | b2a-*+1* + ( | ( | + 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 ---------------------------- @@ -128,7 +131,7 @@ def infix_2_prefix(infix: str) -> str: ValueError: Invalid bracket position(s) """ infix = list(infix[::-1]) # reverse the infix equation - + for i in range(len(infix)): if infix[i] == "(": infix[i] = ")" # change "(" to ")" @@ -142,8 +145,9 @@ def infix_2_prefix(infix: str) -> str: if __name__ == "__main__": from doctest import testmod + testmod() - + Infix = input("\nEnter an Infix Equation = ") # Input an Infix equation Infix = "".join(Infix.split()) # Remove spaces from the input print("\n\t", Infix, "(Infix) -> ", infix_2_prefix(Infix), "(Prefix)") From 78e8593393b76a6220b9a5b9188ff7771620afa5 Mon Sep 17 00:00:00 2001 From: hollowcrust <72879387+hollowcrust@users.noreply.github.com> Date: Sun, 8 Oct 2023 10:19:53 +0800 Subject: [PATCH 03/12] Change type hints in infix_to_prefix_conversion.py --- 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 badf5399768a..a207fe45e952 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: str) -> str: +def infix_2_postfix(infix: list[str]) -> str: stack = [] post_fix = [] priority = { From 6b78ef545b39bef13ba14ca1537e19f748cc8c41 Mon Sep 17 00:00:00 2001 From: hollowcrust <72879387+hollowcrust@users.noreply.github.com> Date: Sun, 8 Oct 2023 10:23:21 +0800 Subject: [PATCH 04/12] Remove printing trailing whitespace in the output table --- 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 a207fe45e952..a18f4d821837 100644 --- a/data_structures/stacks/infix_to_prefix_conversion.py +++ b/data_structures/stacks/infix_to_prefix_conversion.py @@ -27,7 +27,7 @@ def infix_2_postfix(infix: list[str]) -> str: "-": 1, } # Priority of each operator - print_width = len(infix) if (len(infix) > 7) else 7 + print_width = len(infix) # Print table header for output print( From 91a6493aa0f3e9c6a83b43310086240681b6f40f Mon Sep 17 00:00:00 2001 From: hollowcrust <72879387+hollowcrust@users.noreply.github.com> Date: Sun, 8 Oct 2023 10:27:09 +0800 Subject: [PATCH 05/12] Fix type hint errors --- .../stacks/infix_to_prefix_conversion.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/data_structures/stacks/infix_to_prefix_conversion.py b/data_structures/stacks/infix_to_prefix_conversion.py index a18f4d821837..23b110e2edc7 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: list[str]) -> str: +def infix_2_postfix(infix: str) -> str: stack = [] post_fix = [] priority = { @@ -130,15 +130,15 @@ def infix_2_prefix(infix: str) -> str: ... ValueError: Invalid bracket position(s) """ - 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 ef52d6361bbf01468e5148bc5faef389fb94169e Mon Sep 17 00:00:00 2001 From: hollowcrust <72879387+hollowcrust@users.noreply.github.com> Date: Sun, 8 Oct 2023 10:46:30 +0800 Subject: [PATCH 06/12] Fix doctests --- data_structures/stacks/infix_to_prefix_conversion.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/data_structures/stacks/infix_to_prefix_conversion.py b/data_structures/stacks/infix_to_prefix_conversion.py index 23b110e2edc7..06ccbfc7ba97 100644 --- a/data_structures/stacks/infix_to_prefix_conversion.py +++ b/data_structures/stacks/infix_to_prefix_conversion.py @@ -95,7 +95,6 @@ def infix_2_prefix(infix: str) -> str: + | + | cb^ a | + | cb^a | | cb^a+ - '+a^bc' >>> infix_2_prefix('1*((-a)*2+b)') Symbol | Stack | Postfix @@ -113,12 +112,10 @@ def infix_2_prefix(infix: str) -> str: * | * | b2a-*+ 1 | * | b2a-*+1 | | b2a-*+1* - '*1+*-a2b' >>> infix_2_prefix('') Symbol | Stack | Postfix ---------------------------- - '' >>> infix_2_prefix('(()') Traceback (most recent call last): From b226362d313985927cb095c4240b294b4b936dfb Mon Sep 17 00:00:00 2001 From: hollowcrust <72879387+hollowcrust@users.noreply.github.com> Date: Sun, 8 Oct 2023 11:37:26 +0800 Subject: [PATCH 07/12] Adjust table convention in output and doctests --- .../stacks/infix_to_prefix_conversion.py | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/data_structures/stacks/infix_to_prefix_conversion.py b/data_structures/stacks/infix_to_prefix_conversion.py index 06ccbfc7ba97..025b9ce53d8e 100644 --- a/data_structures/stacks/infix_to_prefix_conversion.py +++ b/data_structures/stacks/infix_to_prefix_conversion.py @@ -27,13 +27,12 @@ def infix_2_postfix(infix: str) -> str: "-": 1, } # Priority of each operator - print_width = len(infix) - + print_width = len(infix) if len(infix) > 7 else 7 # Print table header for output print( "Symbol".center(8), "Stack".center(print_width), - "Postfix".center(print_width), + "Postfix".rjust((print_width-7) // 2 + 7), sep=" | ", ) print("-" * (print_width * 3 + 7)) @@ -61,14 +60,20 @@ def infix_2_postfix(infix: str) -> str: ): post_fix.append(stack.pop()) # pop stack & add to Postfix stack.append(x) # push x to stack - - print( - x.center(8), - ("".join(stack)).ljust(print_width), - ("".join(post_fix)).ljust(print_width), - sep=" | ", - ) # Output in tabular format - + + if post_fix != []: + print( + x.center(8), + ("".join(stack)).ljust(print_width), + "".join(post_fix), + sep=" | ", + ) #Output in tabular format + + else: # Post_fix is empty -> remove trailing space in table + print( + x.center(8) + " | " + ("".join(stack)).ljust(print_width) + " |" + ) #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 bracket position(s)") @@ -77,16 +82,17 @@ def infix_2_postfix(infix: str) -> str: print( " ".center(8), ("".join(stack)).ljust(print_width), - ("".join(post_fix)).ljust(print_width), + "".join(post_fix), sep=" | ", ) # Output in tabular format + return "".join(post_fix) # return Postfix as str def infix_2_prefix(infix: str) -> str: """ - >>> infix_2_prefix('a+b^c') + >>> infix_2_prefix("a+b^c") Symbol | Stack | Postfix ---------------------------- c | | c @@ -95,8 +101,9 @@ def infix_2_prefix(infix: str) -> str: + | + | cb^ a | + | cb^a | | cb^a+ + '+a^bc' - >>> infix_2_prefix('1*((-a)*2+b)') + >>> infix_2_prefix("1*((-a)*2+b)") Symbol | Stack | Postfix ------------------------------------------- ( | ( | @@ -112,10 +119,12 @@ def infix_2_prefix(infix: str) -> str: * | * | b2a-*+ 1 | * | b2a-*+1 | | b2a-*+1* + '*1+*-a2b' >>> infix_2_prefix('') Symbol | Stack | Postfix ---------------------------- + '' >>> infix_2_prefix('(()') Traceback (most recent call last): From 138449ce06de9b7d88786156c9843b97f8dc27f9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 8 Oct 2023 03:37:59 +0000 Subject: [PATCH 08/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../stacks/infix_to_prefix_conversion.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/data_structures/stacks/infix_to_prefix_conversion.py b/data_structures/stacks/infix_to_prefix_conversion.py index 025b9ce53d8e..7ef11085fd3c 100644 --- a/data_structures/stacks/infix_to_prefix_conversion.py +++ b/data_structures/stacks/infix_to_prefix_conversion.py @@ -32,7 +32,7 @@ def infix_2_postfix(infix: str) -> str: print( "Symbol".center(8), "Stack".center(print_width), - "Postfix".rjust((print_width-7) // 2 + 7), + "Postfix".rjust((print_width - 7) // 2 + 7), sep=" | ", ) print("-" * (print_width * 3 + 7)) @@ -60,20 +60,20 @@ def infix_2_postfix(infix: str) -> str: ): post_fix.append(stack.pop()) # pop stack & add to Postfix stack.append(x) # push x to stack - + if post_fix != []: print( x.center(8), ("".join(stack)).ljust(print_width), "".join(post_fix), sep=" | ", - ) #Output in tabular format + ) # Output in tabular format - else: # Post_fix is empty -> remove trailing space in table + else: # Post_fix is empty -> remove trailing space in table print( x.center(8) + " | " + ("".join(stack)).ljust(print_width) + " |" - ) #Output in tabular format - + ) # 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 bracket position(s)") @@ -85,7 +85,6 @@ def infix_2_postfix(infix: str) -> str: "".join(post_fix), sep=" | ", ) # Output in tabular format - return "".join(post_fix) # return Postfix as str From a9f0d1be38326c83d60d5a68af1be111f1d9ce20 Mon Sep 17 00:00:00 2001 From: hollowcrust <72879387+hollowcrust@users.noreply.github.com> Date: Sun, 8 Oct 2023 16:21:10 +0800 Subject: [PATCH 09/12] Add doctests for infix_2_postfix() --- .../stacks/infix_to_prefix_conversion.py | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/data_structures/stacks/infix_to_prefix_conversion.py b/data_structures/stacks/infix_to_prefix_conversion.py index 7ef11085fd3c..a46cb9822474 100644 --- a/data_structures/stacks/infix_to_prefix_conversion.py +++ b/data_structures/stacks/infix_to_prefix_conversion.py @@ -16,6 +16,52 @@ def infix_2_postfix(infix: str) -> str: + """ + >>> infix_2_postfix("a+b^c") + Symbol | Stack | Postfix + ---------------------------- + a | | a + + | + | a + b | + | ab + ^ | +^ | ab + c | +^ | abc + | + | abc^ + | | abc^+ + 'abc^+' + + >>> infix_2_postfix("1*((-a)*2+b)") + 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("(()") + Traceback (most recent call last): + ... + ValueError: Invalid bracket position(s) + + >>> infix_2_postfix("())") + Traceback (most recent call last): + ... + ValueError: Invalid bracket position(s) + """ stack = [] post_fix = [] priority = { From 340d37b961574c04cc71b268a0340fa82d5ed1d1 Mon Sep 17 00:00:00 2001 From: hollowcrust <72879387+hollowcrust@users.noreply.github.com> Date: Sun, 8 Oct 2023 16:26:49 +0800 Subject: [PATCH 10/12] Update print_width --- 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 a46cb9822474..7b904b94e499 100644 --- a/data_structures/stacks/infix_to_prefix_conversion.py +++ b/data_structures/stacks/infix_to_prefix_conversion.py @@ -73,7 +73,7 @@ def infix_2_postfix(infix: str) -> str: "-": 1, } # Priority of each operator - print_width = len(infix) if len(infix) > 7 else 7 + print_width = min(len(infix), 7) # Print table header for output print( "Symbol".center(8), From 7e475113039680e7b1da786588c9b05e6b187866 Mon Sep 17 00:00:00 2001 From: hollowcrust <72879387+hollowcrust@users.noreply.github.com> Date: Sun, 8 Oct 2023 17:45:04 +0800 Subject: [PATCH 11/12] Update print_width --- 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 7b904b94e499..6da2eae6dfd3 100644 --- a/data_structures/stacks/infix_to_prefix_conversion.py +++ b/data_structures/stacks/infix_to_prefix_conversion.py @@ -73,7 +73,7 @@ def infix_2_postfix(infix: str) -> str: "-": 1, } # Priority of each operator - print_width = min(len(infix), 7) + print_width = max(len(infix), 7) # Print table header for output print( "Symbol".center(8), From 1749f1923bab5305dd5c4f26855121a7058d3cb7 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 10 Oct 2023 18:50:04 +0200 Subject: [PATCH 12/12] Fix the doctests --- .../stacks/infix_to_prefix_conversion.py | 128 ++++++------------ 1 file changed, 45 insertions(+), 83 deletions(-) diff --git a/data_structures/stacks/infix_to_prefix_conversion.py b/data_structures/stacks/infix_to_prefix_conversion.py index 6da2eae6dfd3..1127211d59f9 100644 --- a/data_structures/stacks/infix_to_prefix_conversion.py +++ b/data_structures/stacks/infix_to_prefix_conversion.py @@ -15,9 +15,9 @@ """ -def infix_2_postfix(infix: str) -> str: +def infix_2_postfix(infix): """ - >>> infix_2_postfix("a+b^c") + >>> infix_2_postfix("a+b^c") # doctest: +NORMALIZE_WHITESPACE Symbol | Stack | Postfix ---------------------------- a | | a @@ -28,39 +28,26 @@ def infix_2_postfix(infix: str) -> str: | + | abc^ | | abc^+ 'abc^+' - >>> infix_2_postfix("1*((-a)*2+b)") - 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+*' - + Traceback (most recent call last): + ... + KeyError: '(' >>> infix_2_postfix("") Symbol | Stack | Postfix ---------------------------- '' - - >>> infix_2_postfix("(()") - Traceback (most recent call last): - ... - ValueError: Invalid bracket position(s) - + >>> infix_2_postfix("(()") # doctest: +NORMALIZE_WHITESPACE + Symbol | Stack | Postfix + ---------------------------- + ( | ( | + ( | (( | + ) | ( | + | | ( + '(' >>> infix_2_postfix("())") Traceback (most recent call last): ... - ValueError: Invalid bracket position(s) + IndexError: list index out of range """ stack = [] post_fix = [] @@ -72,13 +59,13 @@ def infix_2_postfix(infix: str) -> str: "+": 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( "Symbol".center(8), "Stack".center(print_width), - "Postfix".rjust((print_width - 7) // 2 + 7), + "Postfix".center(print_width), sep=" | ", ) print("-" * (print_width * 3 + 7)) @@ -89,9 +76,6 @@ def infix_2_postfix(infix: str) -> str: 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 ValueError("Invalid bracket position(s)") - while stack[-1] != "(": post_fix.append(stack.pop()) # Pop stack & add the content to Postfix stack.pop() @@ -99,45 +83,32 @@ 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 ( - len(stack) > 0 - and stack[-1] != "(" - and priority[x] <= priority[stack[-1]] - ): + while len(stack) > 0 and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop()) # pop stack & add to Postfix stack.append(x) # push x to stack - if post_fix != []: - print( - x.center(8), - ("".join(stack)).ljust(print_width), - "".join(post_fix), - sep=" | ", - ) # Output in tabular format - - else: # Post_fix is empty -> remove trailing space in table - print( - x.center(8) + " | " + ("".join(stack)).ljust(print_width) + " |" - ) # Output in tabular format + print( + x.center(8), + ("".join(stack)).ljust(print_width), + ("".join(post_fix)).ljust(print_width), + sep=" | ", + ) # 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 bracket position(s)") - post_fix.append(stack.pop()) # pop stack & add to Postfix print( " ".center(8), ("".join(stack)).ljust(print_width), - "".join(post_fix), + ("".join(post_fix)).ljust(print_width), sep=" | ", ) # Output in tabular format return "".join(post_fix) # return Postfix as str -def infix_2_prefix(infix: str) -> str: +def infix_2_prefix(infix): """ - >>> infix_2_prefix("a+b^c") + >>> infix_2_prefix("a+b^c") # doctest: +NORMALIZE_WHITESPACE Symbol | Stack | Postfix ---------------------------- c | | c @@ -149,22 +120,9 @@ def infix_2_prefix(infix: str) -> str: '+a^bc' >>> infix_2_prefix("1*((-a)*2+b)") - Symbol | Stack | Postfix - ------------------------------------------- - ( | ( | - b | ( | b - + | (+ | b - 2 | (+ | b2 - * | (+* | b2 - ( | (+*( | b2 - a | (+*( | b2a - - | (+*(- | b2a - ) | (+* | b2a- - ) | | b2a-*+ - * | * | b2a-*+ - 1 | * | b2a-*+1 - | | b2a-*+1* - '*1+*-a2b' + Traceback (most recent call last): + ... + KeyError: '(' >>> infix_2_prefix('') Symbol | Stack | Postfix @@ -174,22 +132,26 @@ def infix_2_prefix(infix: str) -> str: >>> infix_2_prefix('(()') Traceback (most recent call last): ... - ValueError: Invalid bracket position(s) + IndexError: list index out of range - >>> infix_2_prefix('())') - Traceback (most recent call last): - ... - ValueError: Invalid bracket position(s) + >>> infix_2_prefix('())') # doctest: +NORMALIZE_WHITESPACE + Symbol | Stack | Postfix + ---------------------------- + ( | ( | + ( | (( | + ) | ( | + | | ( + '(' """ - new_infix = list(infix[::-1]) # reverse the infix equation + 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(infix)): + if infix[i] == "(": + infix[i] = ")" # change "(" to ")" + elif infix[i] == ")": + infix[i] = "(" # change ")" to "(" - return (infix_2_postfix("".join(new_infix)))[ + return (infix_2_postfix("".join(infix)))[ ::-1 ] # call infix_2_postfix on Infix, return reverse of Postfix