{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Topic 9: Practice problems (NumPy slicing, masks and broadcasting)\n",
    "\n",
    "Asiman Data School, Module 1: Foundations of Python Programming. Week 5, Tuesday.\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: Before you write a loop, ask: \"can I write this as one calculation on the whole array?\" For a table, say which you want: \"one result for every column\" (`axis=0`) or \"one result for every row\" (`axis=1`)."
   ],
   "id": "t09p-000"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Easy"
   ],
   "id": "t09p-001"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 1. Reversed\n",
    "\n",
    "Read a line of numbers into an array and print it reversed.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `1 2 3 4 5` | `[5 4 3 2 1]` |\n",
    "| `7` | `[7]` |\n",
    "| `10 20` | `[20 10]` |\n",
    "| `3 3 9` | `[9 3 3]` |\n",
    "| `-1 0 1` | `[ 1  0 -1]` |"
   ],
   "id": "t09p-002"
  },
  {
   "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": "t09p-003"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "A slice with the step -1 walks backwards: `a[::-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",
    "a = np.array(list(map(int, input().split())))\n",
    "print(a[::-1])\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `1 2 3 4 5` | `[5 4 3 2 1]` |\n",
    "| `7` | `[7]` |\n",
    "| `10 20` | `[20 10]` |\n",
    "| `3 3 9` | `[9 3 3]` |\n",
    "| `-1 0 1` | `[ 1  0 -1]` |\n",
    "\n",
    "</details>"
   ],
   "id": "t09p-004"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 2. Three times every number\n",
    "\n",
    "Read a line of numbers into an array and print every number multiplied by 3.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `1 2 3` | `[3 6 9]` |\n",
    "| `0` | `[0]` |\n",
    "| `-4 5` | `[-12  15]` |\n",
    "| `10 20 30 40` | `[ 30  60  90 120]` |\n",
    "| `7 7` | `[21 21]` |"
   ],
   "id": "t09p-005"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "1 2 3"
     ]
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array(list(map(int, input().split())))\n",
    "# Write your code here"
   ],
   "id": "t09p-006"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "No loop is needed. Write the calculation on the whole array: `a * 3`.\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 * 3)\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `1 2 3` | `[3 6 9]` |\n",
    "| `0` | `[0]` |\n",
    "| `-4 5` | `[-12  15]` |\n",
    "| `10 20 30 40` | `[ 30  60  90 120]` |\n",
    "| `7 7` | `[21 21]` |\n",
    "\n",
    "</details>"
   ],
   "id": "t09p-007"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 3. The last column\n",
    "\n",
    "Create the numbers 1 to 12 reshaped into 3 rows and 4 columns. Print the last column."
   ],
   "id": "t09p-008"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t09p-009"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`np.arange(1, 13).reshape(3, 4)` makes the table. All rows, last column: `table[:, -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[:, -1])\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "[ 4  8 12]\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t09p-010"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 4. Larger than 10\n",
    "\n",
    "Read a line of numbers into an array and print only the numbers larger than 10.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `5 12 30 10 11` | `[12 30 11]` |\n",
    "| `1 2 3` | `[]` |\n",
    "| `11` | `[11]` |\n",
    "| `10 10` | `[]` |\n",
    "| `-20 50 100` | `[ 50 100]` |"
   ],
   "id": "t09p-011"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "5 12 30 10 11"
     ]
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array(list(map(int, input().split())))\n",
    "# Write your code here"
   ],
   "id": "t09p-012"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The condition `a > 10` gives a mask. Put it inside the brackets: `a[a > 10]`.\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 > 10])\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `5 12 30 10 11` | `[12 30 11]` |\n",
    "| `1 2 3` | `[]` |\n",
    "| `11` | `[11]` |\n",
    "| `10 10` | `[]` |\n",
    "| `-20 50 100` | `[ 50 100]` |\n",
    "\n",
    "</details>"
   ],
   "id": "t09p-013"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 5. Prices with tax\n",
    "\n",
    "Given `prices = np.array([10, 20, 30, 40])`, print the prices with 18% tax added (multiply by 1.18)."
   ],
   "id": "t09p-014"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "prices = np.array([10, 20, 30, 40])\n",
    "# Write your code here"
   ],
   "id": "t09p-015"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "One multiplication for the whole array: `prices * 1.18`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "prices = np.array([10, 20, 30, 40])\n",
    "print(prices * 1.18)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "[11.8 23.6 35.4 47.2]\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t09p-016"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Medium"
   ],
   "id": "t09p-017"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 6. How many are negative?\n",
    "\n",
    "Read a line of numbers into an array. Print how many of them are negative.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `3 -1 -5 0 2` | `2` |\n",
    "| `1 2 3` | `0` |\n",
    "| `-1 -2 -3` | `3` |\n",
    "| `0` | `0` |\n",
    "| `-7` | `1` |"
   ],
   "id": "t09p-018"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "3 -1 -5 0 2"
     ]
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array(list(map(int, input().split())))\n",
    "# Write your code here"
   ],
   "id": "t09p-019"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`a < 0` is a mask, and `sum` counts its `True` values: `(a < 0).sum()`.\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 < 0).sum())\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `3 -1 -5 0 2` | `2` |\n",
    "| `1 2 3` | `0` |\n",
    "| `-1 -2 -3` | `3` |\n",
    "| `0` | `0` |\n",
    "| `-7` | `1` |\n",
    "\n",
    "</details>"
   ],
   "id": "t09p-020"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 7. A block of a table\n",
    "\n",
    "Create the numbers 1 to 20 reshaped into 4 rows and 5 columns. Print the block made of the first two rows and the last two columns."
   ],
   "id": "t09p-021"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t09p-022"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The first two rows are `:2`, the last two columns are `-2:`. Both in one pair of brackets: `table[:2, -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",
    "table = np.arange(1, 21).reshape(4, 5)\n",
    "print(table[:2, -2:])\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "[[ 4  5]\n",
    " [ 9 10]]\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t09p-023"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 8. Pass or fail\n",
    "\n",
    "Read a line of grades into an array. Print an array with `\"pass\"` for every grade of at least 50 and `\"fail\"` for the others.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `85 42 50 49` | `['pass' 'fail' 'pass' 'fail']` |\n",
    "| `50` | `['pass']` |\n",
    "| `49` | `['fail']` |\n",
    "| `0 100` | `['fail' 'pass']` |\n",
    "| `70 80 90` | `['pass' 'pass' 'pass']` |"
   ],
   "id": "t09p-024"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "85 42 50 49"
     ]
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "grades = np.array(list(map(int, input().split())))\n",
    "# Write your code here"
   ],
   "id": "t09p-025"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`np.where(condition, a, b)` chooses between two values for every element: `np.where(grades >= 50, \"pass\", \"fail\")`.\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(list(map(int, input().split())))\n",
    "print(np.where(grades >= 50, \"pass\", \"fail\"))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `85 42 50 49` | `['pass' 'fail' 'pass' 'fail']` |\n",
    "| `50` | `['pass']` |\n",
    "| `49` | `['fail']` |\n",
    "| `0 100` | `['fail' 'pass']` |\n",
    "| `70 80 90` | `['pass' 'pass' 'pass']` |\n",
    "\n",
    "</details>"
   ],
   "id": "t09p-026"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 9. Above the mean\n",
    "\n",
    "Read a line of numbers into an array. Print the numbers that are larger than the mean of the array.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `1 2 3 4 5` | `[4 5]` |\n",
    "| `10 10 10` | `[]` |\n",
    "| `5` | `[]` |\n",
    "| `1 100` | `[100]` |\n",
    "| `-5 0 5 10` | `[ 5 10]` |"
   ],
   "id": "t09p-027"
  },
  {
   "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": "t09p-028"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`a.mean()` is one number, so `a > a.mean()` is a mask. Use it inside the brackets.\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 > a.mean()])\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `1 2 3 4 5` | `[4 5]` |\n",
    "| `10 10 10` | `[]` |\n",
    "| `5` | `[]` |\n",
    "| `1 100` | `[100]` |\n",
    "| `-5 0 5 10` | `[ 5 10]` |\n",
    "\n",
    "</details>"
   ],
   "id": "t09p-029"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 10. Sums of rows and columns\n",
    "\n",
    "Create the numbers 1 to 12 reshaped into 3 rows and 4 columns. Print the sum of every row and the sum of every column, in this order, each on its own line."
   ],
   "id": "t09p-030"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t09p-031"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "One result for every row is `axis=1`. One result for every column is `axis=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.arange(1, 13).reshape(3, 4)\n",
    "print(table.sum(axis=1))\n",
    "print(table.sum(axis=0))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "[10 26 42]\n",
    "[15 18 21 24]\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t09p-032"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Medium-Hard"
   ],
   "id": "t09p-033"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 11. Not above 100\n",
    "\n",
    "Read a line of numbers into an array. Replace every number above 100 with 100 and print the array.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `50 150 100 101 99` | `[ 50 100 100 100  99]` |\n",
    "| `1 2 3` | `[1 2 3]` |\n",
    "| `200` | `[100]` |\n",
    "| `100 100` | `[100 100]` |\n",
    "| `-5 500` | `[ -5 100]` |"
   ],
   "id": "t09p-034"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "50 150 100 101 99"
     ]
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array(list(map(int, input().split())))\n",
    "# Write your code here"
   ],
   "id": "t09p-035"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "A mask on the left side of `=` changes only the chosen elements: `a[a > 100] = 100`. `np.where(a > 100, 100, a)` gives the same result as a new array.\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",
    "a[a > 100] = 100\n",
    "print(a)\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `50 150 100 101 99` | `[ 50 100 100 100  99]` |\n",
    "| `1 2 3` | `[1 2 3]` |\n",
    "| `200` | `[100]` |\n",
    "| `100 100` | `[100 100]` |\n",
    "| `-5 500` | `[ -5 100]` |\n",
    "\n",
    "</details>"
   ],
   "id": "t09p-036"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 12. Between 10 and 20\n",
    "\n",
    "Read a line of numbers into an array. Print the numbers that are at least 10 and at most 20.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `5 10 15 20 25` | `[10 15 20]` |\n",
    "| `9 21` | `[]` |\n",
    "| `10` | `[10]` |\n",
    "| `20 20 3` | `[20 20]` |\n",
    "| `11 19 30 -15` | `[11 19]` |"
   ],
   "id": "t09p-037"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "5 10 15 20 25"
     ]
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array(list(map(int, input().split())))\n",
    "# Write your code here"
   ],
   "id": "t09p-038"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Two conditions joined with `&`, each in its own parentheses: `(a >= 10) & (a <= 20)`.\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 >= 10) & (a <= 20)])\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `5 10 15 20 25` | `[10 15 20]` |\n",
    "| `9 21` | `[]` |\n",
    "| `10` | `[10]` |\n",
    "| `20 20 3` | `[20 20]` |\n",
    "| `11 19 30 -15` | `[11 19]` |\n",
    "\n",
    "</details>"
   ],
   "id": "t09p-039"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 13. The total cost\n",
    "\n",
    "Read two lines of numbers of the same length into two arrays: prices and amounts. Print the total cost.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `10 20 30, 2 1 4` | `160` |\n",
    "| `5, 3` | `15` |\n",
    "| `1 1 1, 0 0 0` | `0` |\n",
    "| `100 250, 2 2` | `700` |\n",
    "| `7 3 5 1, 1 2 3 4` | `32` |"
   ],
   "id": "t09p-040"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "10 20 30",
      "2 1 4"
     ]
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "prices = np.array(list(map(int, input().split())))\n",
    "amounts = np.array(list(map(int, input().split())))\n",
    "# Write your code here"
   ],
   "id": "t09p-041"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`prices * amounts` multiplies element by element: the cost of every product. Then add the costs up with `.sum()`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "prices = np.array(list(map(int, input().split())))\n",
    "amounts = np.array(list(map(int, input().split())))\n",
    "print((prices * amounts).sum())\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `10 20 30, 2 1 4` | `160` |\n",
    "| `5, 3` | `15` |\n",
    "| `1 1 1, 0 0 0` | `0` |\n",
    "| `100 250, 2 2` | `700` |\n",
    "| `7 3 5 1, 1 2 3 4` | `32` |\n",
    "\n",
    "</details>"
   ],
   "id": "t09p-042"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 14. The best average\n",
    "\n",
    "Given a grade table where each row is a student and each column an exam, print the average of every student, rounded to 1 decimal, and the index of the student with the highest average. Two lines."
   ],
   "id": "t09p-043"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "grades = np.array([\n",
    "    [70, 80, 90],\n",
    "    [95, 85, 99],\n",
    "    [60, 75, 71],\n",
    "])\n",
    "# Write your code here"
   ],
   "id": "t09p-044"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "One average for every row: `grades.mean(axis=1)`. Round it with `np.round(averages, 1)`. The index of the highest one is `averages.argmax()`.\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",
    "    [70, 80, 90],\n",
    "    [95, 85, 99],\n",
    "    [60, 75, 71],\n",
    "])\n",
    "averages = grades.mean(axis=1)\n",
    "print(np.round(averages, 1))\n",
    "print(averages.argmax())\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "[80.  93.  68.7]\n",
    "1\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t09p-045"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 15. From 0 to 1\n",
    "\n",
    "Write a function `normalize(a)` that returns the array with its smallest value subtracted from every element and the result divided by the difference between the largest and the smallest value. The smallest value becomes 0 and the largest becomes 1.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `10 20 30` | `[0.  0.5 1. ]` |\n",
    "| `0 5 10 20` | `[0.   0.25 0.5  1.  ]` |\n",
    "| `3 1 2` | `[1.  0.  0.5]` |\n",
    "| `-10 0 10` | `[0.  0.5 1. ]` |\n",
    "| `2 4` | `[0. 1.]` |"
   ],
   "id": "t09p-046"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "10 20 30"
     ]
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Write the function normalize(a) here\n",
    "\n",
    "a = np.array(list(map(int, input().split())))\n",
    "print(normalize(a))"
   ],
   "id": "t09p-047"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Store `a.min()` and `a.max()` first. Then one line on the whole array: `(a - smallest) / (largest - smallest)`.\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 normalize(a):\n",
    "    smallest = a.min()\n",
    "    largest = a.max()\n",
    "    return (a - smallest) / (largest - smallest)\n",
    "\n",
    "a = np.array(list(map(int, input().split())))\n",
    "print(normalize(a))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `10 20 30` | `[0.  0.5 1. ]` |\n",
    "| `0 5 10 20` | `[0.   0.25 0.5  1.  ]` |\n",
    "| `3 1 2` | `[1.  0.  0.5]` |\n",
    "| `-10 0 10` | `[0.  0.5 1. ]` |\n",
    "| `2 4` | `[0. 1.]` |\n",
    "\n",
    "</details>"
   ],
   "id": "t09p-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": "t09p-049"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
