Read the instructions carefully before starting the assignment. Make sure your code follows the stated guidelines to ensure full credit for your work.


Instructions:


- The work in this assignment might be complete alone or on teams of 2 students. Whether or not you work on a team, work must be your own.

- This is a straightforward upgrade of HW4 and there is no starter code. If your code did not complete your code, you need to fix it before starting this assignment.

- A doctest is provided as an example of code functionality. Getting the same result as the doctest does not guarantee full credit. You are responsible for debugging and testing your code with enough data, you can share ideas and testing code during your recitation class.

- Each function must return the output (Do not use print in your final submission, otherwise your submissions will receive a -10 point deduction)

- Do not include test code outside any function in the upload. Printing unwanted or illformatted data to output will cause the test cases to fail. Remove all your testing code before uploading your file (You can also remove the doctest). Do not include the input() function in your submission.

- When any function returns an error, it must be a string containing “error”


IF YOU ARE WORKING ON A TEAM:


- Team size is limited to 2 students, no exceptions!

- In order to accept your submission as a team, both team members must submit a “Contract” prior the submission of the assignment. This contract should include: Names of both students, Goal (goal of the assignment), Meeting Schedule (tentative, it should include a proposed location if meeting in person or application name if working virtually), Accountability Factors (what would keep both students on track and moving towards their goal, for example ‘Inform team of any delays/changes to schedule’) and Violations of Contract (what are the consequences for not following this contract).

- If contract is not submitted by the due date, you must work alone.


Goal:

In HW4, your wrote the function postfix(expr). This function takes an arithmetic expression in infix notation (string that could have multiple spaces) and returns a string that contains expr in postfix notation. Modify the function postfix(expr) so it can support the use of parentheses. Write the function calculator(expr). This function takes an arithmetic expression in infix notation, calls postfix(expr) to obtain the postfix expression of expr and then uses a Stack to evaluate expr. All values returned by calculator should be float, unless is an error message (string).

Notes:


- If you are unable to support parentheses, your calculator must work for expressions with no parentheses for the most partial credit possible

- You are not allowed to use eval or exec. You will not get credit if you use them

- You can write an additional function to get the parenthesis between operator, or modify findNextOpr and getNextNumber to support ‘(‘ and ‘)’

- You can write an utility function (num1,opr,num2) to perform the operations for calculator

- calculator must use the stack from Lab 10 to evaluate the postfix expression. Refer to the Stack video lecture on how to use the stack for such evaluation.

- Operations like ‘3(5)’ are not supported. Expression will be ‘3*(5)’

- You will need to remove ‘(’ and ‘)’ when sending the string to isNumber from getNextNumber, in an expression like ‘3+(5-8)’, you will be sending ‘(5’ to is Number and that is not castable to float. You can use the replace method strNumber.replace('(','').replace(')','')

Grading Notes:

- The grading script will feed 10 randomly chosen test inputs, each for 10 points. Three of them will be inputs that should cause an error such as “4 * (5/ 2) + ^2” or “4 * ((5/ 2) + 2))”, whose expected returned value is an error message.
Examples:
>>> postfix('(2)')
'2.0'
>>> postfix ('2')
'2.0'
>>> postfix ('((2))')
'2.0'
>>> postfix (' 2 * (( 5 + 3) ^ -2+(1 +4)) ')
'2.0 5.0 3.0 + -2.0 ^ 1.0 4.0 + + *'
>>> postfix (' ( 2 * (( 5 + 3) ^ -2+(1 +4))) ')
'2.0 5.0 3.0 + -2.0 ^ 1.0 4.0 + + *'
>>> postfix (' (( 2 * (( 5 + 3) ^ -2+(1 +4)))) ')
'2.0 5.0 3.0 + -2.0 ^ 1.0 4.0 + + *'
>>> postfix(' 2 * ( 5 + 3) ^ -2+(1 +4) ')
'2.0 5.0 3.0 + -2.0 ^ * 1.0 4.0 + +'
>>> calculator('3*(10 - 2*3)')
12.0
>>> calculator(' -2 / (-4) * (3 - 2*( 4- 2^3)) + 3')
8.5
>>> calculator('2*(4+2*(5-3^2)+1)+4')
-2.0
>>> calculator('2 + 3 * ( -2 +(-3) *(5^2 - 2*3^(-2) ) *(-4) ) * ( 2 /8 + 2*( 3
- 1/ 3) ) - 2/ 3^2')
4948.611111111111
>>> calculator('(-2)*10 - 3*(2 - 3*2)) ')
'error, invalid expression'
>>> calculator('(-2)*10 - 3*/(2 - 3*2) ')
'error, invalid expression'

Deliverables:

  • Include all the functions (isNumber, findNextOpr, getNextNumber, postfix, calculator and the stack class) in your script named HW5.py. Submit it to the HW5 Gradescope assignment before the due date. If you are submitting as a team, only one student must upload the submission, however, you must add the second member of the team.
  • Watch how to submit group submissions on Gradescope here: https://youtu.be/rue7p_kATLA

Tips for this assignment:

Let’s consider an expression in infix notation:

Before starting any computations, does the expression has balanced parenthesis? No, return error

Your comparisons for precedence should remain the same if working correctly. The extra step is to check for parenthesis between the old and new operator positions. When an open parenthesis is found, we push it into the stack, when a closing parenthesis is found we pop operators from the stack until we find an open parenthesis.
postfix_expression ?