Advertisement
SimeonTs

SUPyF2 Dict-Exercise - 10. SoftUni Exam Results

Oct 24th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.17 KB | None | 0 0
  1. """
  2. Dictionaries - Exercise
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1737#9
  4.  
  5. SUPyF2 Dict-Exercise - 10. SoftUni Exam Results (not included in final score)
  6.  
  7. Problem:
  8. Judge statistics on the last Programing Fundamentals exam was not working correctly, so you have the task to take all the submissions and analyze them properly. You should collect all the submissions and print the final results and statistics about each language that the participants submitted their solutions in.
  9. You will be receiving lines in the following format: "{username}-{language}-{points}" until you receive "exam finished". You should store each username and his submissions and points.
  10. You can receive a command to ban a user for cheating in the following format: "{username}-banned". In that case, you should remove the user from the contest, but preserve his submissions in the total count of submissions for each language.
  11. After receiving "exam finished" print each of the participants, ordered descending by their max points, then by username, in the following format:
  12. Results:
  13. {username} | {points}
  14. After that print each language, used in the exam, ordered descending by total submission count and then by language name, in the following format:
  15. Submissions:
  16. {language} – {submissionsCount}
  17. Input / Constraints
  18. Until you receive "exam finished" you will be receiving participant submissions in the following format:
  19. "{username}-{language}-{points}".
  20. You can receive a ban command -> "{username}-banned"
  21. The points of the participant will always be a valid integer in the range [0-100];
  22. Output
  23. • Print the exam results for each participant, ordered descending by max points and then by username,
  24. in the following format:
  25. Results:
  26. {username} | {points}
  27. • After that print each language, ordered descending by total submissions and then by language name,
  28. in the following format:
  29. Submissions:
  30. {language} – {submissionsCount}
  31. • Allowed working time / memory: 100ms / 16MB.
  32. Examples:
  33. Input:
  34. Pesho-Java-84
  35. Gosho-C#-84
  36. Gosho-C#-70
  37. Kiro-C#-94
  38. exam finished
  39.  
  40. Output:
  41. Results:
  42. Kiro | 94
  43. Gosho | 84
  44. Pesho | 84
  45. Submissions:
  46. C# - 3
  47. Java - 1
  48.  
  49. Comment:
  50. We order the participant descending by max points and then by name, printing only the username and the max points.
  51. After that we print each language along with the count of submissions,
  52. ordered descending by submissions count, and then by language name.
  53.  
  54. Input:
  55. Pesho-Java-91
  56. Gosho-C#-84
  57. Kiro-Java-90
  58. Kiro-C#-50
  59. Kiro-banned
  60. exam finished
  61.  
  62. Output:
  63. Results:
  64. Pesho | 91
  65. Gosho | 84
  66. Submissions:
  67. C# - 2
  68. Java - 2
  69.  
  70. Comment:
  71. Kiro is banned so he is removed from the contest, but
  72. he`s submissions are still preserved in the languages submissions count.
  73. So althou there are only 2 participants in the results, there are 4 submissions in total.
  74. """
  75.  
  76. # ---------------------------------------------------------------------------------------------
  77. # I have created two solutions to this problem. First will be with Dictionaries:
  78. # ---------------------------------------------------------------------------------------------
  79.  
  80. """
  81. submissions = {}
  82. submissions_count = {}
  83.  
  84. while True:
  85.    command = input()
  86.    if command == "exam finished":
  87.        break
  88.    data = command.split("-")
  89.    if len(data) == 3:  # Adding new submission.
  90.        username = data[0]
  91.        language = data[1]
  92.        points = int(data[2])
  93.        # If we don't have such user:
  94.        if username not in submissions:
  95.            submissions[username] = [language, points]
  96.        # if we have him
  97.        else:
  98.            # check if the new points are more then the old submission.
  99.            if submissions[username][1] < points and language == submissions[username][0]:
  100.                submissions[username][1] = points
  101.  
  102.        # Check if we have the language in the submissions_count
  103.        if language in submissions_count:
  104.            submissions_count[language] += 1
  105.        else:  # If we don't have it:
  106.            submissions_count[language] = 1
  107.  
  108.    elif len(data) == 2:  # Banning user.
  109.        user_to_ban = data[0]
  110.        if user_to_ban in submissions:
  111.            submissions.pop(user_to_ban)
  112.  
  113. # Print all the results:
  114. print(f"Results:")
  115. for user, value in sorted(submissions.items(), key=lambda x: (-x[1][1], x[0])):
  116.    print(f"{user} | {value[1]}")
  117.  
  118. print(f"Submissions:")
  119. for language, count in sorted(submissions_count.items(), key=lambda x: (-x[1], x[0])):
  120.    print(f"{language} - {count}")
  121. """
  122.  
  123. # ---------------------------------------------------------------------------------------------
  124. # And the second will be with Objects and Classes:
  125. # ---------------------------------------------------------------------------------------------
  126.  
  127.  
  128. class Submissions:
  129.     all_users = []
  130.     submissions = {}
  131.  
  132.     def __init__(self, username, language, points):
  133.         self.username = username
  134.         self.language = language
  135.         self.points = int(points)
  136.         self.add_or_update_user()
  137.         self.add_or_update_submission()
  138.  
  139.     @classmethod
  140.     def run(cls):
  141.         while True:
  142.             submission = input()
  143.             if submission == "exam finished":
  144.                 Submissions.print()
  145.                 break
  146.             submission = submission.split("-")
  147.             if len(submission) == 3:
  148.                 Submissions(submission[0], submission[1], int(submission[2]))
  149.             else:
  150.                 Submissions.banned(submission[0])
  151.  
  152.     def add_or_update_user(self):
  153.         add_new_user = True
  154.         for user in Submissions.all_users:
  155.             if user.username == self.username:
  156.                 add_new_user = False
  157.                 if self.points > user.points:
  158.                     user.points = self.points
  159.         if add_new_user:
  160.             Submissions.all_users += [self]
  161.  
  162.     def add_or_update_submission(self):
  163.         if self.language not in Submissions.submissions:
  164.             Submissions.submissions[self.language] = 1
  165.         else:
  166.             Submissions.submissions[self.language] += 1
  167.  
  168.     @classmethod
  169.     def print(cls):
  170.         print("Results:")
  171.         for user in sorted(Submissions.all_users, key=lambda p: (-p.points, p.username)):
  172.             print(f"{user.username} | {user.points}")
  173.         print(f"Submissions:")
  174.         for language, count in sorted(Submissions.submissions.items(), key=lambda x: (-x[1], x[0])):
  175.             print(f"{language} - {count}")
  176.  
  177.     @classmethod
  178.     def banned(cls, name):
  179.         for user in Submissions.all_users:
  180.             if user.username == name:
  181.                 Submissions.all_users.remove(user)
  182.  
  183.  
  184. if __name__ == '__main__':
  185.     Submissions.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement