Gorcupt

1

Mar 28th, 2022
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. import json
  2. import collections
  3. from operator import itemgetter
  4. from threading import Thread
  5. import multiprocessing.dummy as multiprocessing
  6.  
  7.  
  8. def top10most_difficult():
  9. peoplesArray = loadJson()
  10. dictSubjectAndCount = collections.Counter() # предмет и кол-во оценок по нему
  11. dictSubjectAndMark = collections.Counter() # предмет и сумма оценок по нему
  12. dictSubjectAndAverage = {} # предмет и средняя оценка по нему
  13. for d in peoplesArray:
  14. dictSubjectAndCount[d['subject']] += 1
  15. dictSubjectAndMark[d['subject']] += (d['mark'])
  16. for names in list(dictSubjectAndCount.keys()):
  17. dictSubjectAndAverage[names] = dictSubjectAndMark[names] / dictSubjectAndCount[names]
  18.  
  19. # отсортировать словарь по значению и выбрать первые 10:
  20. c = collections.Counter(dictSubjectAndAverage).most_common() # возвращает список с tuple ('предмет', ср.балл)
  21. c.reverse()
  22. print('\n10 самых трудных предметов:')
  23. for elem in c[:10]:
  24. print(elem[0])
  25.  
  26.  
  27. # находим средний балл для студента
  28.  
  29.  
  30. def top10performance():
  31. peoplesArray = loadJson()
  32. # сумма баллов по предметам
  33. marks = collections.Counter()
  34. # кол-во предметов
  35. counts = collections.Counter()
  36. performance = {}
  37. for people in peoplesArray:
  38. marks[people['name']] += people['mark']
  39. counts[people['name']] += 1
  40. for names in list(marks.keys()):
  41. performance[names] = marks[names] / counts[names]
  42. print("\nТоп 10 студентов по успеваемости:")
  43. for student in sorted(performance.items(), key=itemgetter(1), reverse=True)[:10]:
  44. print(student)
  45.  
  46.  
  47. def top10_leave():
  48. peoplesArray = loadJson()
  49. # кол-во прогулов
  50. counts = collections.Counter()
  51. for people in peoplesArray:
  52. counts[people['name']] += people['leave']
  53. print("\nТоп 10 студентов по прогулам:")
  54. for student in counts.most_common()[:10]:
  55. print(student)
  56.  
  57.  
  58. def linearSolution():
  59. top10most_difficult()
  60. top10performance()
  61. top10_leave()
  62.  
  63.  
  64. def threadingSolution():
  65. thread1 = Thread(target=top10most_difficult())
  66. thread2 = Thread(target=top10performance())
  67. thread3 = Thread(target=top10_leave())
  68. thread1.start()
  69. thread2.start()
  70. thread3.start()
  71.  
  72. thread1.join()
  73. thread2.join()
  74. thread3.join()
  75.  
  76.  
  77. def processSolution():
  78. pool = multiprocessing.Pool()
  79. pool.map(lambda f: f(), [top10most_difficult])
  80. pool.map(lambda f: f(), [top10performance])
  81. pool.map(lambda f: f(), [top10_leave])
  82. pool.close()
  83. pool.join()
  84.  
  85.  
  86. def addRecord():
  87. peoplesArray = loadJson()
  88. name = input('Введите ФИО\n')
  89. subject = input('Введите предмет\n')
  90. mark = int(input('Введите средний балл по предмету\n'))
  91. leave = int(input('Введите кол-во прогулов по предмету\n'))
  92.  
  93. new_student = {'name': name, 'subject': subject, 'mark': mark, 'leave': leave}
  94.  
  95. peoplesArray.append(new_student)
  96.  
  97. new_json = {"peoples": peoplesArray}
  98.  
  99. with open("json.json", "w") as write_file:
  100. json.dump(new_json, write_file)
  101.  
  102.  
  103. def loadJson():
  104. jDict = json.load(open('json.json', 'r'))
  105. return jDict.get('peoples') # возвращает список со словарями
  106.  
  107.  
  108. n = input('\n1-линейное решение\n2-решение с потоками\n3-решение с процессами\n4-добавить запись в '
  109. 'json\nexit-выход\nВвод ')
  110.  
  111. while n != 'exit':
  112. if n == '1':
  113. linearSolution()
  114. if n == '2':
  115. threadingSolution()
  116. if n == '3':
  117. processSolution()
  118. if n == '4':
  119. addRecord()
  120. if n == 'exit':
  121. break
  122. n = input('\n1-линейное решение\n2-решение с потоками\n3-решение с процессами\n4-добавить запись в'
  123. 'json\nexit-выход\nВвод: ')
  124.  
Advertisement
Add Comment
Please, Sign In to add comment