def make_gradebook():
return []
def make_module_info(module, grade):
module_info = (module,grade)
return module_info
def get_module(module_info):
r
What if you, instead of having multiple if/elif/else
checks, define dictionaries grade->value and sign-value. Then, in the key function grade_to_numeric_marks
just sum up base points per grade and points per sign (+/- or empty).
For example (you may need to tweak values per grade/sign a bit):
points = {'A': 80, 'B': 65}
signs = {'+': 10, '-': -5}
def grade_to_numeric_marks(item):
grade = item[1]
return points.get(grade[0], 50) + signs.get(grade[1:], 0)
def sort_by_grade(gradebook):
return sorted(gradebook, key=lambda x: grade_to_numeric_marks(x), reverse=True)
grades = [('CS1010S', 'A+'), ('MA1101R', 'C'), ('SSA1207', 'B+'), ('CS2020', 'A')]
print sort_by_grade(grades)
prints:
[('CS1010S', 'A+'), ('CS2020', 'A'), ('SSA1207', 'B+'), ('MA1101R', 'C')]
Or, as @clutton mentioned in the comments, define just one dictionary with a mapping grade to points:
{'A+': 95, 'A': 90 ... }
Then, you can simplify the sorting:
points = {'A+': 95, 'A': 90, 'B+': 70, 'B': 65, 'C+': 55, 'C': 50} # need to define all of the possible grades
grades = [('CS1010S', 'A+'), ('MA1101R', 'C'), ('SSA1207', 'B+'), ('CS2020', 'A')]
print sorted(grades, key=lambda x: points.get(x[1]), reverse=True)