(Using python) you can create a list yourself e.g.[2,3,4,7,8,97,96,45] The media
ID: 3768761 • Letter: #
Question
(Using python) you can create a list yourself e.g.[2,3,4,7,8,97,96,45]
The median value
Calculate the media value - the middle value:
sort the grades in ascending order.
if the # of students is odd you can take the middle of all the grades (i.e. [0, 50, 100] - the median is 50).
if the # of students is even you can compute the mean by averaging the middle two values (i.e. [0, 50, 60, 100] - the median is 55
The mode value
Calculate the mode value - the grade that occurs most frequently - i.e. [0, 50, 50, 100] - the mode is 50.
Hint: you will probably want to create two new lists to do this. One new list can hold the UNIQUE test scores found and the other can hold the # of times that score has been seen. For example:
Use the "in" operator, the "append" method, and the max function to help you solve this problem!
The range
Calculate the range - the difference between the largest and smallest grade in the set.
Sample output
Here's a sample running of your program for class1:
Explanation / Answer
def Median(l):
mid = (low+high)/2
if (len(l)%2 == 1):
return l[mid]
return (l[mid]+l[mid-1])/2.0
def Mode(l):
dict = {}
for ch in l:
if ch not in dict:
dict[ch] = 1
else:
dict[ch] += 1
max = -1
mode = 0
for key in dict:
if (dict[key] > max):
max = dict[key]
mode = key
return mode
def main():
print 'Enter a class file to grade (i.e class1 for class1.txt) : ',
s = raw_input()+'.txt'
file = open(s,'r')
l = []
for line in file:
l.append(int(line))
l = sorted(l)
print 'Grade Summary: '
print 'Total students: '+str(len(l))
print 'Highest score: '+str(l[-1])
print 'Lowest score : '+str(l[0])
print 'Mean score: '+str((sum(l)+0.0)/len(l))
print 'Median score: '+str(Median(l))
print 'Mode: '+str(Mode(l))
print 'Range: '+str(l[-1] - l[0])
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.