Question: ### Stack applications Q 1 ) The first two exercises ask you to use the stack data structure to develop two distinct stack - driven
### Stack applications
QThe first two exercises ask you to use the stack data structure to develop two distinct stackdriven applications.
The completed stack implementation can be found in stackpy It is merely included for reference you should not modify this implementation to complete these exercises.
### Paired delimiter matching points
For this exercise you will implement the code to accept a dictionary which maps opening to closing delimiters with the caveat that all delimiters are onecharacter strings
Eg the following calls should return True:
checkdelimiters:
checkdelimiters:::
checkdelimitersbobmarytom: :
Eg the following calls should return False
checkdelimitersa b::
checkdelimitershi there"::
Note that there may be delimiters used in the string that are not in the dictionary. These should be ignored.
It is also possible that a pair of opening and closing delimiters use the same character eg and in which case they cannot be nested ie if you encounter a delimiter of this type and it is already on the stack, you should pop it off the stack rather than push another one on
Eg while the following call should return Falsebecause the brackets, which nest, are not correctly matched:
checkdelimiters::
the following calls should also return Falsebecause the double quotes do not nest:
checkdelimiters::
checkdelimiters::
Your implementation will go into the checkdelimiters function in stackapps.py
Q Another function we examined in class was one that used a stack to evaluate a postfix arithmetic expression. Because most of us are more accustomed to infixform arithmetic expressions eg however, the function seems to be of limited use. The good news: we can use a stack to convert an infix expression to postfix form!
To do so we will use the following algorithm:
Start with an empty list $L$ and an empty stack $S$ At the end of the algorithm, $L$ will contain the correctly ordered tokens of the postfix expression.
For each whitespace separated token $T$ in the infix expression, carry out the first of the following rules that applies:
If $T$ is a number, simply append it to $L$
If $S$ is empty or the top of $S$ is a left parenthesis, push $T$ onto $S$
If $T$ is a left parenthesis, push it onto $S$
If $T$ is a right parenthesis, keep popping values from $S$ and appending them to $L$ until a left parenthesis is popped from $S$ Note that the parentheses themselves are not appended to $L$
If $T$ has higher precedence than the top of $S$ or if the top of $S$ is a left parenthesis push it onto $S$
If $T$ has equal precedence with the top of $S$ pop $S$ and append the popped value to $L$ then push $T$ onto $S$
If $T$ has lower precedence than the top of $S$ pop $S$ and append the popped value to $L$ Then repeat these last three tests against the new top of $S$
After arriving at the end of the expression, pop and append all operators on $S$ to $L$
For your implementation, you will only need to handle the operators and where the first two have lower precedence than the second two. Assume that all expressions are wellformed ie valid
Your implementation will go into the infixtopostfix function in stackapps.py
stackapps.py
from stack import Stack
def checkdelimitersexpr: str delims: dictstrstr bool:
pass
def infixtopostfixexpr: str str:
pass
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
