{"metadata":{"orig_nbformat":4,"kernelspec":{"name":"python3","display_name":"Python 3 (ipykernel)","language":"python"},"language_info":{"name":"python","version":"3.9.7","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Programming and Data Analysis\n\n> Midterm\n\nKuo, Yao-Jen <yaojenkuo@ntu.edu.tw> from [DATAINPOINT](https://www.datainpoint.com)","metadata":{"deletable":false,"editable":false}},{"cell_type":"code","source":"import json","metadata":{"deletable":false,"editable":false},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Instructions\n\n- The assignment will be disconnected if idling over 10 minutes, we can reactivate a new session by clicking the assignment link again.\n- We've imported necessary modules at the top of each assignment.\n- We've put necessary files(if any) in the working directory.\n- We've defined the names of functions/inputs/parameters for you.\n- Write down your solution between the comments `### BEGIN SOLUTION` and `### END SOLUTION`.\n- It is NECESSARY to `return` the answer, tests will fail by just printing out the answer.\n- It is known that `SyntaxError` and `IndentationError` might break our `test_runner.py` and results in a zero point grade. It is highly recommended testing your solution by calling functions/methods in notebook or running tests before submission.\n- Running tests to see if your solutions are right:\n    - File -> Save Notebook to save exercises.ipynb.\n    - File -> New -> Terminal to open a Terminal.\n    - Use command `python test_runner.py` to run test.\n- When you are ready to submit, click File -> Export Notebook As -> Executable Script.\n- Double check your executable script is truly an executable Python script.\n- Rename the exported Python script with your student ID(e.g. `b01234567.py`) and upload to the Assignment session on NTU COOL/NTNU Moodle.","metadata":{"deletable":false,"editable":false}},{"cell_type":"markdown","source":"## 01. Define a function named `flip_dict_key_value_pairs` which flips a given dictionary's key-value pairs.\n\n- Expected inputs: `dict`.\n- Expected outputs: `dict`.","metadata":{}},{"cell_type":"code","source":"def flip_dict_key_value_pairs(x: dict) -> dict:\n    \"\"\"\n    >>> flip_dict_key_value_pairs({'TWN': 'Taiwan'})\n    {'Taiwan': 'TWN'}\n    >>> flip_dict_key_value_pairs({'TWN': 'Taiwan', 'JPN': 'Japan', 'LTU': 'Lithuania', 'SVN': 'Slovenia'})\n    {'Taiwan': 'TWN', 'Japan': 'JPN', 'Lithuania': 'LTU', 'Slovenia': 'SVN'}\n    \"\"\"\n    ### BEGIN SOLUTION\n    x = {value:key for key, value in x.items()}\n    return x\n    ### END SOLUTION","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 02. Define a class named `RangePrime` which instantiates objects with an attribute `range_list` and a method `filter_primes()`.\n\n- Expected inputs: `int`.\n- Expected outputs: `list`.","metadata":{}},{"cell_type":"code","source":"class RangePrime:\n    \"\"\"\n    >>> range_prime = RangePrime(1, 5)\n    >>> range_prime.range_list\n    [1, 2, 3, 4, 5]\n    >>> range_prime.filter_primes()\n    [2, 3, 5]\n    >>> range_prime = RangePrime(6, 15)\n    >>> range_prime.range_list\n    [6, 7, 8, 9, 10, 11, 12, 13, 14, 15]\n    >>> range_prime.filter_primes()\n    [7, 11, 13]\n    \"\"\"\n    ### BEGIN SOLUTION\n    def __init__(self, a,b):\n        self.a = a\n        self.b = b\n        \n    def range_list(self,a,b):\n        a = self.a\n        b = self.b\n        x = []\n        if b < a:\n            x.extend(range(b, a))\n            x.append(a)\n        else:\n            x.extend(range(a, b))\n            x.append(b)\n        return x\n\n    def filter_primes(self, x=None):\n        if x is not None:\n            self.x = x \n        else:\n            self.x = self.range_list() \n\n        primes = []\n        for possiblePrime in self.x:\n            isPrime = True\n            for num in range(2, possiblePrime):\n                if possiblePrime % num == 0:\n                    isPrime = False\n            if isPrime:\n                primes.append(possiblePrime)\n                if 1 in primes:\n                    primes.remove(1)\n        return primes  \n            \n    ### END SOLUTION","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 03. Define a class named `MinMaxFinder` which instantiates objects with 4 methods `get_min()`, `get_max()`, `get_idxmin()`, and `get_idxmax()` that is able to find the minimum and maximum values/indexes given a `list`. \n\n- Expected inputs: `list`.\n- Expected outputs: `int` | `list`.","metadata":{}},{"cell_type":"code","source":"class MinMaxFinder:\n    \"\"\"\n    >>> min_max_finder = MinMaxFinder([2, 3, 5, 7, 11])\n    >>> min_max_finder.get_min()\n    2\n    >>> min_max_finder.get_max()\n    11\n    >>> min_max_finder.get_idxmin()\n    [0]\n    >>> min_max_finder.get_idxmax()\n    [4]\n    >>> min_max_finder = MinMaxFinder([2, 2, 3, 5, 7, 11, 11])\n    >>> min_max_finder.get_min()\n    2\n    >>> min_max_finder.get_max()\n    11\n    >>> min_max_finder.get_idxmin()\n    [0, 1]\n    >>> min_max_finder.get_idxmax()\n    [5, 6]\n    \"\"\"\n    ### BEGIN SOLUTION\n    def __int__(self,*args):\n        self.*args = *args\n    def get_min(self):\n        return min(*args)\n    def get_max(self):\n        return max(*args)\n    def get_idxmin(self):\n        return self.index(min(*agrs))\n    def get_idxmax(self):\n        return self.index(max(*agrs))\n    \n    ### END SOLUTION","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 04. Define a class named `PigLatin` which instantiates objects performing the Pig Latin language game with 1 attribute `original_word` and 1 method `alter()`.\n\n- For words that begin with consonant sounds, all letters before the initial vowel are placed at the end of the word sequence. Then, \"ay\" is added. e.g. \"pig\" -> \"igpay\". When words begin with consonant clusters (multiple consonants that form one sound), the whole sound is added to the end. e.g. \"smile\" -> \"ilesmay\"\n- For words that begin with vowel sounds, the vowel is left alone, and \"yay\" is added to the end. e.g. \"eat\" -> \"eatyay\"\n\nSource: <https://en.wikipedia.org/wiki/Pig_Latin>\n\n- Expected inputs: `str`.\n- Expected outputs: `str`.","metadata":{}},{"cell_type":"code","source":"class PigLatin:\n    \"\"\"\n    >>> pig_latin = PigLatin(\"pig\")\n    >>> pig_latin.original_word\n    'pig'\n    >>> pig_latin.alter()\n    'igpay'\n    >>> pig_latin = PigLatin(\"smile\")\n    >>> pig_latin.original_word\n    'smile'\n    >>> pig_latin.alter()\n    'ilesmay'\n    >>> pig_latin = PigLatin(\"eat\")\n    >>> pig_latin.original_word\n    'eat'\n    >>> pig_latin.alter()\n    'eatyay'\n    \"\"\"\n    ### BEGIN SOLUTION\n    \n    ### END SOLUTION","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 05. Define a class named `RotThirteen` which instantiates objects performing the ROT13 cipher with 2 methods `rotate_char()` and `rotate_sentence()`.\n\nSource: <https://en.wikipedia.org/wiki/ROT13>\n\n- Expected inputs: `str`.\n- Expected outputs: `str`.","metadata":{}},{"cell_type":"code","source":"class RotThirteen:\n    \"\"\"\n    >>> rot_13 = RotThirteen()\n    >>> rot_13.rotate_char(\"A\")\n    'N'\n    >>> rot_13.rotate_char(\"B\")\n    'O'\n    >>> rot_13.rotate_char(\"Y\")\n    'L'\n    >>> rot_13.rotate_char(\"Z\")\n    'M'\n    >>> rot_13.rotate_char(\"a\")\n    'n'\n    >>> rot_13.rotate_char(\"b\")\n    'o'\n    >>> rot_13.rotate_char(\"y\")\n    'l'\n    >>> rot_13.rotate_char(\"z\")\n    'm'\n    >>> rot_13.rotate_sentence(\"Ornhgvshy vf orggre guna htyl.\")\n    'Beautiful is better than ugly.'\n    >>> rot_13.rotate_sentence(\"Abj vf orggre guna arire.\")\n    'Now is better than never.'\n    \"\"\"\n    ### BEGIN SOLUTION\n    \n    ### END SOLUTION","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 06. Define a function named `lookup_zip_codes` which returns the 3-digit zip code given the name of county and town, based on `zip_codes.json` in working directory.\n\n- Expected inputs: `str`.\n- Expected outputs: `str`.","metadata":{}},{"cell_type":"code","source":"def lookup_zip_codes(county: str, town: str) -> str:\n    \"\"\"\n    >>> lookup_zip_codes(\"臺北市\", \"中正區\")\n    '100'\n    >>> lookup_zip_codes(\"基隆市\", \"中正區\")\n    '202'\n    >>> lookup_zip_codes(\"臺北市\", \"中山區\")\n    '104'\n    >>> lookup_zip_codes(\"基隆市\", \"中山區\")\n    '203'\n    >>> lookup_zip_codes(\"臺北市\", \"大安區\")\n    '106'\n    >>> lookup_zip_codes(\"臺中市\", \"大安區\")\n    '439'\n    \"\"\"\n    ### BEGIN SOLUTION\n    \n    ### END SOLUTION","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 07. Define a function named `lookup_country_iso_codes` which returns the 2-digit and 3-digit ISO country code given the name of country, based on `countries.json` in working directory.\n\n- Expected inputs: `str`.\n- Expected outputs: `tuple`.","metadata":{}},{"cell_type":"code","source":"def lookup_country_iso_codes(country: str) -> tuple:\n    \"\"\"\n    >>> lookup_country_iso_codes(\"Taiwan\")\n    ('TW', 'TWN')\n    >>> lookup_country_iso_codes(\"Japan\")\n    ('JP', 'JPN')\n    >>> lookup_country_iso_codes(\"Lithuania\")\n    ('LT', 'LTU')\n    >>> lookup_country_iso_codes(\"Slovenia\")\n    ('SI', 'SVN')\n    \"\"\"\n    ### BEGIN SOLUTION\n    \n    ### END SOLUTION","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 08. Define a class named `CoachRoster` instantiates objects with 2 methods `get_head_coach()` and `get_assistant_coaches()` returns the coach roster of NBA's standard league given a team's tricode based on `coaches.json` in working directory.\n\n- Expected inputs: `str`.\n- Expected outputs: `str` | `list`.","metadata":{}},{"cell_type":"code","source":"class CoachRoster:\n    \"\"\"\n    >>> cr = CoachRoster(\"coaches.json\")\n    >>> cr.get_head_coach(\"ATL\")\n    'Nate McMillan'\n    >>> cr.get_assistant_coaches(\"ATL\")\n    ['Chris Jent',\n     'Joe Prunty',\n     'Jamelle McMillan',\n     'Matt Hill',\n     'Nick Van Exel',\n     'Scottie Parker']\n    >>> cr.get_head_coach(\"BKN\")\n    'Steve Nash'\n    >>> cr.get_assistant_coaches(\"BKN\")\n    ['Jacque Vaughn',\n     'David Vanterpool',\n     'Brian Keefe',\n     'Adam Harrington',\n     'Jordan Ott',\n     'Tiago Splitter',\n     'Royal Ivey',\n     'Ryan Forehan-Kelly',\n     'Sebastien Poirier']\n    \"\"\"\n    ### BEGIN SOLUTION\n    \n    ### END SOLUTION","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 09. Define a function named `find_tallest_shortest_players` which returns the tallest and the shortest NBA standard league players based on `players.json` in working directory.\n\nPS. Skip those players whose `heightMeters` is an empty string.\n\n- Expected inputs: `str`.\n- Expected outputs: `dict`.","metadata":{}},{"cell_type":"code","source":"def find_tallest_shortest_players(x: str) -> dict:\n    \"\"\"\n    >>> find_tallest_shortest_players('tallest')\n    {'heightMeters': 2.29, 'players': ['Tacko Fall']}\n    >>> find_tallest_shortest_players('shortest')\n    {'heightMeters': 1.78, 'players': ['Facundo Campazzo', 'Markus Howard']}\n    \"\"\"\n    ### BEGIN SOLUTION\n    \n    ### END SOLUTION","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 10. Define a function named `get_current_team_roster` which returns the current roster of 2021-2022 season for every NBA standard league team based on the `teamId` attribute from `players.json` and `teams.json` in working directory.\n\n- Expected inputs: `str`.\n- Expected outputs: `list`.","metadata":{}},{"cell_type":"code","source":"def get_current_team_roster(team_tricode: str) -> list:\n    \"\"\"\n    >>> get_current_team_roster(\"BKN\")\n    ['LaMarcus Aldridge',\n     \"DeAndre' Bembry\",\n     'Bruce Brown',\n     'Jevon Carter',\n     'Nic Claxton',\n     'David Duke Jr.',\n     'Kevin Durant',\n     'Kessler Edwards',\n     'Blake Griffin',\n     'James Harden',\n     'Joe Harris',\n     'Kyrie Irving',\n     'James Johnson',\n     'Patty Mills',\n     'Paul Millsap',\n     \"Day'Ron Sharpe\",\n     'Cam Thomas']\n    >>> get_current_team_roster(\"MIA\")\n    ['Bam Adebayo',\n     'Jimmy Butler',\n     'Dewayne Dedmon',\n     'Marcus Garrett',\n     'Udonis Haslem',\n     'Tyler Herro',\n     'Kyle Lowry',\n     'Caleb Martin',\n     'Markieff Morris',\n     'KZ Okpala',\n     'Victor Oladipo',\n     'Duncan Robinson',\n     'Max Strus',\n     'P.J. Tucker',\n     'Gabe Vincent',\n     'Omer Yurtseven']\n    \"\"\"\n    ### BEGIN SOLUTION\n    \n    ### END SOLUTION","metadata":{},"execution_count":null,"outputs":[]}]}