PYTHON
Given three unsorted lists, a name list (e.g. nameLst = [“Betty”, “Andrew”, “Zach”, “Cathy”, …]), an Exam1 score list (e.g. score1 = [50, 45, 48, 48, …]), and an Exam 2 score list (e.g. score2=[35, 48, 50, 37, …])
Write the following functions:
#the function will return a list of tuples as in the following form
#[(“Betty”, (50,35)), (“Andrew”, (45,48)), (“Zach”, (48,50)), (“Cathy”, (48,37)), …]
#takes a class list in the form of [(“Betty”, (50,35)), (“Andrew”, (45,48)), …]
#returns another class list in the form of [(“Betty”, 42.5), (“Andrew”, 46.5), …]
#takes a class list in the form of [(“Betty”, 42.5), (“Andrew”, 46.5), …]
#returns another class list in the form of [(“Andrew”, 46.5), (“Betty”, 42.5), …]
#i.e. sort by name in ascending order
#note: you may call predefined sort() or sorted() in function body
#takes a class list in the form of [(“Betty”, 42.5), (“Andrew”, 46.5), …]
#returns another class list in the form of [(“Zach”, 48.0), (“Andrew”, 46.5), …]
#i.e. sort by average score in descending order
#takes a class list in the form of [(“Betty”, (50,35)), (“Andrew”, (45,48)), …]
#returns exam1MeanScore and exam2MeanScore for the class
#similar to (5) but returns the exam1MedianScore and exam2MedianScore
#similar to (5) but returns the standard deviation for Exam1 and Exam2 respectively
Tasks in main function:
score1 = [50, 45, 48, 48, 46, 49, 35, 38, 42, 50, 44, 37, 35, 32, 48, 32, 44, 45, 41, 46]
score2 = [35, 48, 50, 37, 45, 41, 47, 50, 47, 41, 47, 48, 46, 49, 41, 38, 46, 42, 33, 39]
nameLst = [], score1=[], score2=[]
nameLst = [‘A’, ‘D’, ‘W’, ‘K’, ‘Z’]
score1 = [30, 20, 50]
score2 = [50, 30, 25, 49, 23, 42]
Special notes:
(1) you may call predefined sort() or sorted() in the body of any of the above functions
(2) you cannot use the statistics module
Python code to perform the given tasks
import math
def makeLst(aNameLst,e1ScoreLst,e2ScoreLst):#task1
if len(aNameLst) == 0 :
alist = []
else:
a = list(zip(e1ScoreLst,e2ScoreLst))
alist = list(zip(aNameLst,a))
return alist
def personalAverage(alist):#task2
if len(alist) == 0:
blist = []
else:
k = list(zip(*alist))
names = list(k[0])
b = list(k[1])
avg = []
for x in b:
avg.append((x[0]+x[1])/2)
blist = list(zip(names,avg))
return blist
def sortByName(blist):#task3
if len(blist) == 0:
h = []
else:
h = sorted(blist)
return h
def sortByAveScore(blist):#task4
if len(blist)==0:
k = []
else:
k = blist
k.sort(key = lambda x: x[1],reverse = True)
#print(k)
return k
def classMean(alist):#task5
if len(alist) == 0:
e1_mean = 'Nan'
e2_mean = 'Nan'
else:
k = list(zip(*alist))
names = list(k[0])
b = list(k[1])
e = list(zip(*b))
e1 = list(e[0])
e2 = list(e[1])
e1_mean = sum(e1)/len(e1)
e2_mean = sum(e2)/len(e2)
return e1_mean,e2_mean
def classMedian(alist):#task6
if len(alist)==0:
median1 = 'Nan'
median2 = 'Nan'
else:
k = list(zip(*alist))
names = list(k[0])
b = list(k[1])
e = list(zip(*b))
e1 = list(e[0])
e2 = list(e[1])
e1.sort()
e2.sort()
n1 = len(e1)
if n1 % 2 == 0:
m1 = e1[n1//2]
m2 = e1[n1//2 - 1]
median1 = (m1+m2)/2
else:
median1 = e1[n1//2]
n2 = len(e2)
if n2 % 2 == 0:
h1 = e2[n2//2]
h2 = e2[n2//2 - 1]
median2 = (h1+h2)/2
else:
median2 = e2[n2//2]
return median1,median2
def classStdDev(alist):3task7
if len(alist)==0:
std1 = 'Nan'
std2 = 'Nan'
else:
k = list(zip(*alist))
names = list(k[0])
b = list(k[1])
e = list(zip(*b))
e1 = list(e[0])
e2 = list(e[1])
e1_mean = sum(e1)/len(e1)
e2_mean = sum(e2)/len(e2)
v1 = 0
for x in e1:
v1 += (x-e1_mean)**2
v1 /= len(e1)
std1 = math.sqrt(v1)
v2 = 0
for x in e2:
v2 += (x-e2_mean)**2
v2 /= len(e2)
std2 = math.sqrt(v2)
return std1,std2
def main():
def call(nameLst,score1,score2):
if len(nameLst) == len(score1) == len(score2):
mlist = makeLst(nameLst,score1,score2)
avglist = personalAverage(mlist)
slist = sortByName(avglist)
salist = sortByAveScore(avglist)
print(classMean(mlist))
print(classMedian(mlist))
print(classStdDev(mlist))
else:
print("Please re-enter the data..")
#TestCase1
nameLst = ["Betty", "Andrew", "Zach", "Cathy", "Jay", "Kevin",
"Cassie", "Matt", "Lux", "Xavier", "Peter", "John", "Jamie", "Joe",
"Ellen", "Nancy", "Ray", "Bryce", "Gordon", "Gee"]
score1 = [50, 45, 48, 48, 46, 49, 35, 38, 42, 50, 44, 37, 35, 32,
48, 32, 44, 45, 41, 46]
score2 = [35, 48, 50, 37, 45, 41, 47, 50, 47, 41, 47, 48, 46, 49,
41, 38, 46, 42, 33, 39]
print("TEST CASE 1")
call(nameLst,score1,score2)
#TestCase2
nameLst = []
score1 = []
score2 = []
print("TEST CASE 2")
call(nameLst,score1,score2)
#TestCase3
nameLst = ['A', 'D', 'W', 'K', 'Z']
score1 = [30, 20, 50]
score2 = [50, 30, 25, 49, 23, 42]
print("TEST CASE 3")
call(nameLst,score1,score2)
if __name__ == '__main__':
main()
Screenshots for the above code:






Output of the program:

Note - Values nan for mean ,median and standard deviation are returnd when there is no data,that means when scores and names lists are empty.
Can check the accuracy of the mean,median and standard deviation for the data in a list using numpy built in functions as follows:
import numpy as np
a = [50, 45, 48, 48, 46, 49, 35, 38, 42, 50, 44, 37, 35, 32, 48, 32, 44, 45, 41, 46]
np.mean(a)#mean
np.median(a)#median
np.std(a)#standard deviation
PYTHON Given three unsorted lists, a name list (e.g. nameLst = [“Betty”, “Andrew”, “Zach”, “Cathy”, …]),...