Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions 2025/Tutorials/Exercise05/Exercise05_09_NOI_medals.ipynb

This file was deleted.

106 changes: 106 additions & 0 deletions 2025/Tutorials/Exercise05/Jovan_Exercise05_09_NOI_medals.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "4e6a874f",
"metadata": {},
"source": [
"## Exercise 5.9\n",
"\n",
"A text file, `NOI_24_RESULT.txt` contains the results of the medallist recipients for the 24th National Olympiad in Informatics.\n",
"\n",
"Write a code to:\n",
"\n",
"- read the data from the text file,\n",
"- store the school name and medal counts in a Python dictionary `medal_count` where the school name is the key and the associated value is a tuple of the form `(GOLD, SILVER, BRONZE)`,\n",
"- print out the top 5 school with the highest number of medals won, regardless of the medal type. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "29381517",
"metadata": {},
"outputs": [
{
"ename": "FileNotFoundError",
"evalue": "[Errno 2] No such file or directory: './resources/NOI_24_RESULT.csv'",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mFileNotFoundError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[1]\u001b[39m\u001b[32m, line 9\u001b[39m\n\u001b[32m 5\u001b[39m Bronze = []\n\u001b[32m 7\u001b[39m amount_of_medals_school = []\n\u001b[32m----> \u001b[39m\u001b[32m9\u001b[39m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43m./resources/NOI_24_RESULT.csv\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m f:\n\u001b[32m 10\u001b[39m s = csv.reader(f, delimiter=\u001b[33m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[33m\"\u001b[39m)\n\u001b[32m 11\u001b[39m list_of_medals = [row \u001b[38;5;28;01mfor\u001b[39;00m row \u001b[38;5;129;01min\u001b[39;00m s \u001b[38;5;28;01mif\u001b[39;00m row != []]\n",
"\u001b[36mFile \u001b[39m\u001b[32m~/.local/lib/python3.12/site-packages/IPython/core/interactiveshell.py:325\u001b[39m, in \u001b[36m_modified_open\u001b[39m\u001b[34m(file, *args, **kwargs)\u001b[39m\n\u001b[32m 318\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m {\u001b[32m0\u001b[39m, \u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m}:\n\u001b[32m 319\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[32m 320\u001b[39m \u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mIPython won\u001b[39m\u001b[33m'\u001b[39m\u001b[33mt let you open fd=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m by default \u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 321\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 322\u001b[39m \u001b[33m\"\u001b[39m\u001b[33myou can use builtins\u001b[39m\u001b[33m'\u001b[39m\u001b[33m open.\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 323\u001b[39m )\n\u001b[32m--> \u001b[39m\u001b[32m325\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mio_open\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[31mFileNotFoundError\u001b[39m: [Errno 2] No such file or directory: './resources/NOI_24_RESULT.csv'"
]
}
],
"source": [
"import csv\n",
"\n",
"Gold = []\n",
"Silver = []\n",
"Bronze = []\n",
"\n",
"amount_of_medals_school = []\n",
"\n",
"with open(\"./resources/NOI_24_RESULT.csv\") as f:\n",
" s = csv.reader(f, delimiter=\"\\n\")\n",
" list_of_medals = [row for row in s if row != []]\n",
" \n",
" gold_start = list_of_medals.index(['Gold']) + 1\n",
" silver_start = list_of_medals.index(['Silver'])\n",
" bronze_start = list_of_medals.index(['Bronze'])\n",
"\n",
" Gold = [row[0] for row in list_of_medals[gold_start:silver_start]]\n",
" Silver = [row[0] for row in list_of_medals[silver_start + 1:bronze_start]]\n",
" Bronze = [row[0] for row in list_of_medals[bronze_start + 1:]]\n",
"\n",
" for i in Gold:\n",
" _, school = i.split(',', 1)\n",
" amount_of_medals_school.append([item.strip() for item in school.split(',')][-1])\n",
" \n",
" for i in Silver:\n",
" _, school = i.split(',', 1)\n",
" amount_of_medals_school.append([item.strip() for item in school.split(',')][-1])\n",
" \n",
" for i in Bronze:\n",
" _, school = i.split(',', 1)\n",
" amount_of_medals_school.append([item.strip() for item in school.split(',')][-1])\n",
" \n",
" medal_count = {i : amount_of_medals_school.count(i) for i in amount_of_medals_school}\n",
" \n",
" print(\"Top 5 schools with most medals:\")\n",
" for i in range(len(medal_count)):\n",
" s = max(medal_count, key=medal_count.get)\n",
" if s == 'Singapore':\n",
" s = 'School Of Science And Technology, Singapore'\n",
" s_amt = max(medal_count.values())\n",
" print(f\"{s}, with {s_amt} medals\")\n",
" s = max(medal_count, key=medal_count.get)\n",
" medal_count.pop(s)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}