Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Python: Am trying to write a code that can be tested and only have the evaluate function. Any assistance will be appreciated. In this

Using Python: Am trying to write a code that can be tested and only have the evaluate function. Any assistance will be appreciated.

In this assignment, we will implement a function to evaluate an arithmetic expression. We will use the algorithm outlined in the PowerPoint slides in Chapter 6. You will use two stacks to keep track things. The function is named evaluate, it takes a string as input parameter, and returns an integer. The input string represents an arithmetic expression, with each character in the string being a token, while the returned value should be the value of the expression. Examples: evaluate ('1+2*3') 7 evaluate ('1*2+3') 5 Download the four files csc220a3tester.py, csc220a3testData.py, ArrayStack.py, and csc220a3.py from Canvas and save them in the same folder. The first two files are the tester program and the testing data file, respectively. The third file is the ArrayStack class from the textbook. Do not modify these three files. The last file is the file you will work on. Note that you cannot rename this file and you cannot change the name of the function (otherwise the tester will not be able to pick up your implementation). However, you can change the name of the parameter if you like. The file csc220a3.py contains a dummy implementation of the function that returns a hardcoded (probably) incorrect value: from ArrayStack import ArrayStack def evaluate (expression): return -1 The tester program will use the testing data to test your implementation. It will run a total of 100 test cases. If you make no change to the function above, it should produce the following output:

Passed: [] - total 0. Failed: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100] - total 100. 0 of 100 test cases passed. Score = 0.00 of 15.00 Notes: 1. You can assume the input string is always a valid arithmetic expression, with all numerical values being one-digit integers (1-9). You do not have to check for the correctness of the input expression. 2. The only operators that will appear in the expression are +, -, and *, each of these operators are having the usual meaning as they are usually used in programming languages. 3. Your program will be tested with similar but different data. The testing results will earn you up to 15 points (out of 20 points total). 4. The remaining 5 points will be awarded based on the structure, readability, and documentation of your implementation. 5. You implementation should be compatible to Python 3.x. 6. There can only be one import statement in your program; and that statement must be from ArrayStack import ArrayStack. (This statement has already been put in the csc220a3.py file. 7. The tester contains a function runTestCase, this function takes a testcase number and run that single testcase. It will produce a more verbose output to help you debug your program. >>> runTestCase (50) Test 50 failed - expecting 65, got -1 instead.

image text in transcribedimage text in transcribed

from csc220a3TestData import tests def loadFunction (module, path, function): import sys sys.path.insert (0, path) try: return getattr import (module), function) finally: sys.path.pop (0) def message (record, msg, verbose): if verbose: record (msg) def runsingleTest (tc, expectedRe sult, expression, verbose= False, record = print): try: result - evaluate (expression) except Exception as err: message (record, return 0 'Test %d failed- %s caught- %s' % (tc, type (err).-name-, err), verbose) if resultexpectedResult: message (record, 'Test %d passed.' % tc, verbose) return l else: message (record, 'Test %d failed- expecting %d, got %d instead.' % (tc, expectedRe suit, result), verbose) return 0 def runTest (modulec220a3', path - '.', recordprint): global evaluate try: evaluateloadFunction (module, path, valuate') except: record File loading error - testing not executed. ') return 0 passed-[] failed - [] n=len (tests) for (testId, expectedResult, inputString) in tests: score - runSingleTest (testId, expectedResult, inputString, False, record) if score 1: passed.append (testId) else: failed.append (testId) score- len (passed) testScore = 15.0 * score / n record ('Passed: %s - total %d.' % (passed, len (passed))) record ('Failed: %s - total %d.' % (failed, len (failed))) record (' %d of %d test cases passed. Score %.2f of 15.00 ' % (score, n, testScore)) return testScore def runTestCase (i): for (tc, expectedResult, expression) in tests: runSingleTest (tc, expectedResult, expression, True, print) f name- main runTest ) 8+5 2-9-0+ 0-0+5-7 60.-1620, 6-4-5*247-8*4*745-78 943*6-9-7 245--s8-5 from csc220a3TestData import tests def loadFunction (module, path, function): import sys sys.path.insert (0, path) try: return getattr import (module), function) finally: sys.path.pop (0) def message (record, msg, verbose): if verbose: record (msg) def runsingleTest (tc, expectedRe sult, expression, verbose= False, record = print): try: result - evaluate (expression) except Exception as err: message (record, return 0 'Test %d failed- %s caught- %s' % (tc, type (err).-name-, err), verbose) if resultexpectedResult: message (record, 'Test %d passed.' % tc, verbose) return l else: message (record, 'Test %d failed- expecting %d, got %d instead.' % (tc, expectedRe suit, result), verbose) return 0 def runTest (modulec220a3', path - '.', recordprint): global evaluate try: evaluateloadFunction (module, path, valuate') except: record File loading error - testing not executed. ') return 0 passed-[] failed - [] n=len (tests) for (testId, expectedResult, inputString) in tests: score - runSingleTest (testId, expectedResult, inputString, False, record) if score 1: passed.append (testId) else: failed.append (testId) score- len (passed) testScore = 15.0 * score / n record ('Passed: %s - total %d.' % (passed, len (passed))) record ('Failed: %s - total %d.' % (failed, len (failed))) record (' %d of %d test cases passed. Score %.2f of 15.00 ' % (score, n, testScore)) return testScore def runTestCase (i): for (tc, expectedResult, expression) in tests: runSingleTest (tc, expectedResult, expression, True, print) f name- main runTest ) 8+5 2-9-0+ 0-0+5-7 60.-1620, 6-4-5*247-8*4*745-78 943*6-9-7 245--s8-5

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Mannino Michael

5th Edition

0983332401, 978-0983332404

More Books

Students also viewed these Databases questions