46
RPN Calculator in Python 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

  • Upload
    others

  • View
    46

  • Download
    2

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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