Skip to content
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

Added doctests, exceptions, type hints, fixed bug for infix_to_prefix_conversion #10062

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 64 additions & 4 deletions data_structures/stacks/infix_to_prefix_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""


def infix_2_postfix(infix):
def infix_2_postfix(infix: list[str]) -> str:
stack = []
post_fix = []
priority = {
Expand All @@ -26,7 +26,8 @@ def infix_2_postfix(infix):
"+": 1,
"-": 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(
Expand All @@ -43,14 +44,21 @@ 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()
else:
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

Expand All @@ -62,6 +70,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),
Expand All @@ -73,7 +84,52 @@ def infix_2_postfix(infix):
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')
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)):
Expand All @@ -88,6 +144,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)")