Advertisement
Guest User

Courses 20/100 solution

a guest
Nov 23rd, 2020
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. def register(data):
  2.     courses = {}
  3.     students = {}
  4.     result = ""
  5.  
  6.     while data != "end":
  7.         course_name, student_name = data.split(" : ")
  8.  
  9.         if course_name not in courses:
  10.             courses[course_name] = 1
  11.         elif course_name in courses:
  12.             courses[course_name] += 1
  13.  
  14.         students[student_name] = course_name
  15.  
  16.         data = input()
  17.  
  18.     ordered_courses = sorted(courses.items(), key=lambda x: (-x[1], x[0]))
  19.     ordered_students = sorted(students.items())
  20.  
  21.     for course, count in ordered_courses:
  22.         result += f"{course}: {count}\n"
  23.         for student, subject in ordered_students:
  24.             if subject == course:
  25.                 result += f"-- {student}\n"
  26.  
  27.     return result
  28.  
  29.  
  30. action = input()
  31. print(register(action))
  32.  
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement