Created
September 19, 2022 21:24
-
-
Save les-peters/2a0c4f6ace946c69a978f2946399954e to your computer and use it in GitHub Desktop.
GPA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
question = """ | |
Given a set of letter grades, output the GPA (grade point average) of those grades. | |
Key and examples: | |
A = 4 grade points | |
A- = 3.7 grade points | |
B+ = 3.3 grade points | |
B = 3 grade points | |
B- = 2.7 grade points | |
C+ = 2.3 grade points | |
C = 2 grade points | |
C- = 1.7 grade points | |
D+ = 1.3 grade points | |
D = 1 grade point | |
D- = 0.7 grade points | |
F = 0 grade points | |
$ calculateGPA(['A']) | |
$ 4 | |
$ calculateGPA(['F', 'F', 'F']) | |
$ 0 | |
$ calculateGPA(['A', 'A-', 'B+', 'B', 'B-']) | |
$ 3.3 | |
$ calculateGPA(['A', 'B+', 'C-', 'A']) | |
$ 3.3 | |
""" | |
import re | |
def calculateGPA(grades): | |
total = 0 | |
T = { 'A' : 4, 'B' : 3, 'C' : 2, 'D' : 1, 'F' : 0 } | |
Z = [] | |
for grade in grades: | |
Z.append(grade) | |
all_grades = ''.join(sorted(Z)) | |
for k in T.keys(): | |
all_grades = re.sub(k, str(T[k]), all_grades) | |
for ch in all_grades: | |
if ch == '-': | |
total -= 0.3 | |
elif ch == '+': | |
total += 0.3 | |
else: | |
total += int(ch) | |
print(str(total / len(grades))) | |
calculateGPA(['A']) | |
calculateGPA(['F', 'F', 'F']) | |
calculateGPA(['A', 'A-', 'B+', 'B', 'B-']) | |
calculateGPA(['A', 'B+', 'C-', 'A']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment