Advertisement
Guest User

03. Student Credits

a guest
Feb 12th, 2023
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. import re
  2.  
  3.  
  4. def get_course_name(data):
  5.     valid = []
  6.     pattern = "[A-Za-z]+"
  7.  
  8.     for el in data:
  9.         match = " ".join(re.findall(pattern, el))
  10.         valid.append(match)
  11.     return valid
  12.  
  13.  
  14. def get_course_grades(data):
  15.     valid = []
  16.     pattern = "\-[A-Za-z]|\d+"
  17.  
  18.     for el in data:
  19.         match = " ".join(re.findall(pattern, el))
  20.         valid.append(match)
  21.     return valid
  22.  
  23.  
  24. def students_credits(*data):
  25.     course_names = get_course_name(data)
  26.     course_grades = get_course_grades(data)
  27.  
  28.     student_info = {}
  29.  
  30.     for el in range(len(course_names)):
  31.         student_info[course_names[el]] = [int(x) for x in course_grades[el].split()]
  32.  
  33.  
  34.     # actual logic for the calculations (start on the following line)
  35.     course_and_credits_info = {}
  36.     total_student_credits = 0
  37.  
  38.     for course, grade in student_info.items():
  39.         course_credits = grade[0]
  40.         total_points = grade[1]
  41.         student_points = grade[2]
  42.  
  43.         percentage = student_points / total_points
  44.         student_credits = course_credits * percentage
  45.  
  46.         course_and_credits_info[course] = student_credits
  47.         total_student_credits += student_credits
  48.  
  49.     sorted_course_and_credits_info = dict(sorted(course_and_credits_info.items(), key=lambda x: -x[1]))
  50.  
  51.     if total_student_credits >= 240:
  52.         print(f"Diyan gets a diploma with {total_student_credits:.1f} credits.")
  53.     else:
  54.         print(f"Diyan needs {240 - total_student_credits:.1f} credits more for a diploma.")
  55.  
  56.     return '\n'.join([f"{k} - {v:.1f}" for k, v in sorted_course_and_credits_info.items()])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement