Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Language Python: You will be expected to use an appropriate ADT to s

ID: 3885583 • Letter: P

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 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()

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

Explanation / Answer

class Stack:
     def __init__(self):
         self.items = []

     def isEmpty(self):
         return self.items == []

     def push(self, item):
         self.items.append(item)

     def pop(self):
         return self.items.pop()

     def peek(self):
         return self.items[len(self.items)-1]

     def size(self):
         return len(self.items)

from pythonds.basic.stack import Stack

s=Stack()

print(s.isEmpty())
s.push(4)
s.push('dog')
print(s.peek())
s.push(True)
print(s.size())
print(s.isEmpty())
s.push(8.4)
print(s.pop())
print(s.pop())
print(s.size())