Gorcupt

Untitled

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