Question

In: Computer Science

The function print_mean() that you wrote in the previous lesson calculates an average value and prints...

The function print_mean() that you wrote in the previous lesson calculates an average value and prints it on the screen. Change this function so that instead of printing the average it returns the average.In order to calculate the sum, you won't need to form a loop; call the function column_sum() instead.

# columns are [0]title [1]year [2]rating [3]length(min) [4]genre [5]budget($mil) [6]box_office_gross($mil)
oscar_data = [
["The Shape of Water", 2017, 6.914, 123, ['sci-fi', 'drama'], 19.4, 195.243464],
["Moonlight", 2016, 6.151, 110, ['drama'], 1.5, 65.046687],
["Spotlight", 2015, 7.489, 129, ['drama', 'crime', 'history'], 20.0, 88.346473],
["Birdman", 2014, 7.604, 119, ['drama', 'comedy'], 18.0, 103.215094],
["12 Years a Slave", 2013, 7.71, 133, ['drama', 'biography', 'history'], 20.0, 178.371993],
["Argo", 2012, 7.517, 120, ['thriller', 'drama', 'biography'], 44.5, 232.324128],
["The Artist", 2011, 7.942, 96, ['drama', 'melodrama', 'comedy'], 15.0, 133.432856],
["The King\'s Speech", 2010, 7.977, 118, ['drama', 'biography', 'history'], 15.0, 414.211549],
["The Hurt Locker", 2008, 7.298, 126, ['thriller', 'drama', 'war', 'history'], 15.0, 49.230772],
["Slumdog Millionaire", 2008, 7.724, 120, ['drama', 'melodrama'], 15.0, 377.910544],
["No Country for Old Men", 2007, 7.726, 122, ['thriller', 'drama', 'crime'], 25.0, 171.627166],
["The Departed", 2006, 8.456, 151, ['thriller', 'drama', 'crime'], 90.0, 289.847354],
["Crash", 2004, 7.896, 108, ['thriller', 'drama', 'crime'], 6.5, 98.410061],
["Million Dollar Baby", 2004, 8.075, 132, ['drama', 'sport'], 30.0, 216.763646],
["The Lord of the Rings: Return of the King", 2003, 8.617, 201, ['fantasy', 'drama', 'adventure'], 94.0, 1119.110941],
["Chicago", 2002, 7.669, 113, ['musical', 'comedy', 'crime'], 45.0, 306.776732],
['A Beautiful Mind', 2001, 8.557, 135, ['drama', 'biography', 'melodrama'], 58.0, 313.542341],
["Gladiator", 2000, 8.585, 155, ['action', 'drama', 'adventure'], 103.0, 457.640427],
["American Beauty", 1999, 7.965, 122, ['drama'], 15.0, 356.296601],
["Shakespeare in Love", 1998, 7.452, 123, ['drama', 'melodrama', 'comedy', 'history'], 25.0, 289.317794],
["Titanic", 1997, 8.369, 194, ['drama', 'melodrama'], 200.0, 2185.372302],
["The English Patient", 1996, 7.849, 155, ['drama', 'melodrama', 'war'], 27.0, 231.976425],
["Braveheart", 1995, 8.283, 178, ['drama', 'war', 'biography', 'history'], 72.0, 210.409945],
["Forrest Gump", 1994, 8.915, 142, ['drama', 'melodrama'], 55.0, 677.386686],
["Schindler\'s List", 1993, 8.819, 195, ['drama', 'biography', 'history'], 22.0, 321.265768],
["Unforgiven", 1992, 7.858, 131, ['drama', 'western'], 14.4, 159.157447],
["Silence of the Lambs", 1990, 8.335, 114, ['thriller', 'crime', 'mystery', 'drama', 'horror'], 19.0, 272.742922],
["Dances with Wolves", 1990, 8.112, 181, ['drama', 'adventure', 'western'], 22.0, 424.208848],
["Driving Miss Daisy", 1989, 7.645, 99, ['drama'], 7.5, 145.793296],
["Rain Man", 1988, 8.25, 133, ['drama'], 25.0, 354.825435],
]


def column_sum(data, column):
result = 0
for row in data:
result += row[column]
return result

def column_mean(data, column):
# < write code here >
  

mean_score = column_mean(oscar_data, 2)
print('Average rating: {:.2f}'.format(mean_score))

mean_length = column_mean(oscar_data, 3)
print('Average length: {:.2f} min.'.format(mean_length))

mean_budget = column_mean(oscar_data, 5)
print('Average budget: ${:.2f} mil.'.format(mean_budget))

mean_gross = column_mean(oscar_data, 6)
print('Average revenue: ${:.2f} mil.'.format(mean_gross))

Solutions

Expert Solution

  • Code snippets are provided below the typed code.
  • Please refer to snippet of code to understand the indentation of code.
  • Output snippet is also attached at the end.

Required Function:

def column_mean(data, column):
    # returning average using column_sum() and len() functions
    return column_sum(data, column)/len(data)

Snippet:

Complete Code:

oscar_data = [
["The Shape of Water", 2017, 6.914, 123, ['sci-fi', 'drama'], 19.4, 195.243464],
["Moonlight", 2016, 6.151, 110, ['drama'], 1.5, 65.046687],
["Spotlight", 2015, 7.489, 129, ['drama', 'crime', 'history'], 20.0, 88.346473],
["Birdman", 2014, 7.604, 119, ['drama', 'comedy'], 18.0, 103.215094],
["12 Years a Slave", 2013, 7.71, 133, ['drama', 'biography', 'history'], 20.0, 178.371993],
["Argo", 2012, 7.517, 120, ['thriller', 'drama', 'biography'], 44.5, 232.324128],
["The Artist", 2011, 7.942, 96, ['drama', 'melodrama', 'comedy'], 15.0, 133.432856],
["The King\'s Speech", 2010, 7.977, 118, ['drama', 'biography', 'history'], 15.0, 414.211549],
["The Hurt Locker", 2008, 7.298, 126, ['thriller', 'drama', 'war', 'history'], 15.0, 49.230772],
["Slumdog Millionaire", 2008, 7.724, 120, ['drama', 'melodrama'], 15.0, 377.910544],
["No Country for Old Men", 2007, 7.726, 122, ['thriller', 'drama', 'crime'], 25.0, 171.627166],
["The Departed", 2006, 8.456, 151, ['thriller', 'drama', 'crime'], 90.0, 289.847354],
["Crash", 2004, 7.896, 108, ['thriller', 'drama', 'crime'], 6.5, 98.410061],
["Million Dollar Baby", 2004, 8.075, 132, ['drama', 'sport'], 30.0, 216.763646],
["The Lord of the Rings: Return of the King", 2003, 8.617, 201, ['fantasy', 'drama', 'adventure'], 94.0, 1119.110941],
["Chicago", 2002, 7.669, 113, ['musical', 'comedy', 'crime'], 45.0, 306.776732],
['A Beautiful Mind', 2001, 8.557, 135, ['drama', 'biography', 'melodrama'], 58.0, 313.542341],
["Gladiator", 2000, 8.585, 155, ['action', 'drama', 'adventure'], 103.0, 457.640427],
["American Beauty", 1999, 7.965, 122, ['drama'], 15.0, 356.296601],
["Shakespeare in Love", 1998, 7.452, 123, ['drama', 'melodrama', 'comedy', 'history'], 25.0, 289.317794],
["Titanic", 1997, 8.369, 194, ['drama', 'melodrama'], 200.0, 2185.372302],
["The English Patient", 1996, 7.849, 155, ['drama', 'melodrama', 'war'], 27.0, 231.976425],
["Braveheart", 1995, 8.283, 178, ['drama', 'war', 'biography', 'history'], 72.0, 210.409945],
["Forrest Gump", 1994, 8.915, 142, ['drama', 'melodrama'], 55.0, 677.386686],
["Schindler\'s List", 1993, 8.819, 195, ['drama', 'biography', 'history'], 22.0, 321.265768],
["Unforgiven", 1992, 7.858, 131, ['drama', 'western'], 14.4, 159.157447],
["Silence of the Lambs", 1990, 8.335, 114, ['thriller', 'crime', 'mystery', 'drama', 'horror'], 19.0, 272.742922],
["Dances with Wolves", 1990, 8.112, 181, ['drama', 'adventure', 'western'], 22.0, 424.208848],
["Driving Miss Daisy", 1989, 7.645, 99, ['drama'], 7.5, 145.793296],
["Rain Man", 1988, 8.25, 133, ['drama'], 25.0, 354.825435],
]


def column_sum(data, column):
    result = 0
    for row in data:
        result += row[column]
    return result


def column_mean(data, column):
    # returning average using column_sum() and len() functions
    return column_sum(data, column)/len(data)


# printing output
mean_score = column_mean(oscar_data, 2)
print('Average rating: {:.2f}'.format(mean_score))

mean_length = column_mean(oscar_data, 3)
print('Average length: {:.2f} min.'.format(mean_length))

mean_budget = column_mean(oscar_data, 5)
print('Average budget: ${:.2f} mil.'.format(mean_budget))

mean_gross = column_mean(oscar_data, 6)
print('Average revenue: ${:.2f} mil.'.format(mean_gross))

Code Snippet:

Output Snippet:

I hope you got the provided solution.

Thank You.


Related Solutions

This function takes in a string as a parameter and prints the average number of characters...
This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision (see test cases below). Assume a sentence always ends with a period (.) or when the string ends. Assume there is always a blank space character (" ") between each word. Do not count the blank spaces between words or the periods...
Apply the augmented Philips curve policy lesson to the suggestion that the SARB prints the money....
Apply the augmented Philips curve policy lesson to the suggestion that the SARB prints the money. Make use of graphs and explain what the impact might be on output and inflation.
Please solve the following problem for MATLAB Write a user-defined function that calculates the average and...
Please solve the following problem for MATLAB Write a user-defined function that calculates the average and the standard deviation of a list of numbers. Use the function to calculate the average and the standard deviation of the following list of grades : 80 75 91 60 79 89 65 80 95 50 81
In Python write a program that calculates and prints out bills of the city water company....
In Python write a program that calculates and prints out bills of the city water company. The water rates vary, depending on whether the bill is for home use, commercial use, or industrial use. A code of r means residential use, a code of c means commercial use, and a code of i means industrial use. Any other code should be treated as an error. The water rates are computed as follows:Three types of customers and their billing rates: Code...
Create a bash script that takes numbers as parameters, calculates sum and prints the result. If...
Create a bash script that takes numbers as parameters, calculates sum and prints the result. If no parameters passed, prompt a user (only one time) to enter numbers to sum and print the result.
Write a pyhton program that reads a stream of bits, e.g. 11001, and calculates and prints...
Write a pyhton program that reads a stream of bits, e.g. 11001, and calculates and prints the decimal value of the binary number represented by the entered bits, i.e. 25 in this case.
Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the...
Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the result. If no parameters passed, prompt a user (only one time) to enter numbers to sum and print the result. Submit your program as LastName_FirstName.sh file.
Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation:...
Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation: arctan⁡(x)=x-x^3/3+x^5/5-x^7/7+⋯ The function should accept 3 parameters: value of x, number of significant figures accuracy i.e. n, and the maximum number of iterations. In the function, use ε_s=(0.5×〖10〗^(2-n ) )% in order to continue until the ε_a falls below this criteria. The function should return 3 values: the approximate value of arctan(x) at the end of the program, final ε_a and the number of...
The average value of a function f over the interval [-2,3] is -6, and the average...
The average value of a function f over the interval [-2,3] is -6, and the average value of f over the interval [3,5] is 20. What is the average value of f over the interval [-2,5]?
Make a function that calculates the summation of even numbers in the range. The function sum...
Make a function that calculates the summation of even numbers in the range. The function sum takes the two integer parameters and they are used as the range. The function uses default parameters for the range. When we call this function with one argument, it will be used as a starting point and the end of the range will be 100. Also, the function is called without any argument, the default range (0,100) will be used. We will use default...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT