Add tests for infix_2_postfix() in infix_to_prefix_conversion.py (#10095)

* 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)

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

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

* Change type hints in infix_to_prefix_conversion.py

* Remove printing trailing whitespace in the output table

* Fix type hint errors

* Fix doctests

* Adjust table convention in output and doctests

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

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

* Add doctests for infix_2_postfix()

* Update print_width

* Update print_width

* Fix the doctests

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
hollowcrust 2023-10-11 00:52:53 +08:00 committed by GitHub
parent 0b440285e8
commit 5be5d21bed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,6 +16,39 @@ Enter an Infix Equation = a + b ^c
def infix_2_postfix(infix):
"""
>>> infix_2_postfix("a+b^c") # doctest: +NORMALIZE_WHITESPACE
Symbol | Stack | Postfix
----------------------------
a | | a
+ | + | a
b | + | ab
^ | +^ | ab
c | +^ | abc
| + | abc^
| | abc^+
'abc^+'
>>> infix_2_postfix("1*((-a)*2+b)")
Traceback (most recent call last):
...
KeyError: '('
>>> infix_2_postfix("")
Symbol | Stack | Postfix
----------------------------
''
>>> infix_2_postfix("(()") # doctest: +NORMALIZE_WHITESPACE
Symbol | Stack | Postfix
----------------------------
( | ( |
( | (( |
) | ( |
| | (
'('
>>> infix_2_postfix("())")
Traceback (most recent call last):
...
IndexError: list index out of range
"""
stack = []
post_fix = []
priority = {
@ -74,6 +107,42 @@ def infix_2_postfix(infix):
def infix_2_prefix(infix):
"""
>>> infix_2_prefix("a+b^c") # doctest: +NORMALIZE_WHITESPACE
Symbol | Stack | Postfix
----------------------------
c | | c
^ | ^ | c
b | ^ | cb
+ | + | cb^
a | + | cb^a
| | cb^a+
'+a^bc'
>>> infix_2_prefix("1*((-a)*2+b)")
Traceback (most recent call last):
...
KeyError: '('
>>> infix_2_prefix('')
Symbol | Stack | Postfix
----------------------------
''
>>> infix_2_prefix('(()')
Traceback (most recent call last):
...
IndexError: list index out of range
>>> infix_2_prefix('())') # doctest: +NORMALIZE_WHITESPACE
Symbol | Stack | Postfix
----------------------------
( | ( |
( | (( |
) | ( |
| | (
'('
"""
infix = list(infix[::-1]) # reverse the infix equation
for i in range(len(infix)):
@ -88,6 +157,10 @@ 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)")