import fractions import typing MAX_CAPACITY: int = 300 def ratio( grade_in: int, grade_out: int, across_categories: bool ) -> typing.Optional[fractions.Fraction]: trading_up = grade_out > grade_in grade_base = 6 if trading_up else 3 grade_ratio = fractions.Fraction(grade_base ** grade_out, grade_base ** grade_in) category_ratio = ( fractions.Fraction(6, 1) if across_categories else fractions.Fraction(1, 1) ) result = grade_ratio * category_ratio if max(result.numerator, result.denominator) > MAX_CAPACITY: return None else: return result