From ef77a8046f863ce917ca341b7305a34a8b2317c6 Mon Sep 17 00:00:00 2001 From: nutanaarohi123 Date: Mon, 31 Aug 2020 16:21:08 +0530 Subject: [PATCH 1/3] Added Infix to Postfix Conversion in Python --- .../Infix_to_Postfix_Conversion.py | 64 +++++++++++++++++++ .../Infix_to_Postfix_Conversion/README.txt | 12 ++++ 2 files changed, 76 insertions(+) create mode 100644 Basic-Scripts/Infix_to_Postfix_Conversion/Infix_to_Postfix_Conversion.py create mode 100644 Basic-Scripts/Infix_to_Postfix_Conversion/README.txt diff --git a/Basic-Scripts/Infix_to_Postfix_Conversion/Infix_to_Postfix_Conversion.py b/Basic-Scripts/Infix_to_Postfix_Conversion/Infix_to_Postfix_Conversion.py new file mode 100644 index 00000000..c8a5afc9 --- /dev/null +++ b/Basic-Scripts/Infix_to_Postfix_Conversion/Infix_to_Postfix_Conversion.py @@ -0,0 +1,64 @@ +# function to convert infix to postfix using array as stack +def infix_to_postfix(infix_expression): + """ + Function to change infix expression to postfix one + Params: + infix_expression: Infix Expression provided by user + Returns: + postfix_expression: Postfix Expression convertef from Infix one + """ + + # initially empty stack + stack = [] + # initially empty postfix_expression + postfix_expression = "" + for char in infix_expression: + # if an operand then put it directly in postfix expression + if char not in operators: + postfix_expression += char + # else if operators should be put in stack + elif char == "(": + """append function to push + elements in the stack""" + stack.append("(") + elif char == ")": + while stack and stack[-1] != "(": + postfix_expression += stack.pop() + """ pop function to pop + elements from stack in LIFO order """ + stack.pop() + else: + """if priority of char in infix_expression is less than or equal to + char at stack[-1] pop out and put in postfix_expression""" + while ( + stack and stack[-1] != "(" and priorities[char] <= priorities[stack[-1]] + ): + postfix_expression += stack.pop() + stack.append(char) + while stack: + postfix_expression += stack.pop() + return postfix_expression + + +# Set of operators +operators = set(["+", "-", "*", "/", "(", ")", "^"]) + +# dictionary having priorities +priorities = {"+": 1, "-": 1, "*": 2, "/": 2, "^": 3} + +print("Input") +infix_expression = input("Enter infix expression\n") +print("Output") + +# Displaying the Output +print("Postfix expression: ", infix_to_postfix(infix_expression)) + +""" + Input: + Enter infix expression: A+C*(B^D-E)^(G+H*K)-K + Output: + Postfix expression: ACBD^E-GHK*+^*+K- + + Time Complexity : O(n) + Space Complexity: Θ(n) +""" diff --git a/Basic-Scripts/Infix_to_Postfix_Conversion/README.txt b/Basic-Scripts/Infix_to_Postfix_Conversion/README.txt new file mode 100644 index 00000000..6735847d --- /dev/null +++ b/Basic-Scripts/Infix_to_Postfix_Conversion/README.txt @@ -0,0 +1,12 @@ +#In Infix expression operator is in between every pair of operands. +# In Postfix expression, the operator is followed for every pair of operands. +#Infix expression is converted to postfix conversion using Stack. +# Postfix expression is evaluated using Stack in Left to Right order. +#If the scanned character is operand, output it +#Else, If precedence of scanned operator os greater than the precedence of the operator in the stack,push it. +#Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator. +#If the scanned input is '(', push it into stack +#If the scanned input is ')' ,pop the stack and the output it until '(' comes. +#Repeat above steps. +#pop and output from stack until it becomes empty +# It makes the code more efficient and even reduces the time complexity. From f6febbf451af866a2993271e924b79cbb83333ec Mon Sep 17 00:00:00 2001 From: nutanaarohi123 Date: Mon, 31 Aug 2020 18:03:17 +0530 Subject: [PATCH 2/3] Updated --- .../Infix_to_Postfix_Conversion.py | 10 ------- .../Infix_to_Postfix_Conversion/README.md | 27 +++++++++++++++++++ .../Infix_to_Postfix_Conversion/README.txt | 12 --------- 3 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 Basic-Scripts/Infix_to_Postfix_Conversion/README.md delete mode 100644 Basic-Scripts/Infix_to_Postfix_Conversion/README.txt diff --git a/Basic-Scripts/Infix_to_Postfix_Conversion/Infix_to_Postfix_Conversion.py b/Basic-Scripts/Infix_to_Postfix_Conversion/Infix_to_Postfix_Conversion.py index c8a5afc9..aab586a5 100644 --- a/Basic-Scripts/Infix_to_Postfix_Conversion/Infix_to_Postfix_Conversion.py +++ b/Basic-Scripts/Infix_to_Postfix_Conversion/Infix_to_Postfix_Conversion.py @@ -52,13 +52,3 @@ def infix_to_postfix(infix_expression): # Displaying the Output print("Postfix expression: ", infix_to_postfix(infix_expression)) - -""" - Input: - Enter infix expression: A+C*(B^D-E)^(G+H*K)-K - Output: - Postfix expression: ACBD^E-GHK*+^*+K- - - Time Complexity : O(n) - Space Complexity: Θ(n) -""" diff --git a/Basic-Scripts/Infix_to_Postfix_Conversion/README.md b/Basic-Scripts/Infix_to_Postfix_Conversion/README.md new file mode 100644 index 00000000..5c274318 --- /dev/null +++ b/Basic-Scripts/Infix_to_Postfix_Conversion/README.md @@ -0,0 +1,27 @@ +### Infix to Postfix + +> • In Infix expression operator is in between every pair of operands.
+> • In Postfix expression, the operator is followed for every pair of operands. + +- Infix expression is converted to postfix conversion using Stack. +- Postfix expression is evaluated using Stack in Left to Right order. + +##### If the scanned character is operand, show it as output. Else, If precedence of scanned operator is greater than the precedence of the operator in the stack,push it. + +> Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator. + +• If the scanned input is '(', push it into stack
+• If the scanned input is ')' ,pop the stack and the output it until '(' comes.
+• Repeat above steps. Continue Pop and output from stack until it becomes empty. + +##### It makes the code more efficient and even reduces the time complexity. + +""" + Input: + Enter infix expression: A+C*(B^D-E)^(G+H*K)-K + Output: + Postfix expression: ACBD^E-GHK*+^*+K- + + Time Complexity : O(n) + Space Complexity: Θ(n) +""" \ No newline at end of file diff --git a/Basic-Scripts/Infix_to_Postfix_Conversion/README.txt b/Basic-Scripts/Infix_to_Postfix_Conversion/README.txt deleted file mode 100644 index 6735847d..00000000 --- a/Basic-Scripts/Infix_to_Postfix_Conversion/README.txt +++ /dev/null @@ -1,12 +0,0 @@ -#In Infix expression operator is in between every pair of operands. -# In Postfix expression, the operator is followed for every pair of operands. -#Infix expression is converted to postfix conversion using Stack. -# Postfix expression is evaluated using Stack in Left to Right order. -#If the scanned character is operand, output it -#Else, If precedence of scanned operator os greater than the precedence of the operator in the stack,push it. -#Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator. -#If the scanned input is '(', push it into stack -#If the scanned input is ')' ,pop the stack and the output it until '(' comes. -#Repeat above steps. -#pop and output from stack until it becomes empty -# It makes the code more efficient and even reduces the time complexity. From 30903b215c9644d01e58b5dad1f1e3745956b043 Mon Sep 17 00:00:00 2001 From: Nutan <35139185+nutanaarohi123@users.noreply.github.com> Date: Mon, 31 Aug 2020 18:10:52 +0530 Subject: [PATCH 3/3] Update README.md --- Basic-Scripts/Infix_to_Postfix_Conversion/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Basic-Scripts/Infix_to_Postfix_Conversion/README.md b/Basic-Scripts/Infix_to_Postfix_Conversion/README.md index 5c274318..d6bc1a60 100644 --- a/Basic-Scripts/Infix_to_Postfix_Conversion/README.md +++ b/Basic-Scripts/Infix_to_Postfix_Conversion/README.md @@ -3,8 +3,8 @@ > • In Infix expression operator is in between every pair of operands.
> • In Postfix expression, the operator is followed for every pair of operands. -- Infix expression is converted to postfix conversion using Stack. -- Postfix expression is evaluated using Stack in Left to Right order. +> Infix expression is converted to postfix conversion using Stack. +> Postfix expression is evaluated using Stack in Left to Right order. ##### If the scanned character is operand, show it as output. Else, If precedence of scanned operator is greater than the precedence of the operator in the stack,push it. @@ -15,7 +15,7 @@ • Repeat above steps. Continue Pop and output from stack until it becomes empty. ##### It makes the code more efficient and even reduces the time complexity. - +### Constraints """ Input: Enter infix expression: A+C*(B^D-E)^(G+H*K)-K @@ -24,4 +24,4 @@ Time Complexity : O(n) Space Complexity: Θ(n) -""" \ No newline at end of file +"""