Skip to content

Instantly share code, notes, and snippets.

@teebow1e
Created January 26, 2026 19:25
Show Gist options
  • Select an option

  • Save teebow1e/c9eb2239fd2363c1643df67c3a0c1eb7 to your computer and use it in GitHub Desktop.

Select an option

Save teebow1e/c9eb2239fd2363c1643df67c3a0c1eb7 to your computer and use it in GitHub Desktop.
[HUST] Tính điểm CPA dựa trên thông tin trên CTT
def calculate_cpa_both_modes(data_string):
grade_points = {
'A+': 4.0,
'A': 4.0,
'B+': 3.5,
'B': 3.0,
'C+': 2.5,
'C': 2.0,
'D+': 1.5,
'D': 1.0,
'F': 0.0
}
lines = data_string.strip().split('\n')
total_credits_with_f = 0
total_points_with_f = 0
total_credits_without_f = 0
total_points_without_f = 0
all_courses = []
passed_courses = []
failed_courses = []
# header included so I use lines[1:] !important
for line in lines[1:]:
parts = line.split('\t')
if len(parts) >= 5:
semester = parts[0].strip()
course_code = parts[1].strip()
course_name = parts[2].strip()
credits = int(parts[3].strip())
grade = parts[4].strip()
# only handle subjects with credits > 0
if credits > 0 and grade in grade_points:
point = grade_points[grade]
course_info = {
'semester': semester,
'code': course_code,
'name': course_name,
'credits': credits,
'grade': grade,
'point': point
}
total_credits_with_f += credits
total_points_with_f += point * credits
all_courses.append(course_info)
if grade == 'F':
failed_courses.append(course_info)
else:
total_credits_without_f += credits
total_points_without_f += point * credits
passed_courses.append(course_info)
# Quy chế HUST
# 3. Điểm trung bình tích lũy (CPA) là trung bình cộng điểm số quy đổi theo thang 4
# của các học phần đã học từ đầu khóa thuộc chương trình đào tạo với trọng số là số TC của
# học phần. Điểm trung bình tích lũy được làm tròn tới 2 chữ số thập phân.
cpa_with_f = round(total_points_with_f / total_credits_with_f, 2) if total_credits_with_f > 0 else 0.0
cpa_without_f = round(total_points_without_f / total_credits_without_f, 2) if total_credits_without_f > 0 else 0.0
return {
'all_courses': all_courses,
'passed_courses': passed_courses,
'failed_courses': failed_courses,
'cpa_with_f': cpa_with_f,
'credits_with_f': total_credits_with_f,
'cpa_without_f': cpa_without_f,
'credits_without_f': total_credits_without_f
}
def print_comprehensive_report(data):
result = calculate_cpa_both_modes(data)
# print("=" * 80)
# print("CHI TIẾT ĐIỂM CÁC MÔN HỌC")
# print("=" * 80)
# print(f"{'Mã HP':<12} {'Tên môn học':<40} {'TC':<4} {'Điểm':<6} {'Điểm số'}")
# print("-" * 80)
# for course in result['all_courses']:
# marker = " ⚠️" if course['grade'] == 'F' else ""
# print(f"{course['code']:<12} {course['name'][:40]:<40} {course['credits']:<4} {course['grade']:<6} {course['point']:.1f}{marker}")
# print("=" * 80)
# print()
# Hiển thị kết quả so sánh
print("=" * 80)
print("Kết quả tính")
print("=" * 80)
if result['failed_courses']:
print(f"\n📌 SỐ MÔN TRƯỢT: {len(result['failed_courses'])} môn")
print(f" Tổng tín chỉ trượt: {sum(c['credits'] for c in result['failed_courses'])} TC")
print("\n Danh sách môn trượt:")
for course in result['failed_courses']:
print(f" - {course['code']}: {course['name']} ({course['credits']} TC)")
else:
print("\n✅ Không có môn trượt")
print("\n" + "-" * 80)
print(f"{'PHƯƠNG THỨC TÍNH':<40} {'Tín chỉ':<12} {'CPA (4.0)':<12} {'CPA (10.0)'}")
print("-" * 80)
print(f"{'1️⃣ Tính cả môn trượt':<40} {result['credits_with_f']:<12} {result['cpa_with_f']:<12.2f} {result['cpa_with_f']*2.5:.2f}")
print(f"{'2️⃣ Chỉ tính môn đã qua':<40} {result['credits_without_f']:<12} {result['cpa_without_f']:<12.2f} {result['cpa_without_f']*2.5:.2f}")
if result['failed_courses']:
diff_cpa = result['cpa_without_f'] - result['cpa_with_f']
diff_credits = result['credits_with_f'] - result['credits_without_f']
print("-" * 80)
print(f"{'📊 Chênh lệch':<40} {diff_credits:<12} {diff_cpa:<12.2f} {diff_cpa*2.5:.2f}")
print("=" * 80)
return result
# Data column must be seperated by "\t" (tab character)
data = """
Học kỳ Mã HP Tên HP TC Điểm học phần Tên HP(E)
thông tin điểm lấy tại https://ctt-sis.hust.edu.vn/Students/StudentCourseGrade.aspx
20242 IT4403E Phát triển ứng dụng Web an toàn 2 A Secure web Development
"""
print_comprehensive_report(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment