SimeonTs

SUPyF2 Dict-Exercise - 06. Courses

Oct 24th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. """
  2. Dictionaries - Exercise
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1737#5
  4.  
  5. SUPyF2 Dict-Exercise - 06. Courses
  6.  
  7. Problem:
  8. Write a program that keeps information about courses. Each course has a name and registered students.
  9. You will be receiving a course name and a student name, until you receive the command "end".
  10. Check if such course already exists, and if not, add the course. Register the user into the course.
  11. When you receive the command "end", print the courses with their names and total registered users,
  12. ordered by the count of registered users in descending order.
  13. For each contest print the registered users ordered by name in ascending order.
  14.  
  15. Input:
  16. • Until the "end" command is received, you will be receiving input in the format: "{courseName} : {studentName}".
  17. • The product data is always delimited by " : ".
  18. Output:
  19. • Print the information about each course in the following the format:
  20. "{courseName}: {registeredStudents}"
  21. • Print the information about each student, in the following the format:
  22. "-- {studentName}"
  23.  
  24. Examples:
  25. Input:
  26. Programming Fundamentals : John Smith
  27. Programming Fundamentals : Linda Johnson
  28. JS Core : Will Wilson
  29. Java Advanced : Harrison White
  30. end
  31.  
  32. Output:
  33. Programming Fundamentals: 2
  34. -- John Smith
  35. -- Linda Johnson
  36. JS Core: 1
  37. -- Will Wilson
  38. Java Advanced: 1
  39. -- Harrison White
  40.  
  41. Input:
  42. Algorithms : Jay Moore
  43. Programming Basics : Martin Taylor
  44. Python Fundamentals : John Anderson
  45. Python Fundamentals : Andrew Robinson
  46. Algorithms : Bob Jackson
  47. Python Fundamentals : Clark Lewis
  48. end
  49.  
  50. Output:
  51. Python Fundamentals: 3
  52. -- Andrew Robinson
  53. -- Clark Lewis
  54. -- John Anderson
  55. Algorithms: 2
  56. -- Bob Jackson
  57. -- Jay Moore
  58. Programming Basics: 1
  59. -- Martin Taylor
  60. """
  61.  
  62. courses = {}
  63.  
  64. while True:
  65.     command = input()
  66.     if command == "end":
  67.         break
  68.     course, student = command.split(" : ")
  69.     if course in courses:
  70.         courses[course].append(student)
  71.     else:
  72.         courses[course] = [student]
  73.  
  74. for course, students in sorted(courses.items(), key=lambda x: (len(x[1])), reverse=True):
  75.     print(f"{course}: {len(courses[course])}")
  76.     for student in sorted(students):
  77.         print(f"-- {student}")
Add Comment
Please, Sign In to add comment