Question: Programming Language Python: You will be expected to use an appropriate ADT to support this application. The ADT should be implemented as a class. A

Programming Language Python:

You will be expected to use an appropriate ADT to support this application. The ADT should be implemented as a class.

A mathematical expression in infix notation has the operators placed between the operands. With such a notation, it is necessary to use parentheses to change the order in which the operators are evaluated. An example of an expression in infix notation is:

3 * (1 + 5)

which evaluates to 18.

In this question, you must write a function called q2() that evaluates infix expressions. The usual four mathematical operators should be supported (+, -, *, /), however there are three additional operators that must also be supported:

Operator

Description

Example

^

This is the exponentiation operator. It raises the base (the left operand) to the power of the exponent (the right operand)

2 ^ 10 = 1024

5 ^ 2 = 25

<

This is the minimum operator. It evaluates to the smaller of the two operands

10 < 20 = 10

20 < 10 = 10

15 < 15 = 15

>

This is the maximum operator. It evaluates to the larger of the two operands

10 > 20 = 20

20 > 10 = 20

15 > 15 = 15

Relative to the four standard operators, the exponentiation operator (^) has higher precedence, and the minimum and maximum operators () have lower precedence. The following table summarizes the precedence of all of the operators:

Highest

2nd

3rd

4th

5th

()

^

*, /

+, -

<, >

In expressions consisting of several operators of the same precedence, the evaluation order is left to right. Some more examples are shown below:

1+ 100 > 200 = 200

1 +( 100 > 200 ) = 201

2 ^ ( 1 + 3 ^ 2 ) = 1024

40 > 15 < 35 + 10 = 40

( 40 > 15 < 35 ) + 10 = 45

2 * ( ( 4 < 2 + 3 ) + 3 * 4 ) = 32

The input to your q2() function will be a string. Each token in the string (whether an operator or an operand) will have a single space character on either side of it. You do not need to check the validity of the input string format you can assume this will be correct.

As an example, the code fragment on the left below should produce the output on the right:

print(q2(2 ^ (1 + 3 ^ 2 ))) outputs 1024

print(q2((3 * 5) (1 > 2 > 3 < 4))) outputs 12

Please complete the function q2()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!