Fix bug and edit doctests for infix_to_prefix_conversion (#10259)

* Fix bug and edit doctests

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add type hints, raiseError and other minor adjustments

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Cleaning code

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
hollowcrust 2023-10-11 17:00:49 +08:00 committed by GitHub
parent c850227bee
commit 672fda9130
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,7 +15,7 @@ Enter an Infix Equation = a + b ^c
""" """
def infix_2_postfix(infix): def infix_2_postfix(infix: str) -> str:
""" """
>>> infix_2_postfix("a+b^c") # doctest: +NORMALIZE_WHITESPACE >>> infix_2_postfix("a+b^c") # doctest: +NORMALIZE_WHITESPACE
Symbol | Stack | Postfix Symbol | Stack | Postfix
@ -28,22 +28,35 @@ def infix_2_postfix(infix):
| + | abc^ | + | abc^
| | abc^+ | | abc^+
'abc^+' 'abc^+'
>>> infix_2_postfix("1*((-a)*2+b)")
Traceback (most recent call last): >>> infix_2_postfix("1*((-a)*2+b)") # doctest: +NORMALIZE_WHITESPACE
... Symbol | Stack | Postfix
KeyError: '(' -------------------------------------------
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("") >>> infix_2_postfix("")
Symbol | Stack | Postfix Symbol | Stack | Postfix
---------------------------- ----------------------------
'' ''
>>> infix_2_postfix("(()") # doctest: +NORMALIZE_WHITESPACE
Symbol | Stack | Postfix >>> infix_2_postfix("(()")
---------------------------- Traceback (most recent call last):
( | ( | ...
( | (( | ValueError: invalid expression
) | ( |
| | (
'('
>>> infix_2_postfix("())") >>> infix_2_postfix("())")
Traceback (most recent call last): Traceback (most recent call last):
... ...
@ -59,7 +72,7 @@ def infix_2_postfix(infix):
"+": 1, "+": 1,
"-": 1, "-": 1,
} # Priority of each operator } # 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 table header for output
print( print(
@ -76,6 +89,9 @@ def infix_2_postfix(infix):
elif x == "(": elif x == "(":
stack.append(x) # if x is "(" push to Stack stack.append(x) # if x is "(" push to Stack
elif x == ")": # if x is ")" pop stack until "(" is encountered 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] != "(": while stack[-1] != "(":
post_fix.append(stack.pop()) # Pop stack & add the content to Postfix post_fix.append(stack.pop()) # Pop stack & add the content to Postfix
stack.pop() stack.pop()
@ -83,7 +99,7 @@ def infix_2_postfix(infix):
if len(stack) == 0: if len(stack) == 0:
stack.append(x) # If stack is empty, push x to stack stack.append(x) # If stack is empty, push x to stack
else: # while priority of x is not > priority of element in the 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 stack and stack[-1] != "(" and priority[x] <= priority[stack[-1]]:
post_fix.append(stack.pop()) # pop stack & add to Postfix post_fix.append(stack.pop()) # pop stack & add to Postfix
stack.append(x) # push x to stack stack.append(x) # push x to stack
@ -95,6 +111,9 @@ def infix_2_postfix(infix):
) # Output in tabular format ) # Output in tabular format
while len(stack) > 0: # while stack is not empty 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 post_fix.append(stack.pop()) # pop stack & add to Postfix
print( print(
" ".center(8), " ".center(8),
@ -106,7 +125,7 @@ def infix_2_postfix(infix):
return "".join(post_fix) # return Postfix as str 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 >>> infix_2_prefix("a+b^c") # doctest: +NORMALIZE_WHITESPACE
Symbol | Stack | Postfix Symbol | Stack | Postfix
@ -119,10 +138,23 @@ def infix_2_prefix(infix):
| | cb^a+ | | cb^a+
'+a^bc' '+a^bc'
>>> infix_2_prefix("1*((-a)*2+b)") >>> infix_2_prefix("1*((-a)*2+b)") # doctest: +NORMALIZE_WHITESPACE
Traceback (most recent call last): Symbol | Stack | Postfix
... -------------------------------------------
KeyError: '(' ( | ( |
b | ( | b
+ | (+ | b
2 | (+ | b2
* | (+* | b2
( | (+*( | b2
a | (+*( | b2a
- | (+*(- | b2a
) | (+* | b2a-
) | | b2a-*+
* | * | b2a-*+
1 | * | b2a-*+1
| | b2a-*+1*
'*1+*-a2b'
>>> infix_2_prefix('') >>> infix_2_prefix('')
Symbol | Stack | Postfix Symbol | Stack | Postfix
@ -134,26 +166,21 @@ def infix_2_prefix(infix):
... ...
IndexError: list index out of range IndexError: list index out of range
>>> infix_2_prefix('())') # doctest: +NORMALIZE_WHITESPACE >>> infix_2_prefix('())')
Symbol | Stack | Postfix Traceback (most recent call last):
---------------------------- ...
( | ( | ValueError: invalid expression
( | (( |
) | ( |
| | (
'('
""" """
infix = list(infix[::-1]) # reverse the infix equation reversed_infix = list(infix[::-1]) # reverse the infix equation
for i in range(len(infix)): for i in range(len(reversed_infix)):
if infix[i] == "(": if reversed_infix[i] == "(":
infix[i] = ")" # change "(" to ")" reversed_infix[i] = ")" # change "(" to ")"
elif infix[i] == ")": elif reversed_infix[i] == ")":
infix[i] = "(" # change ")" to "(" reversed_infix[i] = "(" # change ")" to "("
return (infix_2_postfix("".join(infix)))[ # call infix_2_postfix on Infix, return reverse of Postfix
::-1 return (infix_2_postfix("".join(reversed_infix)))[::-1]
] # call infix_2_postfix on Infix, return reverse of Postfix
if __name__ == "__main__": if __name__ == "__main__":