Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

L 4 - template.py: # Turn diagnostics on / off by setting VERBOSE True / False . VERBOSE = True def L 4 ( inString

L4-template.py:
# Turn diagnostics on/off by setting VERBOSE True/False.
VERBOSE = True
def L4(inString):
# inString is a white-space delimited list of string representations
# of decimal ints. For example, '7-65-28' is a valid inString.
#
# For lab #4, add code to L4() that parses inString and computes the
# count of positive ints equal to 2 mod 3, as well as storing the
# last negative int.
#
# If inString contains a white-space delimited substring of symbols that
# is not a valid decimal int, L4(inString) returns 'no';
# else if inString does not contain any negative ints, L4(inString)
# returns 'no';
# else if the count of positive ints equal to 2 mod 3 does not match
# the absolute value of the last negative int, L4(inString) returns 'no';
# else L4(inString) returns 'yes'.
#
# For example, if inString ='7-65-28', L4(inString) would
# return 'yes' because inString contains two positive ints equal to 2
# mod 3(5 and 8), and the absolute value of the last negative int
# in inString is 2; if inString ='-11-23-58', L4(inString)
# returns 'no' because there is only one positive int equal to 2 mod 3,
# not 5; if inString ='112358', L4(inString) returns 'no'
# because inString does not contain any negative int; and if inString =
# '7-65-2 x0F 8', L4(inString) returns 'no' because 'x0F' is not
# a valid representation of a decimal int.
#
# Do not insert any print statements into this function. If you want to
# show the result, print the return value.
return 'yes'
if __name__=='__main__':
def test_case(F,string,expected,num,comment=''):
err ='**'
result = F(string)
func_name = str(F).split()[1]
func_call = f'''{func_name}("{string}")'''
if result == expected: err =''
e = expected
print (f'{err}test #{num}{func_call}: expected "{e}", received
"{result}"')
print (f'test #{num} Explanation: {comment}
')
return num +1
num =1
s ='7-65-28'
exp ='2 positive ints =23,-2 is the last negative int'
num = test_case(L4,s,'yes',num,exp)
s ='-11-23-58'
exp = 'Only one positive int equal to 2 mod 3, not 5'
num = test_case(L4,s,'no',num,exp)
s ='112358'
exp ='no negative ints'
num = test_case(L4,s,'no',num,exp)
s ='7-65-2 x0F 8'
exp ='x0F is not a valid representation of a decimal int'
num = test_case(L4,s,'no',num,exp)
Modify the attached program L4-template.py to create a SISO (String In, String Out) Python decision function with the following properties:
The expected input parameter inString is a string encoding integers delimited by white space. For example, '7-65-28' is a valid inString.
The function L4() should parse inString and compute the count of positive ints equal to 2 mod 3, as well as storing the last negative int. Note that the Python mod operator is '%',and thus these expressions all evaluate to True: 7%3==1; 5%3==8%3==2.
If inString contains a white-space delimited substring of symbols that is not a valid decimal int, L4(inString) returns 'no';
else if inString does not contain any negative ints, L4(inString) returns 'no';
else if the count of positive ints equal to 2 mod 3 does not match the absolute value of the last negative int, L4(inString) returns 'no';
else L4(inString) returns 'yes'.
L4.py comes with a test harness initialized with 4 test cases. Executing L4 prints a hand-coded explanation for the expected results, and flags unexpected results with '**'. The template just returns 'yes' for all inputs, resulting in this output:
test #1 L4("7-65-28"): expected "yes", received "yes"
test #1 Explanation: 2 positive ints =23,-2 is the last negative int
** test #2 L4("-11-23-58"): expected "no", received "yes"
test #2 Explanation: Only one positive int equal to 2 mod 3, not 5
** test #3 L4("112358"): expected "no", received "yes"
test #3 Explanation: no negative ints
** test #4 L4("7-65-2 x0F 8"): expected "no", received "yes"
test #4 Explanation: x0F is not a valid representation of a decimal int
A correct implementation would return 'no' for test cases 2,3, and 4, while still returning 'yes' for test case 1.

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

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions