Menu
support@nursinghomework.org
+1 714-215-3097

(Solved) : Using Python Trying Write Code Tested Evaluate Function Assistance Appreciated Assignment Q35535004

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

In this assignment, we will implement a function to evaluate anarithmetic expression. We will use the algorithm outlined in thePowerPoint slides in Chapter 6. You will use two stacks to keeptrack things. The function is named ‘evaluate’, it takes a stringas input parameter, and returns an integer. The input stringrepresents an arithmetic expression, with each character in thestring being a token, while the returned value should be the valueof 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 thesame folder. The first two files are the tester program and thetesting data file, respectively. The third file is the ArrayStackclass from the textbook. Do not modify these three files. The lastfile is the file you will work on. Note that you cannot rename thisfile and you cannot change the name of the function (otherwise thetester 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 thefunction 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 yourimplementation. It will run a total of 100 test cases. If you makeno change to the function above, it should produce the followingoutput:

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 arithmeticexpression, with all numerical values being one-digit integers(1-9). You do not have to check for the correctness of the inputexpression. 2. The only operators that will appear in theexpression are “+”, “-”, and “*”, each of these operators arehaving the usual meaning as they are usually used in programminglanguages. 3. Your program will be tested with similar butdifferent data. The testing results will earn you up to 15 points(out of 20 points total). 4. The remaining 5 points will be awardedbased on the structure, readability, and documentation of yourimplementation. 5. You implementation should be compatible toPython 3.x. 6. There can only be one import statement in yourprogram; and that statement must be “from ArrayStack importArrayStack”. (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 thatsingle testcase. It will produce a more verbose output to help youdebug your program.

>>> runTestCase (50) Test 50 failed – expecting 65, got -1instead.

from csc220a3TestData import tests def loadFunction (module, path, function): import sys sys.path.insert (0, path) try: retur8+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 (‘n%d of %d test cases passed. Score %.2f of 15.00n’ % (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 Show transcribed image text 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 (‘n%d of %d test cases passed. Score %.2f of 15.00n’ % (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

Leave a Reply

Your email address will not be published. Required fields are marked *