SimeonTs

SUPyF2 Dict-Exercise - 07. Student Academy

Oct 24th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. """
  2. Dictionaries - Exercise
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1737#6
  4.  
  5. SUPyF2 Dict-Exercise - 07. Student Academy
  6.  
  7. Problem:
  8. Write a program that keeps information about students and their grades.
  9. You will receive n pair of rows. First you will receive the student's name, after that you will receive his grade.
  10. Check if the student already exists and if not, add him. Keep track of all grades for each student.
  11. When you finish reading the data, keep the students with average grade higher than or equal to 4.50.
  12. Order the filtered students by average grade in descending order.
  13. Print the students and their average grade in the following format:
  14. {name} –> {averageGrade}
  15. Format the average grade to the 2nd decimal place.
  16.  
  17. Examples:
  18. Input:          Output:
  19. 5               John -> 5.00
  20. John            George -> 5.00
  21. 5.5             Alice -> 4.50
  22. John
  23. 4.5
  24. Alice
  25. 6
  26. Alice
  27. 3
  28. George
  29. 5
  30.  
  31. Input:          Output:
  32. 5               Robert -> 6.00
  33. Amanda          Rob -> 5.50
  34. 3.5             Christian -> 5.00
  35. Amanda
  36. 4
  37. Rob
  38. 5.5
  39. Christian
  40. 5
  41. Robert
  42. 6
  43. """
  44. academy = {}
  45.  
  46. for i in range(int(input())):
  47.     student, grade = input(), float(input())
  48.     if student not in academy:
  49.         academy[student] = [grade]
  50.     else:
  51.         academy[student].append(grade)
  52.  
  53. for student, grades in academy.items():
  54.     academy[student] = (sum(grades) / len(grades))
  55.  
  56. for student, grade in sorted(academy.items(), key=lambda x: -x[1]):
  57.     if grade >= 4.5:
  58.         print(f"{student} -> {grade:.2f}")
Add Comment
Please, Sign In to add comment