DA 219 Data Structures for Data Analytics Assignment 4
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
DATA 5TRucTuREs FoR DATA ANALYT1cs - DA 219
Assignment 4
Overall Assignment
In this assignment you’ll create a simple calculator that can read expressions from a String and produce a result. The calculator will use a syntax referred to as "Reverse Polish Notation" (RPN) or "Postfix", which has historically been common amongst scientific calculators like those created by HP. Download the assignment zip and extract it. You should see the file: Calculator.py Getting Started
|
The calculator is based on a stack. It works as follows: (1) when a number is entered, push it onto a stack. (2) when an operator is entered, pop the appropriate number of arguments off the stack, perform the operation, and push the result back onto the stack (3) when no more numbers or operators are left to process, pop the top value off the stack. This is the ’answer’ . We will use String expressions to interact with the calculator. A valid String consists of 1 or more numbers and 0 or more operators that are separated by whitespace (spaces, tabs). Collectively, we’ll refer to numbers or operators as “tokens” . For example, the String “4 2 +” contains two numbers: 4, and 2; and one operator "+". Thus, there are three tokens. Evaluating the String should begin a process that reads in each token and takes an action based on the token’s type (either number or operator). For the String above, this process should unfold as follows: (1) push the value 4 onto the stack; (2) push the value 2 onto the stack; (3) pop two values off the stack, compute 4 + 2 and push the result, 6, onto the stack. Then (4) since the expression is fully processed (i.e., there are no more tokens are left to process), we should pop the top element off the stack, here it is 6, and return that value as the answer. Implementing the Calculator
|
The Calculator.py file contains a partially implemented class. Your job is to complete it. To get you started, the skeleton code contains method signatures for all of the methods that you must implement. Look through the code, and consider: 1) What instance variables will you need to support your work? Hint, use a list for your stack. 2) How will you process the string expression? Here, you’ll likely want to make use of “if”, “in”, and isnumeric. Remember, isnumeric() is a method of a string that allows you to check if the current string is a number or not. Rubric
|
40% Successfully completing Basic Operations
40% Successfully completing Variables
20% Code style and quality
What to turn in:
• Calculator.py
2023-03-22