rpn calculator in python 3 - temple university sitesrpn calculator in python 3. notation ways of...
TRANSCRIPT
![Page 1: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/1.jpg)
RPN Calculator in Python 3
![Page 2: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/2.jpg)
Notation
● Ways of writing an expression● How operators and operands are placed in an expression
![Page 3: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/3.jpg)
2 + 3
operand operand
operator
![Page 4: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/4.jpg)
Infix Notation
● Operator is placed between operands
2 + 3
![Page 5: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/5.jpg)
Infix Notation
● In absence of precedence rules● Order of operations is ambiguous● 14 or 20 depending on order
2 + 3 * 4
![Page 6: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/6.jpg)
Infix Notation
● Requires parentheses to remove ambiguity
(2 + 3) * 4
![Page 7: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/7.jpg)
Postfix Notation
● Operator follows all its operands● Also called Reverse Polish Notation (RPN)● Invented in the 1920● Value to computer science recognized 1950-60s
○ Works in a way similar to how computers execute expressions
2 3 +
![Page 8: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/8.jpg)
2 3 4 * +2 + 3 * 4
infix postfix (RPN)
![Page 9: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/9.jpg)
2 3 4 * +
![Page 10: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/10.jpg)
2 3 4 * +
3 4 * = 12
![Page 11: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/11.jpg)
2 3 4 * +
3 4 * = 12
2 12 +
![Page 12: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/12.jpg)
2 3 4 * +
3 4 * = 12
2 12 +2 12 + = 14
![Page 13: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/13.jpg)
2 3 4 * +
3 4 * = 12
2 12 +2 12 + = 14
14
![Page 14: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/14.jpg)
2 3 4 * +2 + 3 * 4
infix postfix (RPN)
2 3 + 4 *(2 + 3) * 4
postfix notation requires NO parentheses
![Page 15: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/15.jpg)
Stack
Wikipedia says:In computer science, a stack is an abstract data type that serves as a
collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the most recently added element that was not yet removed.
![Page 16: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/16.jpg)
Stack
Wikipedia says:In computer science, a stack is an abstract data type that serves as a
collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the most recently added element that was not yet removed.
![Page 17: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/17.jpg)
push
![Page 18: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/18.jpg)
push
![Page 19: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/19.jpg)
push
![Page 20: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/20.jpg)
![Page 21: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/21.jpg)
pop
![Page 22: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/22.jpg)
pop
![Page 23: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/23.jpg)
pop
![Page 24: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/24.jpg)
pop
![Page 25: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/25.jpg)
Stacks and postfix notation
● Stacks are the natural way to evaluate postfix expression
![Page 26: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/26.jpg)
2 3 4 * +
push
![Page 27: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/27.jpg)
2 3 4 * +
2 push
![Page 28: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/28.jpg)
2 3 4 * +
23
push
![Page 29: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/29.jpg)
2 3 4 * +
234
![Page 30: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/30.jpg)
2 3 4 * +
23 4
pop
![Page 31: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/31.jpg)
2 3 4 * +
243pop
![Page 32: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/32.jpg)
2 3 4 * +
243 *
![Page 33: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/33.jpg)
2 3 4 * +
212
push
![Page 34: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/34.jpg)
2 3 4 * +
212
![Page 35: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/35.jpg)
2 3 4 * +
212
![Page 36: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/36.jpg)
2 3 4 * +
2 pop
12
![Page 37: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/37.jpg)
2 3 4 * +
2pop
12
![Page 38: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/38.jpg)
2 3 4 * +
2 12 +
![Page 39: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/39.jpg)
2 3 4 * +
14
![Page 40: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/40.jpg)
2 3 4 * +
14push
![Page 41: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/41.jpg)
2 3 4 * +
14
![Page 42: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/42.jpg)
2 3 4 * +
14Top of stack has the expression value
![Page 43: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/43.jpg)
Python list as a stack
● Push operation is list.append()● Pop operation is list.pop()
![Page 44: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/44.jpg)
>>>> a = []>>>> a.append(2)>>>> a.append(3)>>>> a.append(4)>>>> print(a)[2, 3, 4]
>>>> top = a.pop()>>>> print(top)4>>>> print(a)[2, 3]
>>>> top = a.pop()>>>> print(top)3
>>>> top = a.pop()>>>> print(top)2
>>>> print(a)[]
![Page 45: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/45.jpg)
Homework
![Page 46: RPN Calculator in Python 3 - Temple University SitesRPN Calculator in Python 3. Notation Ways of writing an expression How operators and operands are placed in an expression. 2 + 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e6f32d6d0f2c50bca1bb069/html5/thumbnails/46.jpg)