Question: single file that contains two functions (Paren Match and Evaluate Postfix) and a main program Consider the following algorithm that determines whether the parenthesis in


single file that contains two functions (Paren Match and Evaluate Postfix) and a main program Consider the following algorithm that determines whether the parenthesis in an arithmetic expression are properly matched: Algorithm ParenMatch(X): Input: A string X of n tokens, each of which is either a grouping symbol from the lefty list of three symbols: ({[ or the righty list of three symbols: ) } ] or from the list of single character uppercase variables: ABC... Z or from the list of arithmetic binary operators: +-*/ There are NO blanks in the string. Output: True if and only if all the grouping symbols in X match; otherwise, print False. Let S be an empty stack for i=0 to n-1 do if X[] is an opening grouping symbol then #i.e. {[( S.push(X[1]) else if X[i] is a closing grouping symbol then #i.e. }]) if S.is_empty() then return false #nothing to match with if S.pop() does not match the type of X[1] then return false #wrong type if S.isEmpty() then return true #every symbol matched else return false #some symbols were never matched Part A: Paren Match Implement the above algorithm as a Python function named ParenMatch that returns either True or False according to the input list of tokens does or does not have the various parenthesis properly matched. (In PartA, you are not being asked to evaluate any expression or to even determine whether it is otherwise a proper binary arithmetic expression. You are only asked to determine whether the parenthesis properly match; including whether any match is a match of corresponding parenthesis types.) Part B: Evaluate Postfix Implement an algorithm named PostfixEvaluation to evaluate a Postfix expression that contains arithmetic only operators +, -, and * and positive integer operands. Your implementation can assume that each postfix expression is a correct postfix expression and that the expression contains operands and/or operators each separated by one or more blanks. See your lecture notes for a sample implementation, however, it must be changed to accommodate multi-digit numbers and spaces. Main Method Both Part A and B will be included in the same python source file. The root (main) programing will consist of the following function calls: Paren Match("A*B+C*D") Paren Match("(A+B)*(C-(D-E))*(F+G)") Paren Match("((A+{B+C)-[D-E])+[F]-[G]") Paren Match("(A+B)*(C+D)") Paren Match("A/B-C+D*E-A*C") Paren Match("(((A/B)-C)+(D*E))-A*C)") Paren Match("A/(B-C+D))*(E-A)*C") Postfix Expression("9 7 2 - *") PostfixEvaluation("10 2 8 * + 3 -") PostfixEvaluation("5 2 + 8 3 - * 4 -"). PostfixExpression(20 3 1 * + 90 -")
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
