Skip to content

Instantly share code, notes, and snippets.

@johnnyg
Created June 27, 2012 23:40
Show Gist options
  • Save johnnyg/3007619 to your computer and use it in GitHub Desktop.
Save johnnyg/3007619 to your computer and use it in GitHub Desktop.
Calculates (interpolated) average grade
#!/usr/bin/env python2
from itertools import izip, count
from math import fsum, ceil, floor
import fileinput
GRADES = ("FL", "PC", "PS", "CR", "DN", "HD")
MARKS = dict(izip(GRADES, count()))
def average(seq):
return fsum(seq)/len(seq)
def interpolated_grade(mark):
upper = int(ceil(mark))
lower = int(floor(mark))
grade = GRADES[lower]
if lower != upper:
grade += "/%s" % GRADES[upper]
return grade
mark = average([average([MARKS[grade] for grade in line.strip().split('/')]) for line in fileinput.input()])
print "%s (%.1f)" % (interpolated_grade(mark), mark)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment