Skip to content

Commit 5be5d21

Browse files
hollowcrustpre-commit-ci[bot]cclauss
authoredOct 10, 2023
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>
1 parent 0b44028 commit 5be5d21

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
 

‎data_structures/stacks/infix_to_prefix_conversion.py

+73
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,39 @@
1616

1717

1818
def infix_2_postfix(infix):
19+
"""
20+
>>> infix_2_postfix("a+b^c") # doctest: +NORMALIZE_WHITESPACE
21+
Symbol | Stack | Postfix
22+
----------------------------
23+
a | | a
24+
+ | + | a
25+
b | + | ab
26+
^ | +^ | ab
27+
c | +^ | abc
28+
| + | abc^
29+
| | abc^+
30+
'abc^+'
31+
>>> infix_2_postfix("1*((-a)*2+b)")
32+
Traceback (most recent call last):
33+
...
34+
KeyError: '('
35+
>>> infix_2_postfix("")
36+
Symbol | Stack | Postfix
37+
----------------------------
38+
''
39+
>>> infix_2_postfix("(()") # doctest: +NORMALIZE_WHITESPACE
40+
Symbol | Stack | Postfix
41+
----------------------------
42+
( | ( |
43+
( | (( |
44+
) | ( |
45+
| | (
46+
'('
47+
>>> infix_2_postfix("())")
48+
Traceback (most recent call last):
49+
...
50+
IndexError: list index out of range
51+
"""
1952
stack = []
2053
post_fix = []
2154
priority = {
@@ -74,6 +107,42 @@ def infix_2_postfix(infix):
74107

75108

76109
def infix_2_prefix(infix):
110+
"""
111+
>>> infix_2_prefix("a+b^c") # doctest: +NORMALIZE_WHITESPACE
112+
Symbol | Stack | Postfix
113+
----------------------------
114+
c | | c
115+
^ | ^ | c
116+
b | ^ | cb
117+
+ | + | cb^
118+
a | + | cb^a
119+
| | cb^a+
120+
'+a^bc'
121+
122+
>>> infix_2_prefix("1*((-a)*2+b)")
123+
Traceback (most recent call last):
124+
...
125+
KeyError: '('
126+
127+
>>> infix_2_prefix('')
128+
Symbol | Stack | Postfix
129+
----------------------------
130+
''
131+
132+
>>> infix_2_prefix('(()')
133+
Traceback (most recent call last):
134+
...
135+
IndexError: list index out of range
136+
137+
>>> infix_2_prefix('())') # doctest: +NORMALIZE_WHITESPACE
138+
Symbol | Stack | Postfix
139+
----------------------------
140+
( | ( |
141+
( | (( |
142+
) | ( |
143+
| | (
144+
'('
145+
"""
77146
infix = list(infix[::-1]) # reverse the infix equation
78147

79148
for i in range(len(infix)):
@@ -88,6 +157,10 @@ def infix_2_prefix(infix):
88157

89158

90159
if __name__ == "__main__":
160+
from doctest import testmod
161+
162+
testmod()
163+
91164
Infix = input("\nEnter an Infix Equation = ") # Input an Infix equation
92165
Infix = "".join(Infix.split()) # Remove spaces from the input
93166
print("\n\t", Infix, "(Infix) -> ", infix_2_prefix(Infix), "(Prefix)")

0 commit comments

Comments
 (0)
Please sign in to comment.