Skip to content

Instantly share code, notes, and snippets.

@KorigamiK
Last active June 22, 2024 20:57
Show Gist options
  • Save KorigamiK/2ea6814dd339aded53dc7ebf8af67007 to your computer and use it in GitHub Desktop.
Save KorigamiK/2ea6814dd339aded53dc7ebf8af67007 to your computer and use it in GitHub Desktop.
cgpa drop calculation

Calculate Optimal Sub Drop CGPA

To get max gains 💪

data.csv
calc_cgpa.testcases
import csv
from dataclasses import dataclass
from typing import List, Tuple
"""
Use https://www.convertcsv.com/html-table-to-csv.htm
to convert your transcript to `data.csv`
"""
@dataclass
class Subject:
s_no: int
paper_code: str
paper_name: str
paper_type: str
sem: str
credit: int
grade_letter: str
grade_point: float
credit_points: float
def parse_csv(filename: str) -> List[Subject]:
subjects = []
with open(filename, "r") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
subject = Subject(
s_no=int(row["S. No."]),
paper_code=row["Paper Code"],
paper_name=row["Paper Name"],
paper_type=row["Paper Type"],
sem=row["Sem"],
credit=int(row["Credit"]),
grade_letter=row["Grade Letter"],
grade_point=float(row["Grade Point"]),
credit_points=float(row["Credit Points"]),
)
subjects.append(subject)
return subjects
def print_subjects(subjects: List[Subject]):
headers = [
"S. No",
"Paper Code",
"Paper Name",
"Sem",
"Credit",
"Credit Points",
"Grade Point",
]
col_widths = [
max(
len(str(getattr(s, h.lower().replace(". ", "_").replace(" ", "_"))))
for s in subjects + [Subject(0, "", "", "", "", 0, "", 0, 0)]
)
for h in headers
]
col_widths = [max(len(h), w) for h, w in zip(headers, col_widths)]
# Print headers
header = " | ".join(h.ljust(w) for h, w in zip(headers, col_widths))
print(header)
print("-" * len(header))
# Print rows
for subject in subjects:
row = [
str(subject.s_no),
subject.paper_code,
subject.paper_name,
subject.sem,
str(subject.credit),
str(subject.credit_points),
f"{subject.grade_letter} ({subject.grade_point})",
]
print(" | ".join(str(r).ljust(w) for r, w in zip(row, col_widths)))
def calculate_cgpa(subjects: List[Subject]) -> float:
total_credit_points = sum(subject.credit_points for subject in subjects)
total_credits = sum(subject.credit for subject in subjects)
return total_credit_points / total_credits if total_credits > 0 else 0
def optimize_cgpa(
subjects: List[Subject], num_drops: int
) -> Tuple[float, List[Subject], List[Subject]]:
current_cgpa = calculate_cgpa(subjects)
total_credits = sum(subject.credit for subject in subjects)
total_credit_points = sum(subject.credit_points for subject in subjects)
subject_impacts = []
for subject in subjects:
cgpa_without_subject = (total_credit_points - subject.credit_points) / (
total_credits - subject.credit
)
impact = current_cgpa - cgpa_without_subject
subject_impacts.append((impact, subject))
subject_impacts.sort(key=lambda x: x[0], reverse=True)
kept_subjects = [subject for _, subject in subject_impacts[:-num_drops]]
dropped_subjects = [subject for _, subject in subject_impacts[-num_drops:]]
new_cgpa = calculate_cgpa(kept_subjects)
return new_cgpa, kept_subjects, dropped_subjects
def main(print_to_console: bool):
filename = "data.csv"
subjects = parse_csv(filename)
if print_to_console:
print("Original CSV data:")
print_subjects(subjects)
current_cgpa = calculate_cgpa(subjects)
print(f"\nCurrent CGPA: {current_cgpa:.2f}")
num_drops = int(input("\nEnter the number of subjects to drop: "))
best_cgpa, kept_subjects, dropped_subjects = optimize_cgpa(subjects, num_drops)
print(f"\nBest CGPA after dropping {num_drops} subject(s): {best_cgpa:.2f}")
print("\nDropped subjects:")
print_subjects(dropped_subjects)
print(f"\nOld CGPA: {current_cgpa:.2f}")
print(f"New CGPA: {best_cgpa:.2f}")
if __name__ == "__main__":
main(False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment