0% found this document useful (0 votes)
2 views4 pages

E Xercise 10 - Reverse Poltsh (46 Mark?) : 3 Def Getelements (Expression)

The document discusses Reverse Polish Notation (RPN) as an alternative to infix notation for mathematical expressions, where operators follow their operands. It includes a program that converts infix expressions to RPN and outlines the structure of the code, including functions for validating expressions and converting them. Additionally, it poses questions related to the program and binary tree representations of expressions.

Uploaded by

User.9463820
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

E Xercise 10 - Reverse Poltsh (46 Mark?) : 3 Def Getelements (Expression)

The document discusses Reverse Polish Notation (RPN) as an alternative to infix notation for mathematical expressions, where operators follow their operands. It includes a program that converts infix expressions to RPN and outlines the structure of the code, including functions for validating expressions and converting them. Additionally, it poses questions related to the program and binary tree representations of expressions.

Uploaded by

User.9463820
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

E XeRCISE 10 — REVERSE PolTsH (46 MArK?

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

An alternative way of writing mathematical expressions is using ‘postfix’ notation, also


commonly known as Reverse Polish Notation (RPN). In case you were wondering, ‘Polish’is in
reference to the nationality of Jan tukasiewicz (pictured), who invented the notation in 1924.

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.

1 operators = ['+', '-', '*', '/']

3 def getElements (expression):


4 elements (1
9 element = ""
6 expression = expression.strip()

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:

Python Exercises for A Level Page 41 of 92 © ZigZag Education, 2019


37 if not isInt(element):
38 return False
39 lastElement = element
40 return True

42 def isInt (value):


43 try:
44 int (value)
45 return True
46 except:
47 return False

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

Python Exercises for A Level Page 42 of 92 © ZigZag Education, 2019


SECTION A

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]

Write the RPN form of the following infix expression: (3 +2) * (4 - 1) / 4 21

The program currently does not always add the final operand given to the list of elements.
Explain the cause of this error. [1)]

Python Exercises for A Level Page 43 of 92 © ZigZag Education, 2019


Al 7 Mathematical expressions can be represented as a binary tree, where a postorder tree traversal will
produce the RPN expression, and an inorder tree traversal will produce the infix expression.
Write the RPN expression produced by the following binary tree: [2]

A| 8| Write the infix expression produced by the binary tree in A7. 2]

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

Python Exercises for A Level Page 44 of 92 © ZigZag Education, 2019

You might also like