How to make a triple equivalence dictionary?

  • A+

I'm trying to make a mapping between the grade categories in the picture below. Then I would like to be able to call a function that converts a grade to the same grade in an equivalent format. Ex:

def convert(num, letter, gpa):     """Converts a grade into an equivalent grade. The desired output will be      specified by -1 and the grade format not to be involved in the conversion      will be specified by None. When converting to GPA, the minimum of the gpa      range will be returned."""     >>> convert(83, None, -1)     >>> 'A-'     >>>convert(-1, 'B+', None)      >>>77 

I thought of making three parallel lists for the equivalence and then the function would end up using a bunch of if statements. What's the best way to do this?

How to make a triple equivalence dictionary?

I would probably do something like this, it avoids conditional branching and makes it abundantly clear what you are trying to do when you convert.

class GradeRange:     def __init__(self, pct, ltr, gpa):         self.pct = pct         self.ltr = ltr         self.gpa = gpa  class GradeTable:     def __init__(self):         self.ranges = [             GradeRange(range(0,  50), 'F',  0.0),             GradeRange(range(50, 53), 'D-', 0.7),             GradeRange(range(53, 57), 'D',  1.0),             GradeRange(range(57, 60),  'D+', 1.3),             GradeRange(range(60, 63), 'C-', 1.7),             GradeRange(range(63, 67), 'C', 2.0),             GradeRange(range(67, 70), 'C+', 2.3),             GradeRange(range(70, 73), 'B-', 2.7),             GradeRange(range(73, 77), 'B', 3.0),             GradeRange(range(77, 80), 'B+', 3.3),             GradeRange(range(80, 85), 'A-', 3.7),             GradeRange(range(85, 90), 'A', 4.0),             GradeRange(range(90, 101), 'A+', 4.0),         ]      def convert_pct(self, pct):         for r in self.ranges:             if pct in r.pct:                 return r.ltr, r.gpa      def convert_ltr(self, ltr):         for r in self.ranges:             if r.ltr == ltr:                 return r.pct[0], r.gpa      def convert_gpa(self, gpa):         for r in self.ranges:             if r.gpa == gpa:                 return r.pct[0], r.ltr 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: