{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Topic 8: Practice problems (NumPy arrays and indexing)\n",
    "\n",
    "Asiman Data School, Module 1: Foundations of Python Programming. Week 4, Thursday.\n",
    "\n",
    "Fifteen problems in three levels, on the same ideas and at the same level as the lesson notebook.\n",
    "\n",
    "How to work\n",
    "\n",
    "- Write your code in the cell under each problem. The lines that are already in the cell read the input for you. Where a problem asks for a function, write it above those lines; until it is written, the cell stops with a NameError, and that is expected.\n",
    "- When the cell runs, a small box appears. Type the value there and press Enter. Several numbers on one line are typed with spaces between them.\n",
    "- Run your code with every row of the examples table and compare the output. Print exactly what the problem asks for.\n",
    "- A hint and one possible solution are under each problem. Open them only after you have tried.\n",
    "\n",
    "Tip: For every problem, first write down the shape of the array you need. For a 2D array, say out loud \"row, then column\" before you write the indexes."
   ],
   "id": "t08p-000"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Easy"
   ],
   "id": "t08p-001"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 1. First and last\n",
    "\n",
    "Create an array with the numbers from 0 to 9 using `arange`, and print the first and the last element on one line."
   ],
   "id": "t08p-002"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t08p-003"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`np.arange(10)` stops before 10. The first element is `a[0]` and the last one is `a[-1]`. One `print` with a comma puts both on one line.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "a = np.arange(10)\n",
    "print(a[0], a[-1])\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "0 9\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t08p-004"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 2. Zeros with one value\n",
    "\n",
    "Create an array of 6 zeros with the type `int`. Change the element at index 2 to 7 and print the array."
   ],
   "id": "t08p-005"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t08p-006"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`np.zeros(6, dtype=int)` makes whole numbers. Then `a[2] = 7`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "a = np.zeros(6, dtype=int)\n",
    "a[2] = 7\n",
    "print(a)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "[0 0 7 0 0 0]\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t08p-007"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 3. An array from input\n",
    "\n",
    "Read a line of numbers and turn it into an array. Print the array and its largest element on one line.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `4 8 15 16` | `[ 4  8 15 16] 16` |\n",
    "| `7` | `[7] 7` |\n",
    "| `-3 -9 -1` | `[-3 -9 -1] -1` |\n",
    "| `5 5 5` | `[5 5 5] 5` |\n",
    "| `10 2 33 4 15` | `[10  2 33  4 15] 33` |"
   ],
   "id": "t08p-008"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "4 8 15 16"
     ]
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "numbers = list(map(int, input().split()))\n",
    "# Write your code here"
   ],
   "id": "t08p-009"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`np.array(numbers)` turns the list into an array. The largest element is `a.max()`. Print both with one `print(a, a.max())`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "numbers = list(map(int, input().split()))\n",
    "a = np.array(numbers)\n",
    "print(a, a.max())\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `4 8 15 16` | `[ 4  8 15 16] 16` |\n",
    "| `7` | `[7] 7` |\n",
    "| `-3 -9 -1` | `[-3 -9 -1] -1` |\n",
    "| `5 5 5` | `[5 5 5] 5` |\n",
    "| `10 2 33 4 15` | `[10  2 33  4 15] 33` |\n",
    "\n",
    "</details>"
   ],
   "id": "t08p-010"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 4. Shape and one element\n",
    "\n",
    "Create the array `[[1, 2], [3, 4], [5, 6]]` and print its `shape`, its number of rows and the element in row 2, column 1, each on its own line."
   ],
   "id": "t08p-011"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t08p-012"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`len(table)` or `table.shape[0]` is the number of rows. Row 2, column 1 is `table[2, 1]`: counting starts at 0.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "table = np.array([[1, 2], [3, 4], [5, 6]])\n",
    "print(table.shape)\n",
    "print(len(table))\n",
    "print(table[2, 1])\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "(3, 2)\n",
    "3\n",
    "6\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t08p-013"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Medium"
   ],
   "id": "t08p-014"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 5. Three rows and four columns\n",
    "\n",
    "Create the numbers 1 to 12 and reshape them into 3 rows and 4 columns. Print the table and the element in the last row and the last column."
   ],
   "id": "t08p-015"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t08p-016"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`np.arange(1, 13)` gives 1 to 12. Then `.reshape(3, 4)`. The last row and the last column: `table[-1, -1]`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "table = np.arange(1, 13).reshape(3, 4)\n",
    "print(table)\n",
    "print(table[-1, -1])\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "[[ 1  2  3  4]\n",
    " [ 5  6  7  8]\n",
    " [ 9 10 11 12]]\n",
    "12\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t08p-017"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 6. The middle element\n",
    "\n",
    "Read a line of numbers into an array. Print the element in the middle (for an even size, the one right after the middle).\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `10 20 30 40 50` | `30` |\n",
    "| `10 20 30 40` | `30` |\n",
    "| `7` | `7` |\n",
    "| `1 2` | `2` |\n",
    "| `5 3 8 1 9 2` | `1` |"
   ],
   "id": "t08p-018"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "10 20 30 40 50"
     ]
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array(list(map(int, input().split())))\n",
    "# Write your code here"
   ],
   "id": "t08p-019"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The middle index is `a.size // 2`. For 5 elements that is index 2, for 4 elements it is also index 2.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "a = np.array(list(map(int, input().split())))\n",
    "print(a[a.size // 2])\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `10 20 30 40 50` | `30` |\n",
    "| `10 20 30 40` | `30` |\n",
    "| `7` | `7` |\n",
    "| `1 2` | `2` |\n",
    "| `5 3 8 1 9 2` | `1` |\n",
    "\n",
    "</details>"
   ],
   "id": "t08p-020"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 7. The lowest grade\n",
    "\n",
    "Given `names = np.array([\"Aylin\", \"Murad\", \"Leyla\", \"Kamran\"])` and `grades = np.array([85, 92, 78, 45])`, print the name of the student with the lowest grade."
   ],
   "id": "t08p-021"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "names = np.array([\"Aylin\", \"Murad\", \"Leyla\", \"Kamran\"])\n",
    "grades = np.array([85, 92, 78, 45])\n",
    "# Write your code here"
   ],
   "id": "t08p-022"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`grades.argmin()` gives the index of the lowest grade. Use the same index in `names`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "names = np.array([\"Aylin\", \"Murad\", \"Leyla\", \"Kamran\"])\n",
    "grades = np.array([85, 92, 78, 45])\n",
    "print(names[grades.argmin()])\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "Kamran\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t08p-023"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 8. Elements at chosen indexes\n",
    "\n",
    "Read a line of numbers into an array and a line of indexes. Print the elements at those indexes, as an array.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `10 20 30 40 50, 0 2 4` | `[10 30 50]` |\n",
    "| `10 20 30 40 50, 4 0` | `[50 10]` |\n",
    "| `7 8 9, 1 1 1` | `[8 8 8]` |\n",
    "| `5 6, -1` | `[6]` |\n",
    "| `3 1 4 1 5 9, 5 4 3 2 1 0` | `[9 5 1 4 1 3]` |"
   ],
   "id": "t08p-024"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "10 20 30 40 50",
      "0 2 4"
     ]
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array(list(map(int, input().split())))\n",
    "indexes = list(map(int, input().split()))\n",
    "# Write your code here"
   ],
   "id": "t08p-025"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "A list of indexes goes inside the brackets: `a[indexes]`. Here `indexes` already is a list, so no extra brackets are needed.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "a = np.array(list(map(int, input().split())))\n",
    "indexes = list(map(int, input().split()))\n",
    "print(a[indexes])\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `10 20 30 40 50, 0 2 4` | `[10 30 50]` |\n",
    "| `10 20 30 40 50, 4 0` | `[50 10]` |\n",
    "| `7 8 9, 1 1 1` | `[8 8 8]` |\n",
    "| `5 6, -1` | `[6]` |\n",
    "| `3 1 4 1 5 9, 5 4 3 2 1 0` | `[9 5 1 4 1 3]` |\n",
    "\n",
    "</details>"
   ],
   "id": "t08p-026"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 9. Ones on the diagonal\n",
    "\n",
    "Create a 3 by 3 table of zeros (type `int`) and set the three elements of its diagonal to 1 with indexes. Print the table."
   ],
   "id": "t08p-027"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t08p-028"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The diagonal elements are (0, 0), (1, 1) and (2, 2). Three lines like `table[0, 0] = 1` work, and so does one line with two lists: `table[[0, 1, 2], [0, 1, 2]] = 1`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "table = np.zeros((3, 3), dtype=int)\n",
    "table[[0, 1, 2], [0, 1, 2]] = 1\n",
    "print(table)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "[[1 0 0]\n",
    " [0 1 0]\n",
    " [0 0 1]]\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t08p-029"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Medium-Hard"
   ],
   "id": "t08p-030"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 10. The sum of every column\n",
    "\n",
    "Create the numbers 1 to 20 reshaped into 4 rows and 5 columns. With two loops and indexes, print the sum of every column, one line for each column, like `Column 0 sum: 34`."
   ],
   "id": "t08p-031"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t08p-032"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The outer loop goes over the columns: `for j in range(table.shape[1])`. Inside it, start `total = 0` and add `table[i, j]` for every row `i`. Print with `print(\"Column\", j, \"sum:\", total)`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "table = np.arange(1, 21).reshape(4, 5)\n",
    "for j in range(table.shape[1]):\n",
    "    total = 0\n",
    "    for i in range(table.shape[0]):\n",
    "        total = total + table[i, j]\n",
    "    print(\"Column\", j, \"sum:\", total)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "Column 0 sum: 34\n",
    "Column 1 sum: 38\n",
    "Column 2 sum: 42\n",
    "Column 3 sum: 46\n",
    "Column 4 sum: 50\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t08p-033"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 11. Above the mean\n",
    "\n",
    "Read a line of numbers into an array. With a loop, count how many elements are larger than the mean of the array, and print the count.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `1 2 3 4 5` | `2` |\n",
    "| `10 10 10` | `0` |\n",
    "| `5` | `0` |\n",
    "| `1 100` | `1` |\n",
    "| `-5 0 5 10` | `2` |\n",
    "| `3 9 2 8 7` | `3` |"
   ],
   "id": "t08p-034"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "1 2 3 4 5"
     ]
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array(list(map(int, input().split())))\n",
    "# Write your code here"
   ],
   "id": "t08p-035"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Store `a.mean()` in a variable first. Then loop over the array and add 1 to a counter for every value that is larger.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "a = np.array(list(map(int, input().split())))\n",
    "mean = a.mean()\n",
    "count = 0\n",
    "for value in a:\n",
    "    if value > mean:\n",
    "        count = count + 1\n",
    "print(count)\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `1 2 3 4 5` | `2` |\n",
    "| `10 10 10` | `0` |\n",
    "| `5` | `0` |\n",
    "| `1 100` | `1` |\n",
    "| `-5 0 5 10` | `2` |\n",
    "| `3 9 2 8 7` | `3` |\n",
    "\n",
    "</details>"
   ],
   "id": "t08p-036"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 12. The four corners\n",
    "\n",
    "Create a 4 by 4 table from the numbers 1 to 16. Print its four corner elements as one array, using two lists of indexes."
   ],
   "id": "t08p-037"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t08p-038"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The corners are (0, 0), (0, -1), (-1, 0) and (-1, -1). The first list holds the four row indexes, the second one the four column indexes: `table[[0, 0, -1, -1], [0, -1, 0, -1]]`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "table = np.arange(1, 17).reshape(4, 4)\n",
    "print(table[[0, 0, -1, -1], [0, -1, 0, -1]])\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "[ 1  4 13 16]\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t08p-039"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 13. Swap the two rows\n",
    "\n",
    "Read a line of 6 numbers and reshape them into 2 rows and 3 columns. Swap the two rows. The last line, which is already there, prints both rows on one line.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `1 2 3 4 5 6` | `[4 5 6] [1 2 3]` |\n",
    "| `7 7 7 0 0 0` | `[0 0 0] [7 7 7]` |\n",
    "| `9 8 7 6 5 4` | `[6 5 4] [9 8 7]` |\n",
    "| `-1 0 1 1 0 -1` | `[ 1  0 -1] [-1  0  1]` |\n",
    "| `5 5 5 5 5 5` | `[5 5 5] [5 5 5]` |"
   ],
   "id": "t08p-040"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "1 2 3 4 5 6"
     ]
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array(list(map(int, input().split())))\n",
    "# Write your code here: make table from a, then swap its rows\n",
    "\n",
    "print(table[0], table[1])"
   ],
   "id": "t08p-041"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`table = a.reshape(2, 3)`. Then swap with two lists of rows, as in solved problem 13: `table[[0, 1]] = table[[1, 0]]`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "a = np.array(list(map(int, input().split())))\n",
    "table = a.reshape(2, 3)\n",
    "table[[0, 1]] = table[[1, 0]]\n",
    "print(table[0], table[1])\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `1 2 3 4 5 6` | `[4 5 6] [1 2 3]` |\n",
    "| `7 7 7 0 0 0` | `[0 0 0] [7 7 7]` |\n",
    "| `9 8 7 6 5 4` | `[6 5 4] [9 8 7]` |\n",
    "| `-1 0 1 1 0 -1` | `[ 1  0 -1] [-1  0  1]` |\n",
    "| `5 5 5 5 5 5` | `[5 5 5] [5 5 5]` |\n",
    "\n",
    "</details>"
   ],
   "id": "t08p-042"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 14. The position of the largest element\n",
    "\n",
    "Write a function `largest_position(table)` that returns the row index and the column index of the largest element of a 2D array. Use loops and indexes. If the largest value appears more than once, return its first position, reading row by row. The input is the number of rows and columns, then the numbers of the table.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `2 3, 3 8 5 12 7 10` | `1 0` |\n",
    "| `3 3, 1 2 3 4 5 6 7 8 9` | `2 2` |\n",
    "| `1 4, 5 9 2 9` | `0 1` |\n",
    "| `2 2, -4 -1 -3 -2` | `0 1` |\n",
    "| `3 2, 7 0 0 0 0 0` | `0 0` |\n",
    "| `4 1, 1 2 3 2` | `2 0` |"
   ],
   "id": "t08p-043"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "2 3",
      "3 8 5 12 7 10"
     ]
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Write the function largest_position(table) here\n",
    "\n",
    "rows, columns = map(int, input().split())\n",
    "table = np.array(list(map(int, input().split()))).reshape(rows, columns)\n",
    "row, column = largest_position(table)\n",
    "print(row, column)"
   ],
   "id": "t08p-044"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Keep the best position in two variables that start at `0, 0`. Go through every `i` and `j` with two loops, and when `table[i, j]` is larger than the element at the best position, store `i` and `j`. Return both: `return best_row, best_column`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "def largest_position(table):\n",
    "    best_row, best_column = 0, 0\n",
    "    for i in range(table.shape[0]):\n",
    "        for j in range(table.shape[1]):\n",
    "            if table[i, j] > table[best_row, best_column]:\n",
    "                best_row, best_column = i, j\n",
    "    return best_row, best_column\n",
    "\n",
    "rows, columns = map(int, input().split())\n",
    "table = np.array(list(map(int, input().split()))).reshape(rows, columns)\n",
    "row, column = largest_position(table)\n",
    "print(row, column)\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `2 3, 3 8 5 12 7 10` | `1 0` |\n",
    "| `3 3, 1 2 3 4 5 6 7 8 9` | `2 2` |\n",
    "| `1 4, 5 9 2 9` | `0 1` |\n",
    "| `2 2, -4 -1 -3 -2` | `0 1` |\n",
    "| `3 2, 7 0 0 0 0 0` | `0 0` |\n",
    "| `4 1, 1 2 3 2` | `2 0` |\n",
    "\n",
    "</details>"
   ],
   "id": "t08p-045"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 15. The best student of every exam\n",
    "\n",
    "Given a grade table where each row is a student and each column an exam, print for every exam the index of the student with the best grade. Use loops and indexes. Print one line for each exam, like `Exam 0 best student: 1`."
   ],
   "id": "t08p-046"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "grades = np.array([\n",
    "    [85, 90, 78],\n",
    "    [92, 60, 88],\n",
    "    [70, 75, 95],\n",
    "])\n",
    "# Write your code here"
   ],
   "id": "t08p-047"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The outer loop goes over the exams (columns). Inside it, start with `best = 0` and compare `grades[i, j]` with `grades[best, j]` for every student `i`. Print with `print(\"Exam\", j, \"best student:\", best)`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "grades = np.array([\n",
    "    [85, 90, 78],\n",
    "    [92, 60, 88],\n",
    "    [70, 75, 95],\n",
    "])\n",
    "for j in range(grades.shape[1]):\n",
    "    best = 0\n",
    "    for i in range(grades.shape[0]):\n",
    "        if grades[i, j] > grades[best, j]:\n",
    "            best = i\n",
    "    print(\"Exam\", j, \"best student:\", best)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "Exam 0 best student: 1\n",
    "Exam 1 best student: 0\n",
    "Exam 2 best student: 2\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t08p-048"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When you have finished, compare your solutions with the hidden ones. A different solution that passes every example is just as good."
   ],
   "id": "t08p-049"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
