Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 10, 2023
73 changes: 73 additions & 0 deletions data_structures/stacks/infix_to_prefix_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,39 @@


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 = {
Expand Down Expand Up @@ -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)):
Expand All @@ -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)")