Advertisement
Angel367

python 3.1-3.12pr

Apr 1st, 2023
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. import csv
  2. import datetime
  3. import matplotlib
  4. from matplotlib import pyplot
  5.  
  6.  
  7. def parse_time(text):
  8. return datetime.datetime.strptime(text, '%Y-%m-%d %H:%M:%S.%f')
  9.  
  10.  
  11. def load_csv(filename):
  12. with open(filename, encoding='utf8') as f:
  13. return list(csv.reader(f, delimiter=','))
  14.  
  15.  
  16. # id, task, variant, group, time
  17. messages = load_csv('data/messages.csv')
  18.  
  19. # id, message_id, time, status
  20. checks = load_csv('data/checks.csv')
  21.  
  22. # task, variant, group, time, status, achievements
  23. statuses = load_csv('data/statuses.csv')
  24.  
  25. games = load_csv('data/GAMES.csv')
  26.  
  27.  
  28. def convert_string_to_list(text):
  29. return list(map(int, text[1:-1].split(',')))
  30.  
  31.  
  32. # 3.1
  33. dates = []
  34. for row in checks:
  35. dates.append(parse_time(row[2]))
  36. days = [0, 0, 0, 0, 0, 0, 0]
  37. for date in dates:
  38. days[date.weekday()] += 1
  39.  
  40. matplotlib.pyplot.plot(days)
  41. matplotlib.pyplot.show()
  42.  
  43. # 3.2
  44. hours = []
  45. for i in range(0, 24):
  46. hours.append(0)
  47. for date in dates:
  48. hours[date.hour] += 1
  49.  
  50. #matplotlib.pyplot.plot(hours)
  51. #matplotlib.pyplot.show()
  52.  
  53. # 3.3
  54. tasks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  55. for row in messages:
  56. tasks[int(row[1])] += 1
  57.  
  58. print(tasks)
  59. #matplotlib.pyplot.plot(tasks)
  60. #matplotlib.pyplot.show()
  61.  
  62. # 3.4
  63.  
  64. tasks_per_day = {0: {}, 1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 7: {}, 8: {}, 9: {}}
  65. for row in messages:
  66. tasks_per_day[int(row[1])][parse_time(row[4]).date()] = 0
  67.  
  68. for row in messages:
  69. tasks_per_day[int(row[1])][parse_time(row[4]).date()] += 1
  70.  
  71. # create 10 plots for 10 tasks with number of messages per day from 2023-02-10 to 2023-03-25
  72. for i in range(0, 10):
  73. x = []
  74. y = []
  75. for date in tasks_per_day[i]:
  76. x.append(date)
  77. y.append(tasks_per_day[i][date])
  78. #matplotlib.pyplot.title('3.3')
  79. matplotlib.pyplot.plot(x, y)
  80. matplotlib.pyplot.show()
  81.  
  82. # 3.5
  83. groups = {}
  84. for row in messages:
  85. if row[3] not in groups:
  86. groups[row[3]] = 0
  87.  
  88. for row in messages:
  89. groups[row[3]] += 1
  90.  
  91. # sort groups by number of the group
  92. groups = dict(sorted(groups.items(), key=lambda item: (item[0])))
  93.  
  94. # create plot with number of messages per group
  95. x = []
  96. y = []
  97. for group in groups:
  98. x.append(group)
  99. y.append(groups[group])
  100. #matplotlib.pyplot.figure(figsize=(75, 10))
  101. #matplotlib.pyplot.plot(x, y)
  102. #matplotlib.pyplot.show()
  103.  
  104. # 3.6
  105. groups = {}
  106. for row in messages:
  107. if row[3] not in groups:
  108. groups[row[3]] = 0
  109.  
  110. for row, row1 in zip(messages, checks):
  111. if row1[3] == '3':
  112. groups[row[3]] += 1
  113.  
  114.  
  115. groups = dict(sorted(groups.items(), key=lambda item: (item[0])))
  116.  
  117.  
  118. x = []
  119. y = []
  120. for group in groups:
  121. x.append(group)
  122. y.append(groups[group])
  123. matplotlib.pyplot.figure(figsize=(15, 10))
  124. matplotlib.pyplot.plot(x, y)
  125. matplotlib.pyplot.show()
  126.  
  127. # 3.7
  128. tasks_status_2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  129. tasks_status_all = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  130.  
  131. for mes, ch in zip(messages, checks):
  132. if ch[3] == '2':
  133. tasks_status_2[int(mes[1])] += 1
  134. if ch[3] == '2' or ch[3] == '3':
  135. tasks_status_all[int(mes[1])] += 1
  136.  
  137.  
  138. tasks_status_2_3 = []
  139. for i in range(0, 8):
  140. tasks_status_2_3.append(tasks_status_all[i] / tasks_status_2[i])
  141.  
  142. print(tasks_status_2)
  143. print(tasks_status_all)
  144. x = []
  145. y = []
  146. for i in range(0, 8):
  147. x.append(i+1)
  148. y.append(tasks_status_2_3[i])
  149. matplotlib.pyplot.figure(figsize=(15, 10))
  150. matplotlib.pyplot.plot(x, y)
  151. matplotlib.pyplot.show()
  152.  
  153. # 3.8
  154.  
  155. groups = {}
  156.  
  157. for row in statuses:
  158. if row[2] not in groups:
  159. groups[row[2]] = 0
  160. if row[4] == '2':
  161. groups[row[2]] += len(convert_string_to_list(row[5]))
  162. #print(len(convert_string_to_list(row[5])))
  163.  
  164. # sort groups by number of the group
  165. groups = dict(sorted(groups.items(), key=lambda item: (item[0])))
  166.  
  167. # create plot with number of messages per group
  168. x = []
  169. y = []
  170. for group in groups:
  171. x.append(group)
  172. y.append(groups[group])
  173. matplotlib.pyplot.figure(figsize=(75, 10))
  174. matplotlib.pyplot.plot(x, y)
  175. matplotlib.pyplot.show()
  176.  
  177. # 3.9
  178. students = {}
  179. for row in statuses:
  180. if row[2] + " вариант: " + row[1] not in students:
  181. students[row[2] + " вариант: " + row[1]] = 0
  182. if row[4] == '2':
  183. students[row[2] + " вариант: " + row[1]] += len(convert_string_to_list(row[5]))
  184.  
  185. students = dict(sorted(students.items(), key=lambda item: (item[1]), reverse=True))
  186.  
  187. # create plot with number of messages per first 10 students
  188. x = []
  189. y = []
  190. i = 0
  191. for student in students:
  192. if i == 10:
  193. break
  194. x.append(student)
  195. y.append(students[student])
  196. i += 1
  197. matplotlib.pyplot.figure(figsize=(50, 10))
  198. matplotlib.pyplot.plot(x, y)
  199. matplotlib.pyplot.show()
  200.  
  201. # 3.10
  202. groups = {}
  203. for row in statuses:
  204. if row[2] not in groups:
  205. groups[row[2]] = 0
  206. if row[4] == '2' and len(convert_string_to_list(row[5])) > 1:
  207. groups[row[2]] += 1
  208.  
  209. groups = dict(sorted(groups.items(), key=lambda item: (item[0])))
  210. x = []
  211. y = []
  212. for group in groups:
  213. x.append(group)
  214. y.append(groups[group])
  215. matplotlib.pyplot.figure(figsize=(75, 10))
  216. matplotlib.pyplot.plot(x, y)
  217. matplotlib.pyplot.show()
  218.  
  219.  
  220. # 3.11
  221.  
  222. years = {}
  223. # split games array by ;
  224. games = [game[0].split(';') for game in games]
  225. for game in games:
  226. if game[3] not in years:
  227. years[game[3]] = 0
  228. years[game[3]] += 1
  229.  
  230. years = dict(sorted(years.items(), key=lambda item: (item[0])))
  231. years.popitem()
  232. x = []
  233. y = []
  234. for year in years:
  235. year = year[1:-1]
  236. x.append(year)
  237. y.append(years['"'+year+'"'])
  238.  
  239. matplotlib.pyplot.figure(figsize=(20, 10))
  240. matplotlib.pyplot.plot(x, y)
  241. matplotlib.pyplot.show()
  242.  
  243. # 3.12
  244. genres = {}
  245. games = [game[0].split(';') for game in games]
  246. for game in games:
  247. if game[1] not in genres:
  248. genres[game[1]] = {}
  249. if game[3] not in genres[game[1]]:
  250. genres[game[1]][game[3]] = 0
  251. genres[game[1]][game[3]] += 1
  252.  
  253. # create plot for each genre
  254.  
  255. matplotlib.pyplot.figure(figsize=(20, 10))
  256.  
  257.  
  258. for genre in genres:
  259. if '"не издана"' in genres[genre]:
  260. genres[genre].pop('"не издана"')
  261. x = []
  262. y = []
  263. for year in genres[genre]:
  264. year = year[1:-1]
  265. x.append(year)
  266. y.append(genres[genre]['"'+year+'"'])
  267. matplotlib.pyplot.plot(x, y, label=genre)
  268.  
  269. matplotlib.pyplot.legend()
  270. matplotlib.pyplot.show()
  271.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement