E Xercise 10 - Reverse Poltsh (46 Mark?) : 3 Def Getelements (Expression)
E Xercise 10 - Reverse Poltsh (46 Mark?) : 3 Def Getelements (Expression)
)
Mathematical expressions are usually written in 'infix' notation, meaning that operations
use the values on either side of the operation. For example, the expression '2 +2"is
evaluated by adding the value on the left of the '+' operator to the value on its right.
In RPN, the operator comes after the values it operates on, so the infix expression '2 + 2
would instead be written as '2 2 +'. RPN expressions are evaluated from left to right, so
the expression '3 4 2 - *' would be evaluated as follows:
342-% The(-)operatoris reached and operates on the two operands that come before it
32% 42 - is simplified to its result, 2. The next operator (*) operates on the two operands that precede it.
6 32 * s then simplified to its result, 6.
Shown below (and provided electronically) is a program that gets an infix expression from the user, splits the
expression into separate elements, and converts the order of the elements from infix notation to RPN. Study the
code and try to understand what is happening in the program, before attempting the questions that follow.
8 for i in expression:
o if i t=" "
10 element = element + i
abik else:
12 elements.append (element)
i3 element = ""
14 valid = checkExpression(elements)
15 if valid:
16 return elements
17 else:
18 print ("Invalid expression given!")
19 returnf]
21 def checkExpression(elements):
22 lastElement = None
23 for element in elements:
24 if isInt(element):
25 element = int(element)
26 else:
27 isOperator = False
28 for operator in operators:
29 if element == operator:
30 isOperator = True
31 if isOperator == False:
32 return False
33 if isInt(lastElement):
34 if isInt(element):
8b; return False
36 else:
49 def infixToRPN(elements):
50 stack = []
51 opStack = []
52 for element in elements:
53 if isInt(element):
54 stack.append (element
55 else:
56 if opStack != []:
57 lastOp = opStack[-1]
58 if opStack [] or element == '(' or ((lastOp == '+' or
lastOp == '-') and (element == '/"' or element ey
59 opStack.append (element)
60 elif element == ')':
61 operator = None
62 while operator != '(' and opStack [1:
63 operator = opStack.pop (
64 if operator != '(':
65 stack.append (operator
66 else:
67 if lastOp != '(':
68 stack.append (lastOp)
69 opStack[-1] = element
70 else:
71 opStack.append (element
72 for i in range (len(opStack)):
73 stack.append (opStack.pop () )
74 return stack
76 elements = getElements
(input ("Enter an expression: "y
77 elements = infixToRPN(elements)
78 print(elements)
79 input()
A Give a line number from the program that contains a comparison operator. m
Explain how the isInt function determines whether or not the given value represents an integer. [2]
The program currently does not always add the final operand given to the list of elements.
Explain the cause of this error. [1)]
Al 9 Draw the binary tree that is created by the following infix expression: 8 /2 -3 * 1 [61
A 10| Draw the binary tree that is created by the following RPN expression: 47 6 - 2 * / [61
/25