In python: Part III: Recursive GPA Calculator (20 points) Write a recursive func
ID: 3598014 • Letter: I
Question
In python:
Part III: Recursive GPA Calculator (20 points) Write a recursive function gpa.calculator (0 that takes a single argument grades: a list containing alternating letter grades and credits for a group of courses taken by a student (see below for further explanation). Only the grades A, B, C, D and F are possible. A list of grades like ['A', 3, 'B', 4, 'D',3] indicates that the student took three courses with the respective credit values (3, 4 and 3). The function computes the weighted GPA and returns i. Note that letter grades can be provided in uppercase or lowercase. Note: Your code should be able to handle invalid grades by returning None. Invalid grades include: 1. A negative or zero credit value 2. A letter grade that is not one of A/a/B/b/C/c/D/d/F/f.Explanation / Answer
def credit_sum_helper(grades):
if grades == None or len(grades) == 0:
return 0.0
x = grades[0]
y = grades[1]
if y < 0 or not x in "AaBbCcDdFf":
return None
z = credit_sum_helper(grades[2:])
if z == None:
return None
return y + z
def get_points_for_grade(g):
if g == 'A' or g == 'a':
return 4.0
elif g == 'B' or g == 'b':
return 3.0
elif g == 'C' or g == 'c':
return 2.0
elif g == 'D' or g == 'd':
return 1.0
return 0.0
def grade_points_helper(grades):
if grades == None or len(grades) == 0:
return 0.0
x = grades[0]
y = grades[1]
if y < 0 or not x in "AaBbCcDdFf":
return None
g = get_points_for_grade(x)
r = grade_points_helper(grades[2:])
if r == None:
return None
return y*g + r
def gpa_calculator(grades):
total_grade_points = grade_points_helper(grades)
if total_grade_points == None:
return None
credit_sum = credit_sum_helper(grades)
if credit_sum == None or credit_sum == 0:
return None
return total_grade_points/credit_sum
print(gpa_calculator(['A', 4, 'B', 4, 'A', 4]))
print(gpa_calculator(['A', 4, 'O', 4, 'C', 1, 'F', 2]))
# copy pastable code link: https://paste.ee/p/qJZ6g
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.