Question: Read the specification of the function clamp in the module funcs. Implement this function according to its specification. Note that this function modifies the original

Read the specification of the function clamp in the module funcs. Implement this function according to its specification. Note that this function modifies the original list and does not return anything. Look at the test cases in tests.py if you are unsure.

 

Remember that you should never modify the loop-sequence in a for-loop. So your implementation should either loop over something else (positions) or use a while-loop. When you have implemented the function, test your answer with the test script before moving on to the next function.

 

code:

def clamp(alist,min,max):

    """

    Modifies alist so that 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:

        If a = [-1, 1, 3, 5], clamp(a,0,4) changes a to [0,1,3,4]

        If a = [-1, 1, 3, 5], clamp(a,-2,8) changes a to [-1,1,3,-5]

        If a = [-1, 1, 3, 5], clamp(a,-2,-1) changes a to [-1,-1,-1,-1]

        If a = [], clamp(a,0,4) returns []

   

    Parameter alist: the list to modify

    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

    """

    pass

 

12. Implement removeall

Read the specification of the function removeall in the module funcs. Implement this function according to its specification. Again, this function modifies the original list and does not return anything. Look at the test cases in tests.py if you are unsure.

 

Remember that you should never modify the loop-sequence in a for-loop. So your implementation should either loop over something else (positions) or use a while-loop. When you have implemented the function, test your answer with the test script. You should now pass all tests.

 

code:

def removeall(alist,n):

    """

    Removes all instances of n from alist

   

    Examples:

        If a = [1,2,2,3,1], removeall(a,1) changes a to [2,2,3]

        If a = [1,2,2,3,1], removeall(a,2) changes a to [1,3,1]

        If a = [1,2,2,3,1], removeall(a,4) changes a to [1,2,2,3,1]

        If a = [1,1,1], removeall(a,1) changes a to []

        If a = [], removeall(a,1) changes a to []

   

    Parameter alist: the list to modify

    Precondition: alist is a list of numbers (float or int)

   

    Parameter n: the number to remove

    Precondition: n is a number

    """

    pass


Step by Step Solution

3.39 Rating (165 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

1 clamp Function The clamp function modifies a given list alist by ensuring that every element in the list is between a specified minimum value min an... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!