{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Topic 5: Practice problems (dictionaries, sets and comprehensions)\n",
    "\n",
    "Asiman Data School, Module 1: Foundations of Python Programming. Week 3, 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: For every problem, first decide which structure fits: a dictionary when you look things up by a name or count things, a set when you need unique values or compare groups, a list when the order matters."
   ],
   "id": "t05p-000"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Easy"
   ],
   "id": "t05p-001"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 1. Price lookup\n",
    "\n",
    "Given `prices = {\"apple\": 1.5, \"bread\": 0.8, \"milk\": 2.0}`, read a product name and print its price, or `not found` if it is not in the dictionary.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `milk` | `2.0` |\n",
    "| `apple` | `1.5` |\n",
    "| `bread` | `0.8` |\n",
    "| `kiwi` | `not found` |\n",
    "| `Milk` | `not found` |"
   ],
   "id": "t05p-002"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "milk"
     ]
    }
   },
   "outputs": [],
   "source": [
    "prices = {\"apple\": 1.5, \"bread\": 0.8, \"milk\": 2.0}\n",
    "product = input()\n",
    "# Write your code here"
   ],
   "id": "t05p-003"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "One method does both jobs: `prices.get(product, \"not found\")`. An `if product in prices:` with `else` works too.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "prices = {\"apple\": 1.5, \"bread\": 0.8, \"milk\": 2.0}\n",
    "product = input()\n",
    "print(prices.get(product, \"not found\"))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `milk` | `2.0` |\n",
    "| `apple` | `1.5` |\n",
    "| `bread` | `0.8` |\n",
    "| `kiwi` | `not found` |\n",
    "| `Milk` | `not found` |\n",
    "\n",
    "</details>"
   ],
   "id": "t05p-004"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 2. Update the stock\n",
    "\n",
    "Given `stock = {\"apple\": 10, \"bread\": 4}`, add `\"milk\"` with 6, increase `\"apple\"` by 5, remove `\"bread\"`, and print the dictionary."
   ],
   "id": "t05p-005"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "stock = {\"apple\": 10, \"bread\": 4}\n",
    "# Write your code here"
   ],
   "id": "t05p-006"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Adding and changing both use `stock[key] = value`. Remove with `del stock[\"bread\"]` or `stock.pop(\"bread\")`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "stock = {\"apple\": 10, \"bread\": 4}\n",
    "stock[\"milk\"] = 6\n",
    "stock[\"apple\"] = stock[\"apple\"] + 5\n",
    "del stock[\"bread\"]\n",
    "print(stock)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "{'apple': 15, 'milk': 6}\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t05p-007"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 3. Average grade\n",
    "\n",
    "Given `grades = {\"Aylin\": 85, \"Murad\": 92, \"Leyla\": 78}`, print the average grade."
   ],
   "id": "t05p-008"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "grades = {\"Aylin\": 85, \"Murad\": 92, \"Leyla\": 78}\n",
    "# Write your code here"
   ],
   "id": "t05p-009"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The grades are the values: `sum(grades.values())` divided by `len(grades)`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "grades = {\"Aylin\": 85, \"Murad\": 92, \"Leyla\": 78}\n",
    "print(sum(grades.values()) / len(grades))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "85.0\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t05p-010"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 4. How many different numbers?\n",
    "\n",
    "Read a list of numbers and print how many different numbers it has.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `3 5 3 8 5 3 9` | `4` |\n",
    "| `1 1 1` | `1` |\n",
    "| `4` | `1` |\n",
    "| `1 2 3` | `3` |\n",
    "| `-1 1 -1` | `2` |"
   ],
   "id": "t05p-011"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "3 5 3 8 5 3 9"
     ]
    }
   },
   "outputs": [],
   "source": [
    "numbers = list(map(int, input().split()))\n",
    "# Write your code here"
   ],
   "id": "t05p-012"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "A set keeps each value once: `len(set(numbers))`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "numbers = list(map(int, input().split()))\n",
    "print(len(set(numbers)))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `3 5 3 8 5 3 9` | `4` |\n",
    "| `1 1 1` | `1` |\n",
    "| `4` | `1` |\n",
    "| `1 2 3` | `3` |\n",
    "| `-1 1 -1` | `2` |\n",
    "\n",
    "</details>"
   ],
   "id": "t05p-013"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 5. Three times every number\n",
    "\n",
    "Read a list of numbers and print a list with every number multiplied by 3. Use a list comprehension.\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]` |"
   ],
   "id": "t05p-014"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "1 2 3"
     ]
    }
   },
   "outputs": [],
   "source": [
    "numbers = list(map(int, input().split()))\n",
    "# Write your code here"
   ],
   "id": "t05p-015"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The pattern is `[expression for n in numbers]`. The expression here is `n * 3`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "numbers = list(map(int, input().split()))\n",
    "print([n * 3 for n in numbers])\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",
    "\n",
    "</details>"
   ],
   "id": "t05p-016"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Medium"
   ],
   "id": "t05p-017"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 6. Count the words\n",
    "\n",
    "Read a line of words and print how many times each word appears, as a dictionary.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `red blue red green red blue` | `{'red': 3, 'blue': 2, 'green': 1}` |\n",
    "| `a` | `{'a': 1}` |\n",
    "| `x y x y` | `{'x': 2, 'y': 2}` |\n",
    "| `one two three` | `{'one': 1, 'two': 1, 'three': 1}` |"
   ],
   "id": "t05p-018"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "red blue red green red blue"
     ]
    }
   },
   "outputs": [],
   "source": [
    "words = input().split()\n",
    "# Write your code here"
   ],
   "id": "t05p-019"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The counting pattern: start with `counts = {}`, and for every word write `counts[word] = counts.get(word, 0) + 1`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "words = input().split()\n",
    "counts = {}\n",
    "for word in words:\n",
    "    counts[word] = counts.get(word, 0) + 1\n",
    "print(counts)\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `red blue red green red blue` | `{'red': 3, 'blue': 2, 'green': 1}` |\n",
    "| `a` | `{'a': 1}` |\n",
    "| `x y x y` | `{'x': 2, 'y': 2}` |\n",
    "| `one two three` | `{'one': 1, 'two': 1, 'three': 1}` |\n",
    "\n",
    "</details>"
   ],
   "id": "t05p-020"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 7. The most frequent letter\n",
    "\n",
    "Read a word and print the letter that appears most often. If several letters share the top count, print the one that appears first in the word.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `banana` | `a` |\n",
    "| `mississippi` | `i` |\n",
    "| `a` | `a` |\n",
    "| `hello` | `l` |\n",
    "| `abc` | `a` |"
   ],
   "id": "t05p-021"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "banana"
     ]
    }
   },
   "outputs": [],
   "source": [
    "word = input()\n",
    "# Write your code here"
   ],
   "id": "t05p-022"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Count the letters with the counting pattern. Then `max(counts, key=counts.get)` gives the key with the largest value (the first one when several are equal).\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "word = input()\n",
    "counts = {}\n",
    "for letter in word:\n",
    "    counts[letter] = counts.get(letter, 0) + 1\n",
    "print(max(counts, key=counts.get))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `banana` | `a` |\n",
    "| `mississippi` | `i` |\n",
    "| `a` | `a` |\n",
    "| `hello` | `l` |\n",
    "| `abc` | `a` |\n",
    "\n",
    "</details>"
   ],
   "id": "t05p-023"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 8. Numbers in both lines\n",
    "\n",
    "Read two lines of numbers and print the numbers that appear in both lines, sorted. Print them as a list.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `1 2 3 4, 3 4 5` | `[3, 4]` |\n",
    "| `1 2, 3 4` | `[]` |\n",
    "| `5 5 5, 5` | `[5]` |\n",
    "| `9 1 7, 7 9 1` | `[1, 7, 9]` |\n",
    "| `3, 3 3` | `[3]` |"
   ],
   "id": "t05p-024"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "1 2 3 4",
      "3 4 5"
     ]
    }
   },
   "outputs": [],
   "source": [
    "a = list(map(int, input().split()))\n",
    "b = list(map(int, input().split()))\n",
    "# Write your code here"
   ],
   "id": "t05p-025"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Turn both lists into sets and take the intersection: `set(a) & set(b)`. `sorted(...)` gives a list in order.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "a = list(map(int, input().split()))\n",
    "b = list(map(int, input().split()))\n",
    "print(sorted(set(a) & set(b)))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `1 2 3 4, 3 4 5` | `[3, 4]` |\n",
    "| `1 2, 3 4` | `[]` |\n",
    "| `5 5 5, 5` | `[5]` |\n",
    "| `9 1 7, 7 9 1` | `[1, 7, 9]` |\n",
    "| `3, 3 3` | `[3]` |\n",
    "\n",
    "</details>"
   ],
   "id": "t05p-026"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 9. A counting function\n",
    "\n",
    "Write a function `count_values(numbers)` that returns a dictionary with how many times each number appears.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `3 5 3 8 5 3` | `{3: 3, 5: 2, 8: 1}` |\n",
    "| `1` | `{1: 1}` |\n",
    "| `2 2 2` | `{2: 3}` |\n",
    "| `1 2 3` | `{1: 1, 2: 1, 3: 1}` |"
   ],
   "id": "t05p-027"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "3 5 3 8 5 3"
     ]
    }
   },
   "outputs": [],
   "source": [
    "# Write your function here\n",
    "\n",
    "\n",
    "numbers = list(map(int, input().split()))\n",
    "print(count_values(numbers))"
   ],
   "id": "t05p-028"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The counting pattern inside a function. Build the dictionary in a loop and `return` it at the end.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "def count_values(numbers):\n",
    "    counts = {}\n",
    "    for number in numbers:\n",
    "        counts[number] = counts.get(number, 0) + 1\n",
    "    return counts\n",
    "\n",
    "numbers = list(map(int, input().split()))\n",
    "print(count_values(numbers))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `3 5 3 8 5 3` | `{3: 3, 5: 2, 8: 1}` |\n",
    "| `1` | `{1: 1}` |\n",
    "| `2 2 2` | `{2: 3}` |\n",
    "| `1 2 3` | `{1: 1, 2: 1, 3: 1}` |\n",
    "\n",
    "</details>"
   ],
   "id": "t05p-029"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 10. All different?\n",
    "\n",
    "Read a list of numbers and print `True` if all the numbers are different, otherwise `False`.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `1 2 3` | `True` |\n",
    "| `1 2 1` | `False` |\n",
    "| `5` | `True` |\n",
    "| `0 0` | `False` |\n",
    "| `4 -4` | `True` |"
   ],
   "id": "t05p-030"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "1 2 3"
     ]
    }
   },
   "outputs": [],
   "source": [
    "numbers = list(map(int, input().split()))\n",
    "# Write your code here"
   ],
   "id": "t05p-031"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "If nothing repeats, the set has as many items as the list: compare `len(numbers)` with `len(set(numbers))`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "numbers = list(map(int, input().split()))\n",
    "print(len(numbers) == len(set(numbers)))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `1 2 3` | `True` |\n",
    "| `1 2 1` | `False` |\n",
    "| `5` | `True` |\n",
    "| `0 0` | `False` |\n",
    "| `4 -4` | `True` |\n",
    "\n",
    "</details>"
   ],
   "id": "t05p-032"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Medium-Hard"
   ],
   "id": "t05p-033"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 11. Numbers that appear exactly once\n",
    "\n",
    "Read a list of numbers and print the numbers that appear exactly once, in their original order, as a list.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `3 5 3 8 5 9` | `[8, 9]` |\n",
    "| `1 1 2 2` | `[]` |\n",
    "| `7` | `[7]` |\n",
    "| `4 5 6` | `[4, 5, 6]` |\n",
    "| `2 3 2 3 4` | `[4]` |"
   ],
   "id": "t05p-034"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "3 5 3 8 5 9"
     ]
    }
   },
   "outputs": [],
   "source": [
    "numbers = list(map(int, input().split()))\n",
    "# Write your code here"
   ],
   "id": "t05p-035"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "First count every number in a dictionary. Then go through the list again and keep the numbers whose count is 1 (a loop with `if`, or a comprehension).\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "numbers = list(map(int, input().split()))\n",
    "counts = {}\n",
    "for number in numbers:\n",
    "    counts[number] = counts.get(number, 0) + 1\n",
    "print([number for number in numbers if counts[number] == 1])\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `3 5 3 8 5 9` | `[8, 9]` |\n",
    "| `1 1 2 2` | `[]` |\n",
    "| `7` | `[7]` |\n",
    "| `4 5 6` | `[4, 5, 6]` |\n",
    "| `2 3 2 3 4` | `[4]` |\n",
    "\n",
    "</details>"
   ],
   "id": "t05p-036"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 12. Only in the first line\n",
    "\n",
    "Read two lines of words and print the words that are in the first line but not in the second, sorted. Print them as a list.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `red blue green, blue` | `['green', 'red']` |\n",
    "| `a b, a b` | `[]` |\n",
    "| `x, y` | `['x']` |\n",
    "| `pear apple fig apple, fig` | `['apple', 'pear']` |\n",
    "| `one two, three` | `['one', 'two']` |"
   ],
   "id": "t05p-037"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "red blue green",
      "blue"
     ]
    }
   },
   "outputs": [],
   "source": [
    "first = input().split()\n",
    "second = input().split()\n",
    "# Write your code here"
   ],
   "id": "t05p-038"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The difference of two sets: `set(first) - set(second)`. Sort the result.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "first = input().split()\n",
    "second = input().split()\n",
    "print(sorted(set(first) - set(second)))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `red blue green, blue` | `['green', 'red']` |\n",
    "| `a b, a b` | `[]` |\n",
    "| `x, y` | `['x']` |\n",
    "| `pear apple fig apple, fig` | `['apple', 'pear']` |\n",
    "| `one two, three` | `['one', 'two']` |\n",
    "\n",
    "</details>"
   ],
   "id": "t05p-039"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 13. The best student\n",
    "\n",
    "Read `n`, then `n` lines with a name and a grade. Print the name of the student with the highest grade. If several students share the highest grade, print the first of them.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `3, Aylin 85, Murad 92, Leyla 78` | `Murad` |\n",
    "| `1, Kamran 45` | `Kamran` |\n",
    "| `2, Ali 70, Veli 70` | `Ali` |\n",
    "| `3, A 10, B 99, C 50` | `B` |"
   ],
   "id": "t05p-040"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "3",
      "Aylin 85",
      "Murad 92",
      "Leyla 78"
     ]
    }
   },
   "outputs": [],
   "source": [
    "n = int(input())\n",
    "grades = {}\n",
    "for i in range(n):\n",
    "    name, grade = input().split()\n",
    "    grades[name] = int(grade)\n",
    "# Write your code here"
   ],
   "id": "t05p-041"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The dictionary is already built by the first lines. `max(grades, key=grades.get)` gives the name with the largest grade.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "n = int(input())\n",
    "grades = {}\n",
    "for i in range(n):\n",
    "    name, grade = input().split()\n",
    "    grades[name] = int(grade)\n",
    "print(max(grades, key=grades.get))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `3, Aylin 85, Murad 92, Leyla 78` | `Murad` |\n",
    "| `1, Kamran 45` | `Kamran` |\n",
    "| `2, Ali 70, Veli 70` | `Ali` |\n",
    "| `3, A 10, B 99, C 50` | `B` |\n",
    "\n",
    "</details>"
   ],
   "id": "t05p-042"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 14. Group the words by length\n",
    "\n",
    "Read a line of words and print a dictionary that groups the words by their length. Example: `red blue sky green` gives `{3: ['red', 'sky'], 4: ['blue'], 5: ['green']}`.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `red blue sky green` | `{3: ['red', 'sky'], 4: ['blue'], 5: ['green']}` |\n",
    "| `a bb cc d` | `{1: ['a', 'd'], 2: ['bb', 'cc']}` |\n",
    "| `hello` | `{5: ['hello']}` |\n",
    "| `one two six` | `{3: ['one', 'two', 'six']}` |"
   ],
   "id": "t05p-043"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "red blue sky green"
     ]
    }
   },
   "outputs": [],
   "source": [
    "words = input().split()\n",
    "# Write your code here"
   ],
   "id": "t05p-044"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Each value is a list. When a length appears for the first time, store an empty list for it, then `append` the word (solved problem 20 groups by the first letter in the same way).\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "words = input().split()\n",
    "groups = {}\n",
    "for word in words:\n",
    "    length = len(word)\n",
    "    if length not in groups:\n",
    "        groups[length] = []\n",
    "    groups[length].append(word)\n",
    "print(groups)\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `red blue sky green` | `{3: ['red', 'sky'], 4: ['blue'], 5: ['green']}` |\n",
    "| `a bb cc d` | `{1: ['a', 'd'], 2: ['bb', 'cc']}` |\n",
    "| `hello` | `{5: ['hello']}` |\n",
    "| `one two six` | `{3: ['one', 'two', 'six']}` |\n",
    "\n",
    "</details>"
   ],
   "id": "t05p-045"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 15. Even or odd for every number\n",
    "\n",
    "Read a list of numbers and print a dictionary where each number is a key and the value is `\"even\"` or `\"odd\"`. Use a dictionary comprehension.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `1 2 3` | `{1: 'odd', 2: 'even', 3: 'odd'}` |\n",
    "| `4` | `{4: 'even'}` |\n",
    "| `0 7` | `{0: 'even', 7: 'odd'}` |\n",
    "| `10 11 12 13` | `{10: 'even', 11: 'odd', 12: 'even', 13: 'odd'}` |"
   ],
   "id": "t05p-046"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "1 2 3"
     ]
    }
   },
   "outputs": [],
   "source": [
    "numbers = list(map(int, input().split()))\n",
    "# Write your code here"
   ],
   "id": "t05p-047"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The pattern is `{key: value for n in numbers}`. The value is the one-line `if/else`: `\"even\" if n % 2 == 0 else \"odd\"`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "numbers = list(map(int, input().split()))\n",
    "print({n: \"even\" if n % 2 == 0 else \"odd\" for n in numbers})\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `1 2 3` | `{1: 'odd', 2: 'even', 3: 'odd'}` |\n",
    "| `4` | `{4: 'even'}` |\n",
    "| `0 7` | `{0: 'even', 7: 'odd'}` |\n",
    "| `10 11 12 13` | `{10: 'even', 11: 'odd', 12: 'even', 13: 'odd'}` |\n",
    "\n",
    "</details>"
   ],
   "id": "t05p-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": "t05p-049"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
