Question

Write four functions: (IN PYTHON 3) 1) bound(l) - given a list of integers l, compute...

Write four functions: (IN PYTHON 3)

1) bound(l) - given a list of integers l, compute a tuple containing the minimum and maximum values (in that order)

2) span(l) - given a list of integers l, compute the difference between the minimum and maximum function.

3) mean(l) - given a list of integers l, compute the average. The return value will be a float.

4) stats(f, l) - given a function f and a list l, apply the function f to the list and return the result.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  1. bound(l) function

This receives a list l as parameter, and finds the minimum and maximum of the list and return the tuple of it.

This can be written as:

# bound(l) - given a list of integers l, compute a tuple containing the minimum and maximum values (in that order)

def bound(l):

    # get minimum of the list

    minimum = min(l)

    # get maximum of the list

    maximum = max(l)

    # return the tuple containing the minimum and maximum values

    return (minimum, maximum)

Below is the sample output:

  1. span(l) function

This function receives a list and find the minimum and maximum of the list like the above function. Then it calculates the difference between maximum and minimum and return that value.

This could be coded as:

# span(l) - given a list of integers l, compute the difference between the minimum and maximum function.

def span(l):

    # get minimum of the list

    minimum = min(l)

    # get maximum of the list

    maximum = max(l)

    # calculate the difference between maximum and minimum

    diff = maximum - minimum

    # return the difference

    return diff

Below is the sample output:

  1. mean(l) function

This function receives a list and calculate the mean of numbers of list and returns it. To calculate the mean, we iterate through the list and calculate the total of all value, then divide that value by the length of list, which will give the mean and then it returns it.

This could be coded as:

# mean(l) - given a list of integers l, compute the average. The return value will be a float

def mean(l):

    # calculate the total of items

    total = 0

    for item in l:

        total += item

    # get size of list

   size = len(l)

    # calculate the average by dividing the total by size of list

    average = float(total/size)

    # return the average

    return average

Below is the sample output:

  1. stats(f, l) function

This function receives another function and a list as a parameter. It calls the given function with list as its parameter. It returns the return value returned by the function f. It basically can be used to call above functions as its parameter.

This could be coded as:

# given a function f and a list l, apply the function f to the list and return the result

def stats(f, l):

    # get value out of function

    value = f(l)

    # return the value which the return value of the respective function

    return value

Below is the sample output:

All the required functions have been coded and explained above. Let me know if you have any queries.

Thanks!

Add a comment
Know the answer?
Add Answer to:
Write four functions: (IN PYTHON 3) 1) bound(l) - given a list of integers l, compute...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Test CASE: DATA1 = [1, 2, 3, 4, 5, 6] DATA2 = [i for i in range(100) if i % 3 == 0] assert(stats(span, DATA1) == 5) ass...

    Test CASE: DATA1 = [1, 2, 3, 4, 5, 6] DATA2 = [i for i in range(100) if i % 3 == 0] assert(stats(span, DATA1) == 5) assert(stats(mean, DATA1) == 3.5) assert(stats(bound, DATA2) == [0, 99]) Write four functions: 1) bound() - given a list of integers 1, compute a tuple containing the minimum and maximum values (in that order) 2) span() given a list of integers 1, compute the difference between the minimum and maximum function 3) mean() given...

  • write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple

    1 write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple. Hint (can be done in one pass, you are not allowed to use built-on min and max functions.)max, min = find_max_min(my_list):2 write a Python function that takes in a list of integers (elements), and an integer number (num). The functions should count and return number of integers in elements greater than, less than, and equal to...

  • Python recursive function: Given an ordered list L. A permutation of L is a rearrangement of...

    Python recursive function: Given an ordered list L. A permutation of L is a rearrangement of its elements in some order. For example (1,3, 2) and (3, 2, 1) are two different permutations of L=(1,2,3). Implement the following function: def permutations (lst, low, high) The function is given a list 1st of integers, and two indices: low and high (lows high), which indicate the range of indices that need to be considered The function should return a list containing all...

  • Use the FDR to design and write a function, maxValTimes, that takes a list of integers...

    Use the FDR to design and write a function, maxValTimes, that takes a list of integers and returns a set containing the value(s) that occur the same number of times as the maximum value in the list. If the list is empty, return an empty set.   For example if the list was [2,1,1,2,3,3,1] the function would return {2,3} as the maximum value is 3 which occurs twice, and 2 also occurs twice (but 1 occurs 3 times). For full marks,...

  • In PYTHON: Write a function that receives a list of integers and returns the number of...

    In PYTHON: Write a function that receives a list of integers and returns the number of integers that are larger than the element immediately before them. For example, given [1,2,3,-5,0,-5,-10] the function should return 3 (since 1<2, 2<3, and -5<0).

  • 2. Write a program with three functions that will be called from the main function The functions will calculate the...

    2. Write a program with three functions that will be called from the main function The functions will calculate the volume, area and perimeter of a cuboid. You should able to pass three parameters such as length, width and height, and a function will return area, another will return volume and final one will return perimeter. Define all your variables in floating point numbers 3. Modify the problem#2 to calculate the volume, area and perimeter in a sing le function...

  • Write function all_coprime_pairs( ) which takes as input a list, L, with positive unique integers (i.e....

    Write function all_coprime_pairs( ) which takes as input a list, L, with positive unique integers (i.e. no duplicated integers), and should return: 1. List (of tuples) containing all pairs of numbers in L that are coprime. 2. Empty list, If no pair of numbers in L is coprime. Your solution must include a function call to the function coprime designed in the previous question. The order of the tuples or the order of the two numbers within the tuples is...

  • ***NEED SOLUTION IN PYTHON *** 1. Create a main function which only gets executed when the...

    ***NEED SOLUTION IN PYTHON *** 1. Create a main function which only gets executed when the file is directly executed Hint: def main(): pass if __name__ == '__main__': main() 2. Your programs will use a file called scores.txt to store a bunch of user-provided numbers. These can be int or float but their values must be between 0 and 100. Inside the main function, display the current list of numbers to the user and ask if they want to update...

  • Assume that L is a list of Boolean values, True and False. Write a function longestFalse(L)...

    Assume that L is a list of Boolean values, True and False. Write a function longestFalse(L) which returns a tuple (start, end) representing the start and end indices of the longest run of False values in L. If there is a tie, then return the first such run. For example, if L is False False True False False False False True True False False 0 1 2 3 4 5 6 7 8 9 10 then the function would return...

  • Python 1. Open up IDLE and in a multiline comment write “Functions and Sets II,” your...

    Python 1. Open up IDLE and in a multiline comment write “Functions and Sets II,” your name, and the date. 2. Use Python to write a function f(n) that finds the sum of the first n positive integers. Do not use the sum() function or lists. Write the function from scratch. Then use a for loop to print the sums for n = 10, 50, 1000. 3. Finding the Image of a function Suppose that we have a function f...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT