No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

218 líneas
7.2 KiB

  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "id": "b59dad38",
  7. "metadata": {},
  8. "outputs": [],
  9. "source": [
  10. "import pandas as pd\n",
  11. "\n",
  12. "\n",
  13. "class FileTypeError(TypeError):\n",
  14. " pass\n",
  15. "\n",
  16. "\n",
  17. "class ContestScore:\n",
  18. " # for eval(cur_info) in gen_max_cases_\n",
  19. " global null\n",
  20. " null = ''\n",
  21. "\n",
  22. " def __init__(self, ):\n",
  23. " self.rank_path = ''\n",
  24. " self.submission_path = ''\n",
  25. " self.problem_path = ''\n",
  26. " self.ranks_df = None\n",
  27. " self.submission_df = None\n",
  28. " self.problem_df = None\n",
  29. " self.problem_names = []\n",
  30. " self.problem_ids = []\n",
  31. " self.contest_score = None\n",
  32. "\n",
  33. " # for calculate max cases\n",
  34. " self.target_problem = 0\n",
  35. " self.gen_all_cases = True\n",
  36. "\n",
  37. " def load_data(self, rank_path, submission_path, problem_path):\n",
  38. " # load data\n",
  39. " self.ranks_df = ContestScore.read_dataframe(rank_path)\n",
  40. " self.submission_df = ContestScore.read_dataframe(submission_path)\n",
  41. " self.rank_path = rank_path\n",
  42. " self.submission_path = submission_path\n",
  43. " rank_columns = self.ranks_df.columns.tolist()\n",
  44. " self.problem_names = rank_columns[6:]\n",
  45. "\n",
  46. " # process ranks\n",
  47. " drop_data = ['User ID', 'Real Name', 'Total Submission'] + self.problem_names\n",
  48. " self.contest_score = self.ranks_df.drop(axis=1, columns=drop_data)\n",
  49. "\n",
  50. " # calculate max cases\n",
  51. " self.contest_score['通过用例数'] = self.contest_score.apply(self.gen_max_cases, axis=1)\n",
  52. "\n",
  53. " # get problem_names and problem_ids\n",
  54. " # calculate problem cases\n",
  55. " if problem_path:\n",
  56. " self.get_problems(problem_path)\n",
  57. " self.calculate()\n",
  58. " else:\n",
  59. " print(\"Please Set Problem names and ids by set_problems(names, ids) function\")\n",
  60. "\n",
  61. " # sort scores\n",
  62. " self.sort()\n",
  63. "\n",
  64. " def get_problems(self, problem_path):\n",
  65. " self.problem_df = ContestScore.read_dataframe(problem_path)\n",
  66. " self.problem_names = self.problem_df.title.to_list()\n",
  67. " self.problem_ids = self.problem_df.id.to_list()\n",
  68. " self.problem_path = problem_path\n",
  69. "\n",
  70. " def gen_max_cases(self, df):\n",
  71. " self.gen_all_cases = True\n",
  72. " return self.gen_max_cases_(df)\n",
  73. "\n",
  74. " def set_problems(self, problem_names, problem_ids):\n",
  75. " self.problem_names = problem_names\n",
  76. " self.problem_ids = problem_ids\n",
  77. "\n",
  78. " def calculate(self):\n",
  79. " # calculate max cases for certain problems\n",
  80. " self.gen_all_cases = False\n",
  81. " for index, p_id in enumerate(self.problem_ids):\n",
  82. " self.target_problem = p_id\n",
  83. " p_name = self.problem_names[index]\n",
  84. " self.contest_score[p_name] = self.contest_score.apply(self.gen_max_cases_problem, axis=1)\n",
  85. "\n",
  86. " def gen_max_cases_problem(self, df):\n",
  87. " self.gen_all_cases = False\n",
  88. " return self.gen_max_cases_(df)\n",
  89. "\n",
  90. " def gen_max_cases_(self, df):\n",
  91. " cur_max_cases = {}\n",
  92. " for i in range(len(self.submission_df)):\n",
  93. " if str(self.submission_df.iloc[i]['username']) == str(df['Username']) \\\n",
  94. " and (self.gen_all_cases or self.target_problem == self.submission_df.iloc[i]['problem_id']):\n",
  95. " try:\n",
  96. " cur_info = self.submission_df.iloc[i]['info']\n",
  97. " cur_problem_id = self.submission_df.iloc[i]['problem_id']\n",
  98. " cur_score = sum(list(map(lambda x: x['result'] == 0, eval(cur_info)['data'])))\n",
  99. "\n",
  100. " if not (cur_problem_id in cur_max_cases.keys()):\n",
  101. " cur_max_cases[cur_problem_id] = cur_score\n",
  102. " elif cur_score > cur_max_cases[cur_problem_id]:\n",
  103. " cur_max_cases[cur_problem_id] = cur_score\n",
  104. " except:\n",
  105. " continue\n",
  106. " return sum(cur_max_cases.values())\n",
  107. "\n",
  108. " def sort(self, by=['AC', '通过用例数', 'Total Time'], ascending=[False, False, True]):\n",
  109. " self.contest_score = self.contest_score.sort_values(by=by, ascending=ascending)\n",
  110. " return self.contest_score\n",
  111. "\n",
  112. " def save(self, path):\n",
  113. " self.contest_score.to_excel(path, index=False)\n",
  114. " return f\"File Saved at {path} as Excel\"\n",
  115. "\n",
  116. " @staticmethod\n",
  117. " def read_dataframe(path):\n",
  118. " filetype = path.split('.')[-1]\n",
  119. " dataframe = None\n",
  120. " if filetype == 'csv':\n",
  121. " dataframe = pd.read_csv(path)\n",
  122. " elif filetype == 'xlsx':\n",
  123. " dataframe = pd.read_excel(path)\n",
  124. " else:\n",
  125. " FileTypeError(filetype)\n",
  126. " return dataframe"
  127. ]
  128. },
  129. {
  130. "cell_type": "code",
  131. "execution_count": 2,
  132. "id": "09049d66",
  133. "metadata": {},
  134. "outputs": [],
  135. "source": [
  136. "test = ContestScore()"
  137. ]
  138. },
  139. {
  140. "cell_type": "code",
  141. "execution_count": 3,
  142. "id": "73373376",
  143. "metadata": {},
  144. "outputs": [
  145. {
  146. "name": "stdout",
  147. "output_type": "stream",
  148. "text": [
  149. "\u001b[0m\u001b[38;5;167m机试成绩导出.ipynb\u001b[0m \u001b[38;5;78mproblem_202207021234.csv\u001b[0m\r\n",
  150. "\u001b[38;5;33;1mcontent-68-rank.xlsx\u001b[0m \u001b[38;5;78msubmission_202207021235.csv\u001b[0m\r\n"
  151. ]
  152. }
  153. ],
  154. "source": [
  155. "%ls"
  156. ]
  157. },
  158. {
  159. "cell_type": "code",
  160. "execution_count": 4,
  161. "id": "2b0cedc1",
  162. "metadata": {
  163. "scrolled": true
  164. },
  165. "outputs": [
  166. {
  167. "data": {
  168. "text/plain": [
  169. "'File Saved at Result.xlsx as Excel'"
  170. ]
  171. },
  172. "execution_count": 4,
  173. "metadata": {},
  174. "output_type": "execute_result"
  175. }
  176. ],
  177. "source": [
  178. "# load data and calculate scores , ranks automatically\n",
  179. "test.load_data(rank_path='./content-68-rank.xlsx', submission_path='./submission_202207021235.csv', problem_path='./problem_202207021234.csv')\n",
  180. "\n",
  181. "# retun the result by a pandas DataFrame\n",
  182. "test.contest_score\n",
  183. "\n",
  184. "# Save result as excel\n",
  185. "test.save('Result.xlsx')"
  186. ]
  187. },
  188. {
  189. "cell_type": "code",
  190. "execution_count": null,
  191. "id": "50e6428e",
  192. "metadata": {},
  193. "outputs": [],
  194. "source": []
  195. }
  196. ],
  197. "metadata": {
  198. "kernelspec": {
  199. "display_name": "Python 3 (ipykernel)",
  200. "language": "python",
  201. "name": "python3"
  202. },
  203. "language_info": {
  204. "codemirror_mode": {
  205. "name": "ipython",
  206. "version": 3
  207. },
  208. "file_extension": ".py",
  209. "mimetype": "text/x-python",
  210. "name": "python",
  211. "nbconvert_exporter": "python",
  212. "pygments_lexer": "ipython3",
  213. "version": "3.7.0"
  214. }
  215. },
  216. "nbformat": 4,
  217. "nbformat_minor": 5
  218. }