day1/exercise/markstats.py
changeset 64 333092b68926
equal deleted inserted replaced
63:f5eac04a00fe 64:333092b68926
       
     1 import math
       
     2 
       
     3 f = open('/home/madhu/Desktop/marks.dat')
       
     4     
       
     5 subj_marks = [[]] * 5
       
     6 names = []
       
     7 for line in f:
       
     8     fields = line.split(';')
       
     9     names.append(fields[2])
       
    10     for i in range(5):
       
    11         subj_marks[i].append(int(fields[i+3]))
       
    12 
       
    13 for i in range(5):
       
    14     avg_marks = float(sum(subj_marks[i])) / len(subj_marks[i])
       
    15     student = names[subj_marks[i].index(max(subj_marks[i]))]
       
    16     sigma = 0
       
    17     for j in subj_marks[i]:
       
    18         sigma += (j - avg_marks) ** 2
       
    19 
       
    20     std_dev = math.sqrt(sigma)
       
    21     print "Average marks for subject: %f is Standard Deviation is %f, Student with Highest Marks is %s" % (avg_marks, std_dev, student)
       
    22