Add suitable code to the code below and then run it Run the provided code first,
ID: 3915074 • Letter: A
Question
Add suitable code to the code below and then run it
Run the provided code first, to load the required function and data into memory, then do the calculation in the interactive Python shell.
Feel free to choose the approach you prefer.
Figure 1 charts data about the highest points, i.e. the points with the highest altitudes above sea level, in each of 56 European countries (Wikipedia, 2017).
Give the median, correct to the nearest whole number. Also copy and paste the Python code you used and briefly (in one or two sentences) explain how you ran it.
For this list of data the mean is 2185, which is smaller than the median. From looking at Figure 1 briefly suggest a possible reason.
-----------------------------------------------------------------------------------------
INITIAL CODE
-----------------------------------------------------------------------------------------
Heighest point of 56 European countries, in metres.
08/10/2017
"""
from data_stats import median
heights = [2764,2942,3798,4466,345,694,2386,2925,1831,1952,1603,3694,171,318,1324,4810,5201,2962,2919,1014,2110,4810,2658,2656,312,2599,294,560,253,430,163,2534,887,321,2469,2499,2351,1993,1041,2764,2544,5642,749,2169,2655,2864,3718,3479,2104,4634,347,5137,1031,2061,1344,75]
""" You can use one of two approaches
-- add suitable code below and then run this file
-- run this file first then do the calculation in the Python
interactive shell.
"""
----------------------------------------------------
PROVIDED CODE
----------------------------------------------------
import math
def median(alist):
""" Calculates the median of a list of numbers.
The list must not be empty.
"""
number_of_values = len(alist)
sorted_list = sorted(alist)
# Two cases, depending on whether the number of values is odd or even.
quotient = number_of_values // 2
remainder = number_of_values % 2
if (remainder == 1):
result = sorted_list[quotient]
else:
result = (sorted_list[quotient - 1] + sorted_list[quotient]) / 2
return result
def test_median():
assert median([2]) == 2
assert median([4, 3]) == 3.5
assert median([3, 1, 8, 4, 7, 6, 4, 2, 5, 9]) == 4.5
assert median([7, 2, 6, 2, 5, 3, 1, 0, 8, 6, 6, 4, 9]) == 5
# Unit test
test_median()
def mean(list):
"""Return mean of list"""
sum = 0
count = 0
for item in list:
sum = sum + item
count = count + 1
return sum / count
def test_mean():
list = [1, 2, 3, 4, 5]
assert(mean(list) == 3)
# Unit test
test_mean()
def corr_coef(list_x, list_y):
""" Return correlation between values in list_x and list_y.
Lists must be of equal length.
"""
x_bar = mean(list_x)
y_bar = mean(list_y)
sxy = 0
sxx = 0
syy = 0
for index in range(len(list_x)):
x = list_x[index]
y = list_y[index]
sxy = sxy + (x - x_bar) * (y - y_bar)
sxx = sxx + (x - x_bar) * (x - x_bar)
syy = syy + (y - y_bar) * (y - y_bar)
return sxy / math.sqrt(sxx * syy)
def test_corr_coef():
# Data from M140 Unit 9 Example 5
list1 = [78.9, 75.8, 77.3, 74.2, 78.1, 72.8, 77.6, 77.9]
list2 = [56.7, 53.1, 56.1, 55.9, 54.1, 48.6, 59.4, 54.0]
assert round(corr_coef(list1, list2), 2) == 0.64
# Unit test
test_corr_coef()
Explanation / Answer
here is your code and instruction : ------------>>>>>>>>>>>>
import math
def median(alist):
""" Calculates the median of a list of numbers.
The list must not be empty.
"""
number_of_values = len(alist)
sorted_list = sorted(alist)
# Two cases, depending on whether the number of values is odd or even.
quotient = number_of_values // 2
remainder = number_of_values % 2
if (remainder == 1):
result = sorted_list[quotient]
else:
result = (sorted_list[quotient - 1] + sorted_list[quotient]) / 2
return result
def test_median():
assert median([2]) == 2
assert median([4, 3]) == 3.5
assert median([3, 1, 8, 4, 7, 6, 4, 2, 5, 9]) == 4.5
assert median([7, 2, 6, 2, 5, 3, 1, 0, 8, 6, 6, 4, 9]) == 5
# Unit test
test_median()
def mean(list):
"""Return mean of list"""
sum = 0
count = 0
for item in list:
sum = sum + item
count = count + 1
return sum / count
def test_mean():
list = [1, 2, 3, 4, 5]
assert(mean(list) == 3)
# Unit test
test_mean()
def corr_coef(list_x, list_y):
""" Return correlation between values in list_x and list_y.
Lists must be of equal length.
"""
x_bar = mean(list_x)
y_bar = mean(list_y)
sxy = 0
sxx = 0
syy = 0
for index in range(len(list_x)):
x = list_x[index]
y = list_y[index]
sxy = sxy + (x - x_bar) * (y - y_bar)
sxx = sxx + (x - x_bar) * (x - x_bar)
syy = syy + (y - y_bar) * (y - y_bar)
return sxy / math.sqrt(sxx * syy)
def test_corr_coef():
# Data from M140 Unit 9 Example 5
list1 = [78.9, 75.8, 77.3, 74.2, 78.1, 72.8, 77.6, 77.9]
list2 = [56.7, 53.1, 56.1, 55.9, 54.1, 48.6, 59.4, 54.0]
assert round(corr_coef(list1, list2), 2) == 0.64
# Unit test
test_corr_coef()
#to run the simulation of your question do below thing
#first run above code from your editor menu
#then go to the shell and write below given code
#for median
#below code is like a list variable declaration
heights = [2764,2942,3798,4466,345,694,2386,2925,1831,1952,1603,3694,171,318,1324,4810,5201,2962,2919,1014,2110,4810,2658,2656,312,2599,294,560,253,430,163,2534,887,321,2469,2499,2351,1993,1041,2764,2544,5642,749,2169,2655,2864,3718,3479,2104,4634,347,5137,1031,2061,1344,75]
print(median(heights))
#press enter it will give you the median
#for mean write below code
print(mean(heights))
#and press enter it will give the mean
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.