💾 Archived View for gem.sdf.org › jquah › extracurriculars › gradefor.py captured on 2021-12-03 at 14:04:38.

View Raw

More Information

-=-=-=-=-=-=-

#!/usr/bin/python

import sys

if len(sys.argv) != 2: # the program name and the file to process
	# stop the program and print an error message
	sys.exit("Must specify the file containing grades");

try: 
	w = open('weights', 'r')
except IOError:
	sys.exit("weights file not found. Abort.")

CatNames=[]
Weights=[]
NoDrop=[]
rows=0

for line in w:
	workline=line.split()
	CatNames.append(workline[0])
	Weights.append(float(workline[1]))
	NoDrop.append(int(workline[2]))
	rows += 1

w.close()

GradeMat = [[0 for j in range(30)] for i in range(rows)]

CatNums={ "blank": "-1" }

for i in range(rows):
	CatNums[CatNames[i]] = i

f = open(sys.argv[1], 'r')
for line in f:
	workline=line.split()
	category=workline[0]
	if len(workline) > 2:
		thisgrade=float(workline[2])
	else:
		thisgrade=0
	rowtoadd=CatNames.index(category)
	colsofar=GradeMat[rowtoadd].index(0)
	GradeMat[rowtoadd][colsofar]=thisgrade
f.close()

# vector of category averages
CatAvgs = [0 for i in range(rows)]

# sort the grades in descending order
# take average of the top N of each category
for i in range(rows):
	TmpSort = sorted(GradeMat[i])
	TmpSort.reverse()
	numtocount = max([1,NoDrop[i]])
	CatAvgs[i]=sum(TmpSort[0:numtocount])/numtocount

# compute final grade
ElementwiseProd = [ CatAvgs[i]*Weights[i] for i in range(rows) ]
CourseGrade = sum(ElementwiseProd)/100

outCourse = "{}'s course grade is {}".format(sys.argv[1],CourseGrade)

print(CatAvgs)
print(outCourse)