{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Topic 7: Practice problems (classes and objects, modules)\n",
    "\n",
    "Asiman Data School, Module 1: Foundations of Python Programming. Week 4, 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 write down what the object has (its attributes) and what it can do (its methods). Then write `__init__`, then the methods."
   ],
   "id": "t07p-000"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Easy"
   ],
   "id": "t07p-001"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 1. A circle\n",
    "\n",
    "Write a class `Circle` with the attribute `radius` and a method `area()` that returns the area. Use `math.pi`.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `1` | `3.14` |\n",
    "| `2` | `12.57` |\n",
    "| `0` | `0.0` |\n",
    "| `2.5` | `19.63` |\n",
    "| `10` | `314.16` |"
   ],
   "id": "t07p-002"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "1"
     ]
    }
   },
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "# Write your class here\n",
    "\n",
    "\n",
    "radius = float(input())\n",
    "circle = Circle(radius)\n",
    "print(round(circle.area(), 2))"
   ],
   "id": "t07p-003"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`__init__(self, radius)` stores the radius in `self.radius`. The area is `math.pi * self.radius ** 2`. Return it, do not print it.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import math\n",
    "\n",
    "class Circle:\n",
    "    def __init__(self, radius):\n",
    "        self.radius = radius\n",
    "\n",
    "    def area(self):\n",
    "        return math.pi * self.radius ** 2\n",
    "\n",
    "radius = float(input())\n",
    "circle = Circle(radius)\n",
    "print(round(circle.area(), 2))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `1` | `3.14` |\n",
    "| `2` | `12.57` |\n",
    "| `0` | `0.0` |\n",
    "| `2.5` | `19.63` |\n",
    "| `10` | `314.16` |\n",
    "\n",
    "</details>"
   ],
   "id": "t07p-004"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 2. A counter\n",
    "\n",
    "Write a class `Counter` with a method `increase()` and a method `reset()`. The counter starts at 0, in an attribute called `value`."
   ],
   "id": "t07p-005"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your class here\n",
    "\n",
    "\n",
    "counter = Counter()\n",
    "counter.increase()\n",
    "counter.increase()\n",
    "print(counter.value)\n",
    "counter.reset()\n",
    "print(counter.value)"
   ],
   "id": "t07p-006"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`__init__(self)` sets `self.value = 0`. `increase` adds 1 to `self.value`, and `reset` sets it back to 0.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "class Counter:\n",
    "    def __init__(self):\n",
    "        self.value = 0\n",
    "\n",
    "    def increase(self):\n",
    "        self.value = self.value + 1\n",
    "\n",
    "    def reset(self):\n",
    "        self.value = 0\n",
    "\n",
    "counter = Counter()\n",
    "counter.increase()\n",
    "counter.increase()\n",
    "print(counter.value)\n",
    "counter.reset()\n",
    "print(counter.value)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "2\n",
    "0\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t07p-007"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 3. A student that prints nicely\n",
    "\n",
    "Add a `__str__` method to a class `Student` (name and grade), so that `print(student)` shows a text like `Aylin: 85`.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `Aylin 85` | `Aylin: 85` |\n",
    "| `Murad 42` | `Murad: 42` |\n",
    "| `Leyla 100` | `Leyla: 100` |\n",
    "| `X 0` | `X: 0` |"
   ],
   "id": "t07p-008"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "Aylin 85"
     ]
    }
   },
   "outputs": [],
   "source": [
    "# Write your class here\n",
    "\n",
    "\n",
    "name, grade = input().split()\n",
    "student = Student(name, int(grade))\n",
    "print(student)"
   ],
   "id": "t07p-009"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Write `__init__(self, name, grade)` first. `__str__(self)` must **return** the text: `return f\"{self.name}: {self.grade}\"`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "class Student:\n",
    "    def __init__(self, name, grade):\n",
    "        self.name = name\n",
    "        self.grade = grade\n",
    "\n",
    "    def __str__(self):\n",
    "        return f\"{self.name}: {self.grade}\"\n",
    "\n",
    "name, grade = input().split()\n",
    "student = Student(name, int(grade))\n",
    "print(student)\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `Aylin 85` | `Aylin: 85` |\n",
    "| `Murad 42` | `Murad: 42` |\n",
    "| `Leyla 100` | `Leyla: 100` |\n",
    "| `X 0` | `X: 0` |\n",
    "\n",
    "</details>"
   ],
   "id": "t07p-010"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 4. Mean and median\n",
    "\n",
    "Use the `statistics` module to print the mean and the median of `[4, 8, 15, 16, 23, 42]`, each on its own line."
   ],
   "id": "t07p-011"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import statistics\n",
    "\n",
    "numbers = [4, 8, 15, 16, 23, 42]\n",
    "# Write your code here"
   ],
   "id": "t07p-012"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The functions are `statistics.mean(numbers)` and `statistics.median(numbers)`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import statistics\n",
    "\n",
    "numbers = [4, 8, 15, 16, 23, 42]\n",
    "print(statistics.mean(numbers))\n",
    "print(statistics.median(numbers))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "18\n",
    "15.5\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t07p-013"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Medium"
   ],
   "id": "t07p-014"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 5. Temperature\n",
    "\n",
    "Write a class `Temperature` that stores degrees Celsius and has a method `to_fahrenheit()`. The formula is F = C × 9 / 5 + 32.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `25` | `77.0` |\n",
    "| `0` | `32.0` |\n",
    "| `100` | `212.0` |\n",
    "| `-40` | `-40.0` |\n",
    "| `36.6` | `97.9` |"
   ],
   "id": "t07p-015"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "25"
     ]
    }
   },
   "outputs": [],
   "source": [
    "# Write your class here\n",
    "\n",
    "\n",
    "celsius = float(input())\n",
    "print(round(Temperature(celsius).to_fahrenheit(), 1))"
   ],
   "id": "t07p-016"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Store the value in `__init__`. The method returns `self.celsius * 9 / 5 + 32`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "class Temperature:\n",
    "    def __init__(self, celsius):\n",
    "        self.celsius = celsius\n",
    "\n",
    "    def to_fahrenheit(self):\n",
    "        return self.celsius * 9 / 5 + 32\n",
    "\n",
    "celsius = float(input())\n",
    "print(round(Temperature(celsius).to_fahrenheit(), 1))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `25` | `77.0` |\n",
    "| `0` | `32.0` |\n",
    "| `100` | `212.0` |\n",
    "| `-40` | `-40.0` |\n",
    "| `36.6` | `97.9` |\n",
    "\n",
    "</details>"
   ],
   "id": "t07p-017"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 6. A bank account\n",
    "\n",
    "Write a class `BankAccount` with `deposit` and `withdraw`. A withdrawal larger than the balance must print `not enough money` and leave the balance unchanged. The class takes the starting balance and keeps it in an attribute called `balance`."
   ],
   "id": "t07p-018"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your class here\n",
    "\n",
    "\n",
    "account = BankAccount(100)\n",
    "account.deposit(50)\n",
    "account.withdraw(500)\n",
    "account.withdraw(30)\n",
    "print(account.balance)"
   ],
   "id": "t07p-019"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "In `withdraw`, check `if amount > self.balance:` first. Print the message and `return`, so the line that subtracts is not reached.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "class BankAccount:\n",
    "    def __init__(self, balance):\n",
    "        self.balance = balance\n",
    "\n",
    "    def deposit(self, amount):\n",
    "        self.balance = self.balance + amount\n",
    "\n",
    "    def withdraw(self, amount):\n",
    "        if amount > self.balance:\n",
    "            print(\"not enough money\")\n",
    "            return\n",
    "        self.balance = self.balance - amount\n",
    "\n",
    "account = BankAccount(100)\n",
    "account.deposit(50)\n",
    "account.withdraw(500)\n",
    "account.withdraw(30)\n",
    "print(account.balance)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "not enough money\n",
    "120\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t07p-020"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 7. A person and a teacher\n",
    "\n",
    "Write a class `Person` (name) and a child class `Teacher` (name and subject). Use `super().__init__`. A method `introduce()` in `Teacher` returns a sentence with both values, like `I am Aylin and I teach Python`.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `Aylin Python` | `I am Aylin and I teach Python` |\n",
    "| `Murad SQL` | `I am Murad and I teach SQL` |\n",
    "| `Leyla Statistics` | `I am Leyla and I teach Statistics` |\n",
    "| `A B` | `I am A and I teach B` |"
   ],
   "id": "t07p-021"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "Aylin Python"
     ]
    }
   },
   "outputs": [],
   "source": [
    "# Write your classes here\n",
    "\n",
    "\n",
    "name, subject = input().split()\n",
    "teacher = Teacher(name, subject)\n",
    "print(teacher.introduce())"
   ],
   "id": "t07p-022"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`class Teacher(Person):`. In its `__init__`, call `super().__init__(name)` first, then store the subject. `introduce` returns an f-string.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "class Person:\n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "\n",
    "class Teacher(Person):\n",
    "    def __init__(self, name, subject):\n",
    "        super().__init__(name)\n",
    "        self.subject = subject\n",
    "\n",
    "    def introduce(self):\n",
    "        return f\"I am {self.name} and I teach {self.subject}\"\n",
    "\n",
    "name, subject = input().split()\n",
    "teacher = Teacher(name, subject)\n",
    "print(teacher.introduce())\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `Aylin Python` | `I am Aylin and I teach Python` |\n",
    "| `Murad SQL` | `I am Murad and I teach SQL` |\n",
    "| `Leyla Statistics` | `I am Leyla and I teach Statistics` |\n",
    "| `A B` | `I am A and I teach B` |\n",
    "\n",
    "</details>"
   ],
   "id": "t07p-023"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 8. A rectangle and a square\n",
    "\n",
    "Write a class `Rectangle` with `area()` and a child class `Square` that takes only one side.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `4` | `16 True` |\n",
    "| `1` | `1 True` |\n",
    "| `0` | `0 True` |\n",
    "| `12` | `144 True` |"
   ],
   "id": "t07p-024"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "4"
     ]
    }
   },
   "outputs": [],
   "source": [
    "# Write your classes here\n",
    "\n",
    "\n",
    "side = int(input())\n",
    "square = Square(side)\n",
    "print(square.area(), isinstance(square, Rectangle))"
   ],
   "id": "t07p-025"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "A square is a rectangle with equal sides. `Square.__init__(self, side)` needs one line: `super().__init__(side, side)`. `area` is inherited.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "class Rectangle:\n",
    "    def __init__(self, width, height):\n",
    "        self.width = width\n",
    "        self.height = height\n",
    "\n",
    "    def area(self):\n",
    "        return self.width * self.height\n",
    "\n",
    "class Square(Rectangle):\n",
    "    def __init__(self, side):\n",
    "        super().__init__(side, side)\n",
    "\n",
    "side = int(input())\n",
    "square = Square(side)\n",
    "print(square.area(), isinstance(square, Rectangle))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `4` | `16 True` |\n",
    "| `1` | `1 True` |\n",
    "| `0` | `0 True` |\n",
    "| `12` | `144 True` |\n",
    "\n",
    "</details>"
   ],
   "id": "t07p-026"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 9. A shopping list\n",
    "\n",
    "Write a class `ShoppingList` that keeps its items in an internal list and has the methods `add(item)`, `remove(item)` and `count()`."
   ],
   "id": "t07p-027"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your class here\n",
    "\n",
    "\n",
    "shopping = ShoppingList()\n",
    "shopping.add(\"milk\")\n",
    "shopping.add(\"bread\")\n",
    "shopping.add(\"eggs\")\n",
    "shopping.remove(\"bread\")\n",
    "print(shopping.count())"
   ],
   "id": "t07p-028"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`__init__` creates `self._items = []`. The three methods use the list methods you know: `append`, `remove` and `len`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "class ShoppingList:\n",
    "    def __init__(self):\n",
    "        self._items = []\n",
    "\n",
    "    def add(self, item):\n",
    "        self._items.append(item)\n",
    "\n",
    "    def remove(self, item):\n",
    "        self._items.remove(item)\n",
    "\n",
    "    def count(self):\n",
    "        return len(self._items)\n",
    "\n",
    "shopping = ShoppingList()\n",
    "shopping.add(\"milk\")\n",
    "shopping.add(\"bread\")\n",
    "shopping.add(\"eggs\")\n",
    "shopping.remove(\"bread\")\n",
    "print(shopping.count())\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "2\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t07p-029"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Medium-Hard"
   ],
   "id": "t07p-030"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 10. A student with checked grades\n",
    "\n",
    "Write a class `Student` with a name and an internal list of grades. Methods: `add_grade(grade)` (it raises a `ValueError` if the grade is not from 0 to 100), `average()` and `best()`.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `85 92 78` | `85.0 92 0` |\n",
    "| `100 150 50` | `75.0 100 1` |\n",
    "| `60` | `60.0 60 0` |\n",
    "| `-5 70 80 200` | `75.0 80 2` |"
   ],
   "id": "t07p-031"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "85 92 78"
     ]
    }
   },
   "outputs": [],
   "source": [
    "# Write your class here\n",
    "\n",
    "\n",
    "student = Student(\"Aylin\")\n",
    "rejected = 0\n",
    "for text in input().split():\n",
    "    try:\n",
    "        student.add_grade(int(text))\n",
    "    except ValueError:\n",
    "        rejected = rejected + 1\n",
    "print(student.average(), student.best(), rejected)"
   ],
   "id": "t07p-032"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`__init__(self, name)` creates `self._grades = []`. In `add_grade`, check the range first and `raise ValueError(...)`, then append. `average` uses `sum` and `len`, `best` uses `max`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "class Student:\n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "        self._grades = []\n",
    "\n",
    "    def add_grade(self, grade):\n",
    "        if grade < 0 or grade > 100:\n",
    "            raise ValueError(\"the grade must be from 0 to 100\")\n",
    "        self._grades.append(grade)\n",
    "\n",
    "    def average(self):\n",
    "        return sum(self._grades) / len(self._grades)\n",
    "\n",
    "    def best(self):\n",
    "        return max(self._grades)\n",
    "\n",
    "student = Student(\"Aylin\")\n",
    "rejected = 0\n",
    "for text in input().split():\n",
    "    try:\n",
    "        student.add_grade(int(text))\n",
    "    except ValueError:\n",
    "        rejected = rejected + 1\n",
    "print(student.average(), student.best(), rejected)\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `85 92 78` | `85.0 92 0` |\n",
    "| `100 150 50` | `75.0 100 1` |\n",
    "| `60` | `60.0 60 0` |\n",
    "| `-5 70 80 200` | `75.0 80 2` |\n",
    "\n",
    "</details>"
   ],
   "id": "t07p-033"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 11. The largest shape\n",
    "\n",
    "Write classes `Shape`, `Rectangle`, `Circle` and `Triangle`, each with `area()`. Put several shapes in a list and print the name of the shape with the largest area. Each shape has an attribute `name`: `\"rectangle\"`, `\"circle\"` or `\"triangle\"`. A triangle takes a base and a height."
   ],
   "id": "t07p-034"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "# Write your classes here\n",
    "\n",
    "\n",
    "shapes = [Rectangle(4, 3), Circle(2), Triangle(6, 5)]\n",
    "largest = max(shapes, key=lambda shape: shape.area())\n",
    "print(largest.name)"
   ],
   "id": "t07p-035"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The parent `Shape` stores the name. Each child calls `super().__init__(\"rectangle\")` with its own name and defines `area()`. The area of a triangle is base × height / 2.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "import math\n",
    "\n",
    "class Shape:\n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "\n",
    "    def area(self):\n",
    "        return 0\n",
    "\n",
    "class Rectangle(Shape):\n",
    "    def __init__(self, width, height):\n",
    "        super().__init__(\"rectangle\")\n",
    "        self.width = width\n",
    "        self.height = height\n",
    "\n",
    "    def area(self):\n",
    "        return self.width * self.height\n",
    "\n",
    "class Circle(Shape):\n",
    "    def __init__(self, radius):\n",
    "        super().__init__(\"circle\")\n",
    "        self.radius = radius\n",
    "\n",
    "    def area(self):\n",
    "        return math.pi * self.radius ** 2\n",
    "\n",
    "class Triangle(Shape):\n",
    "    def __init__(self, base, height):\n",
    "        super().__init__(\"triangle\")\n",
    "        self.base = base\n",
    "        self.height = height\n",
    "\n",
    "    def area(self):\n",
    "        return self.base * self.height / 2\n",
    "\n",
    "shapes = [Rectangle(4, 3), Circle(2), Triangle(6, 5)]\n",
    "largest = max(shapes, key=lambda shape: shape.area())\n",
    "print(largest.name)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "triangle\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t07p-036"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 12. Employees, managers and interns\n",
    "\n",
    "Write a class `Employee` with `pay()`, and child classes `Manager` (salary plus bonus) and `Intern` (half of the salary). Print the total pay of a list of staff."
   ],
   "id": "t07p-037"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your classes here\n",
    "\n",
    "\n",
    "staff = [Employee(\"Aylin\", 1000), Manager(\"Murad\", 1500, 400), Intern(\"Leyla\", 600)]\n",
    "print(sum(person.pay() for person in staff))"
   ],
   "id": "t07p-038"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`Employee` stores the name and the salary, and `pay()` returns the salary. `Manager` adds a bonus in its own `__init__` (with `super().__init__`) and overrides `pay`. `Intern` only overrides `pay`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "class Employee:\n",
    "    def __init__(self, name, salary):\n",
    "        self.name = name\n",
    "        self._salary = salary\n",
    "\n",
    "    def pay(self):\n",
    "        return self._salary\n",
    "\n",
    "class Manager(Employee):\n",
    "    def __init__(self, name, salary, bonus):\n",
    "        super().__init__(name, salary)\n",
    "        self._bonus = bonus\n",
    "\n",
    "    def pay(self):\n",
    "        return self._salary + self._bonus\n",
    "\n",
    "class Intern(Employee):\n",
    "    def pay(self):\n",
    "        return self._salary / 2\n",
    "\n",
    "staff = [Employee(\"Aylin\", 1000), Manager(\"Murad\", 1500, 400), Intern(\"Leyla\", 600)]\n",
    "print(sum(person.pay() for person in staff))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "3200.0\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t07p-039"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 13. A stack\n",
    "\n",
    "Write a class `Stack` with `push(item)`, `pop()` and `is_empty()`. `pop()` on an empty stack raises an `IndexError` with your own message: `the stack is empty`."
   ],
   "id": "t07p-040"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your class here\n",
    "\n",
    "\n",
    "stack = Stack()\n",
    "stack.push(1)\n",
    "stack.push(2)\n",
    "print(stack.pop())\n",
    "print(stack.is_empty())\n",
    "print(stack.pop())\n",
    "print(stack.is_empty())\n",
    "try:\n",
    "    stack.pop()\n",
    "except IndexError as error:\n",
    "    print(error)"
   ],
   "id": "t07p-041"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Keep the items in `self._items = []`. `push` appends, `pop` removes and returns the last item. In `pop`, first check `if self.is_empty():` and `raise IndexError(\"the stack is empty\")`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "class Stack:\n",
    "    def __init__(self):\n",
    "        self._items = []\n",
    "\n",
    "    def push(self, item):\n",
    "        self._items.append(item)\n",
    "\n",
    "    def pop(self):\n",
    "        if self.is_empty():\n",
    "            raise IndexError(\"the stack is empty\")\n",
    "        return self._items.pop()\n",
    "\n",
    "    def is_empty(self):\n",
    "        return len(self._items) == 0\n",
    "\n",
    "stack = Stack()\n",
    "stack.push(1)\n",
    "stack.push(2)\n",
    "print(stack.pop())\n",
    "print(stack.is_empty())\n",
    "print(stack.pop())\n",
    "print(stack.is_empty())\n",
    "try:\n",
    "    stack.pop()\n",
    "except IndexError as error:\n",
    "    print(error)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "2\n",
    "False\n",
    "1\n",
    "True\n",
    "the stack is empty\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t07p-042"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 14. An inventory\n",
    "\n",
    "Write a class `Inventory` that keeps products and their amounts in an internal dictionary. Methods: `add(name, amount)`, `remove(name, amount)` (it raises a `ValueError` if there is not enough), and `report()` that returns the names in alphabetical order. The error message is `not enough` and the name, like `not enough notebook`."
   ],
   "id": "t07p-043"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your class here\n",
    "\n",
    "\n",
    "inventory = Inventory()\n",
    "inventory.add(\"pen\", 10)\n",
    "inventory.add(\"notebook\", 5)\n",
    "inventory.add(\"pen\", 5)\n",
    "inventory.remove(\"pen\", 3)\n",
    "try:\n",
    "    inventory.remove(\"notebook\", 9)\n",
    "except ValueError as error:\n",
    "    print(error)\n",
    "print(inventory.report())"
   ],
   "id": "t07p-044"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`add` uses the counting pattern: `self._products[name] = self._products.get(name, 0) + amount`. `remove` checks the amount first and raises. `report` returns `sorted(self._products)`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "class Inventory:\n",
    "    def __init__(self):\n",
    "        self._products = {}\n",
    "\n",
    "    def add(self, name, amount):\n",
    "        self._products[name] = self._products.get(name, 0) + amount\n",
    "\n",
    "    def remove(self, name, amount):\n",
    "        if self._products.get(name, 0) < amount:\n",
    "            raise ValueError(\"not enough \" + name)\n",
    "        self._products[name] = self._products[name] - amount\n",
    "\n",
    "    def report(self):\n",
    "        return sorted(self._products)\n",
    "\n",
    "inventory = Inventory()\n",
    "inventory.add(\"pen\", 10)\n",
    "inventory.add(\"notebook\", 5)\n",
    "inventory.add(\"pen\", 5)\n",
    "inventory.remove(\"pen\", 3)\n",
    "try:\n",
    "    inventory.remove(\"notebook\", 9)\n",
    "except ValueError as error:\n",
    "    print(error)\n",
    "print(inventory.report())\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "not enough notebook\n",
    "['notebook', 'pen']\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t07p-045"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 15. A clock\n",
    "\n",
    "Write a class `Clock` with hours and minutes and a method `tick()` that adds one minute. After 23:59 comes 00:00. `__str__` shows the time like `09:05`.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `9 4` | `09:05` |\n",
    "| `23 59` | `00:00` |\n",
    "| `0 0` | `00:01` |\n",
    "| `12 59` | `13:00` |\n",
    "| `7 30` | `07:31` |"
   ],
   "id": "t07p-046"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "9 4"
     ]
    }
   },
   "outputs": [],
   "source": [
    "# Write your class here\n",
    "\n",
    "\n",
    "hours, minutes = map(int, input().split())\n",
    "clock = Clock(hours, minutes)\n",
    "clock.tick()\n",
    "print(clock)"
   ],
   "id": "t07p-047"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "In `tick`, add 1 to the minutes. If they reach 60, set them to 0 and add 1 to the hours. If the hours reach 24, set them to 0. Two digits with a leading zero: `f\"{self.hours:02}:{self.minutes:02}\"`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "class Clock:\n",
    "    def __init__(self, hours, minutes):\n",
    "        self.hours = hours\n",
    "        self.minutes = minutes\n",
    "\n",
    "    def tick(self):\n",
    "        self.minutes = self.minutes + 1\n",
    "        if self.minutes == 60:\n",
    "            self.minutes = 0\n",
    "            self.hours = self.hours + 1\n",
    "        if self.hours == 24:\n",
    "            self.hours = 0\n",
    "\n",
    "    def __str__(self):\n",
    "        return f\"{self.hours:02}:{self.minutes:02}\"\n",
    "\n",
    "hours, minutes = map(int, input().split())\n",
    "clock = Clock(hours, minutes)\n",
    "clock.tick()\n",
    "print(clock)\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `9 4` | `09:05` |\n",
    "| `23 59` | `00:00` |\n",
    "| `0 0` | `00:01` |\n",
    "| `12 59` | `13:00` |\n",
    "| `7 30` | `07:31` |\n",
    "\n",
    "</details>"
   ],
   "id": "t07p-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": "t07p-049"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
