Skip to content

Fixed error in infix2prefix file #4006

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
* [Decimal To Octal](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_octal.py)
* [Hexadecimal To Decimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/hexadecimal_to_decimal.py)
* [Molecular Chemistry](https://github.com/TheAlgorithms/Python/blob/master/conversions/molecular_chemistry.py)
* [Octal To Decimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/octal_to_decimal.py)
* [Prefix Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/prefix_conversions.py)
* [Roman To Integer](https://github.com/TheAlgorithms/Python/blob/master/conversions/roman_to_integer.py)
* [Temperature Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/temperature_conversions.py)
Expand Down Expand Up @@ -207,6 +208,7 @@
* [Heaps Algorithm Iterative](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/heaps_algorithm_iterative.py)
* [Inversions](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/inversions.py)
* [Kth Order Statistic](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/kth_order_statistic.py)
* [Max Difference Pair](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/max_difference_pair.py)
* [Max Subarray Sum](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/max_subarray_sum.py)
* [Mergesort](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/mergesort.py)
* [Peak](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/peak.py)
Expand Down Expand Up @@ -875,6 +877,8 @@
* [Get Imdb Top 250 Movies Csv](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_imdb_top_250_movies_csv.py)
* [Get Imdbtop](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_imdbtop.py)
* [Instagram Crawler](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_crawler.py)
* [Instagram Pic](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_pic.py)
* [Recaptcha Verification](https://github.com/TheAlgorithms/Python/blob/master/web_programming/recaptcha_verification.py)
* [Slack Message](https://github.com/TheAlgorithms/Python/blob/master/web_programming/slack_message.py)
* [Test Fetch Github Info](https://github.com/TheAlgorithms/Python/blob/master/web_programming/test_fetch_github_info.py)
* [World Covid19 Stats](https://github.com/TheAlgorithms/Python/blob/master/web_programming/world_covid19_stats.py)
78 changes: 40 additions & 38 deletions data_structures/stacks/infix_to_prefix_conversion.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Output:

Enter an Infix Equation = a + b ^c
Enter an infix Equation = a + b ^c
Symbol | Stack | Postfix
----------------------------
c | | c
Expand All @@ -10,14 +9,13 @@
+ | + | cb^
a | + | cb^a
| | cb^a+

a+b^c (Infix) -> +a^bc (Prefix)
a+b^c (infix) -> +a^bc (Prefix)
"""


def infix_2_postfix(Infix):
Stack = []
Postfix = []
def infix_2_postfix(infix):
stack = []
postfix = []
priority = {
"^": 3,
"*": 2,
Expand All @@ -26,7 +24,7 @@ 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) if (len(infix) > 7) else 7

# Print table header for output
print(
Expand All @@ -37,57 +35,61 @@ def infix_2_postfix(Infix):
)
print("-" * (print_width * 3 + 7))

for x in Infix:
for x in infix:
if x.isalpha() or x.isdigit():
Postfix.append(x) # if x is Alphabet / Digit, add it to Postfix
postfix.append(x) # if x is Alphabet / Digit, add it to postfix
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
while Stack[-1] != "(":
Postfix.append(Stack.pop()) # Pop stack & add the content to Postfix
Stack.pop()
while stack[-1] != "(":
postfix.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
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]]:
Postfix.append(Stack.pop()) # pop stack & add to Postfix
Stack.append(x) # push x to stack
while (
len(stack) > 0
and stack[-1] in priority
and priority[x] <= priority[stack[-1]]
):
postfix.append(stack.pop()) # pop stack & add to postfix
stack.append(x) # push x to stack

print(
x.center(8),
("".join(Stack)).ljust(print_width),
("".join(Postfix)).ljust(print_width),
("".join(stack)).ljust(print_width),
("".join(postfix)).ljust(print_width),
sep=" | ",
) # Output in tabular format

while len(Stack) > 0: # while stack is not empty
Postfix.append(Stack.pop()) # pop stack & add to Postfix
while len(stack) > 0: # while stack is not empty
postfix.append(stack.pop()) # pop stack & add to postfix
print(
" ".center(8),
("".join(Stack)).ljust(print_width),
("".join(Postfix)).ljust(print_width),
("".join(stack)).ljust(print_width),
("".join(postfix)).ljust(print_width),
sep=" | ",
) # Output in tabular format

return "".join(Postfix) # return Postfix as str
return "".join(postfix) # return postfix as str


def infix_2_prefix(Infix):
Infix = list(Infix[::-1]) # reverse the infix equation
def infix_2_prefix(infix):
infix = list(infix[::-1]) # reverse the infix equation

for i in range(len(Infix)):
if Infix[i] == "(":
Infix[i] = ")" # change "(" to ")"
elif Infix[i] == ")":
Infix[i] = "(" # change ")" to "("
for i in range(len(infix)):
if infix[i] == "(":
infix[i] = ")" # change "(" to ")"
elif infix[i] == ")":
infix[i] = "(" # change ")" to "("

return (infix_2_postfix("".join(Infix)))[
return (infix_2_postfix("".join(infix)))[
::-1
] # call infix_2_postfix on Infix, return reverse of Postfix
] # call infix_2_postfix on infix, return reverse of postfix


if __name__ == "__main__":
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)")
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)")