Question
I'm still receiving an error in my python code The call clamp([ -1 , 1 , 3 , 5 ], 0 , 4 ) returns
I'm still receiving an error in my python code "The call clamp([-1, 1, 3, 5], 0, 4) returns None, not [0, 1, 3, 4]."
def clamp(alist,min,max):
"""
Returns a copy of alist where every element is between min and max.
Any number in the list less than min is replaced with min.Any number
in the tuple greater than max is replaced with max. Any number between
min and max is left unchanged.
Examples:
clamp([-1, 1, 3, 5],0,4) returns [0,1,3,4]
clamp([-1, 1, 3, 5],-2,8) returns [-1,1,3,-5]
clamp([-1, 1, 3, 5],-2,-1) returns [-1,-1,-1,-1]
clamp([],0,4) returns []
Parameter alist: the list to copy
Precondition: alist is a list of numbers (float or int)
Parameter min: the minimum value for the list
Precondition: min <= max is a number
Parameter max: the maximum value for the list
Precondition: max >= min is a number
"""
for x in range(len(alist)):
if alist[x] > max:
alist[x] = max
if alist[x] < min:
alist[x] = min
These are the tests:
def test_clamp():
"""
Test procedure for function clamp().
"""
print('Testing clamp()')
alist = [-1, 1, 3, 5]
result = funcs.clamp(alist,0,4)
introcs.assert_equals([ 0, 1, 3, 4],result)
introcs.assert_equals([-1, 1, 3, 5],alist)
result = funcs.clamp(alist,-2,8)
introcs.assert_equals([-1, 1, 3, 5],result)
introcs.assert_equals([-1, 1, 3, 5],alist)
result = funcs.clamp(alist,-2,-1)
introcs.assert_equals([-1,-1,-1,-1],result)
introcs.assert_equals([-1, 1, 3, 5],alist)
result = funcs.clamp(alist,1,1)
introcs.assert_equals([ 1, 1, 1, 1],result)
introcs.assert_equals([-1, 1, 3, 5],alist)
alist = [-1, 4, -1, 4, 2]
result = funcs.clamp(alist,0,4)
introcs.assert_equals([ 0, 4, 0, 4, 2],result)
introcs.assert_equals([-1, 4,-1, 4, 2],alist)
alist = [ 1, 3]
result = funcs.clamp(alist,0,4)
introcs.assert_equals([ 1, 3],result)
introcs.assert_equals([ 1, 3],alist)
alist = []
result = funcs.clamp(alist,0,4)
introcs.assert_equals([],result)
introcs.assert_equals([],alist)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started