Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

=+Differentiate between bong-run and short-run Phillips curves.

Answered: 1 week ago

Question

What is population?

Answered: 1 week ago

Question

Explain the study in demography?

Answered: 1 week ago

Question

Define social demography?

Answered: 1 week ago

Question

What is migration?

Answered: 1 week ago