{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Topic 3: Practice problems (loops and functions)\n",
    "\n",
    "Asiman Data School, Module 1: Foundations of Python Programming. Week 2, 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. Most problems ask for a function: write it above the lines that are already in the cell. Those lines read the input and call your function, so until the function is written the cell stops with a NameError. That is expected.\n",
    "- When the cell runs, a small box appears. Type the value there and press Enter.\n",
    "- Run your code with every row of the examples table and compare the output. Print exactly the text 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 solve a small example by hand, then decide the function name, its parameters, and what it returns. Test with edge values such as 0 and 1."
   ],
   "id": "t03p-000"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Easy"
   ],
   "id": "t03p-001"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 1. Volume of a cube\n",
    "\n",
    "Write a function `cube_volume(side)` that returns the volume of a cube. The volume is side × side × side.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `3` | `27` |\n",
    "| `1` | `1` |\n",
    "| `10` | `1000` |\n",
    "| `0` | `0` |"
   ],
   "id": "t03p-002"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your function here\n",
    "\n",
    "\n",
    "side = int(input())\n",
    "print(cube_volume(side))"
   ],
   "id": "t03p-003"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The function needs one parameter and one line: `return side * side * side`. Return the value, do not print it inside the function.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "def cube_volume(side):\n",
    "    return side * side * side\n",
    "\n",
    "side = int(input())\n",
    "print(cube_volume(side))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `3` | `27` |\n",
    "| `1` | `1` |\n",
    "| `10` | `1000` |\n",
    "| `0` | `0` |\n",
    "\n",
    "</details>"
   ],
   "id": "t03p-004"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 2. Positive or not\n",
    "\n",
    "Write a function `is_positive(n)` that returns `True` or `False`. Zero is not positive.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `5` | `True` |\n",
    "| `-3` | `False` |\n",
    "| `0` | `False` |\n",
    "| `1` | `True` |\n",
    "| `-1` | `False` |"
   ],
   "id": "t03p-005"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your function here\n",
    "\n",
    "\n",
    "n = int(input())\n",
    "print(is_positive(n))"
   ],
   "id": "t03p-006"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "A comparison already gives `True` or `False`, so the body can be one line: `return n > 0`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "def is_positive(n):\n",
    "    return n > 0\n",
    "\n",
    "n = int(input())\n",
    "print(is_positive(n))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `5` | `True` |\n",
    "| `-3` | `False` |\n",
    "| `0` | `False` |\n",
    "| `1` | `True` |\n",
    "| `-1` | `False` |\n",
    "\n",
    "</details>"
   ],
   "id": "t03p-007"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 3. Count the letter a\n",
    "\n",
    "Count how many times the letter `\"a\"` appears in a word. Read the word and print the count.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `banana` | `3` |\n",
    "| `apple` | `1` |\n",
    "| `sky` | `0` |\n",
    "| `Anna` | `1` |\n",
    "| `a` | `1` |"
   ],
   "id": "t03p-008"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "word = input()\n",
    "# Write your code here"
   ],
   "id": "t03p-009"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The counter pattern: `count = 0` before the loop, then `for letter in word:` and add 1 when `letter == \"a\"`. A capital `A` is a different character.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "word = input()\n",
    "\n",
    "count = 0\n",
    "for letter in word:\n",
    "    if letter == \"a\":\n",
    "        count = count + 1\n",
    "\n",
    "print(count)\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `banana` | `3` |\n",
    "| `apple` | `1` |\n",
    "| `sky` | `0` |\n",
    "| `Anna` | `1` |\n",
    "| `a` | `1` |\n",
    "\n",
    "</details>"
   ],
   "id": "t03p-010"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Medium"
   ],
   "id": "t03p-011"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 4. Sum from 1 to n, two ways\n",
    "\n",
    "Write a function that returns the sum of all numbers from 1 to `n`, using a loop. Then write it again using recursion. Name them `sum_to(n)` and `sum_to_recursive(n)`.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `10` | `55 55` |\n",
    "| `1` | `1 1` |\n",
    "| `0` | `0 0` |\n",
    "| `100` | `5050 5050` |"
   ],
   "id": "t03p-012"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write sum_to(n) with a loop and sum_to_recursive(n) with recursion\n",
    "\n",
    "\n",
    "n = int(input())\n",
    "print(sum_to(n), sum_to_recursive(n))"
   ],
   "id": "t03p-013"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Loop: an accumulator that starts at 0. Recursion: the sum up to `n` is `n` plus the sum up to `n - 1`, and the base case is `n <= 0`, which returns 0.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "def sum_to(n):\n",
    "    total = 0\n",
    "    for i in range(1, n + 1):\n",
    "        total = total + i\n",
    "    return total\n",
    "\n",
    "def sum_to_recursive(n):\n",
    "    if n <= 0:                          # base case\n",
    "        return 0\n",
    "    return n + sum_to_recursive(n - 1)  # recursive case\n",
    "\n",
    "n = int(input())\n",
    "print(sum_to(n), sum_to_recursive(n))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `10` | `55 55` |\n",
    "| `1` | `1 1` |\n",
    "| `0` | `0 0` |\n",
    "| `100` | `5050 5050` |\n",
    "\n",
    "</details>"
   ],
   "id": "t03p-014"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 5. Count the digits, two ways\n",
    "\n",
    "Write a function `count_digits(n)` using a `while` loop, then write a recursive version, `count_digits_recursive(n)`. The number 0 has 1 digit.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `98765` | `5 5` |\n",
    "| `7` | `1 1` |\n",
    "| `0` | `1 1` |\n",
    "| `1000` | `4 4` |\n",
    "| `10` | `2 2` |"
   ],
   "id": "t03p-015"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write count_digits(n) with a while loop and count_digits_recursive(n) with recursion\n",
    "\n",
    "\n",
    "n = int(input())\n",
    "print(count_digits(n), count_digits_recursive(n))"
   ],
   "id": "t03p-016"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Loop: `n // 10` removes the last digit; count how many times you can do it. Handle 0 before the loop. Recursion: a number below 10 has 1 digit; otherwise it is 1 plus the digits of `n // 10`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "def count_digits(n):\n",
    "    if n == 0:\n",
    "        return 1\n",
    "    count = 0\n",
    "    while n > 0:\n",
    "        count = count + 1\n",
    "        n = n // 10\n",
    "    return count\n",
    "\n",
    "def count_digits_recursive(n):\n",
    "    if n < 10:                                   # base case: one digit\n",
    "        return 1\n",
    "    return 1 + count_digits_recursive(n // 10)   # recursive case\n",
    "\n",
    "n = int(input())\n",
    "print(count_digits(n), count_digits_recursive(n))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `98765` | `5 5` |\n",
    "| `7` | `1 1` |\n",
    "| `0` | `1 1` |\n",
    "| `1000` | `4 4` |\n",
    "| `10` | `2 2` |\n",
    "\n",
    "</details>"
   ],
   "id": "t03p-017"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 6. Perfect square\n",
    "\n",
    "Write a function `is_perfect_square(n)` using a loop (do not use `**0.5`). It returns `True` for 0, 1, 4, 9, 16 and so on.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `16` | `True` |\n",
    "| `15` | `False` |\n",
    "| `1` | `True` |\n",
    "| `0` | `True` |\n",
    "| `2` | `False` |\n",
    "| `144` | `True` |"
   ],
   "id": "t03p-018"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your function here\n",
    "\n",
    "\n",
    "n = int(input())\n",
    "print(is_perfect_square(n))"
   ],
   "id": "t03p-019"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Try `i = 0, 1, 2, ...` while `i * i <= n`. If `i * i == n`, return `True` at once. If the loop ends without a match, return `False`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "def is_perfect_square(n):\n",
    "    i = 0\n",
    "    while i * i <= n:\n",
    "        if i * i == n:\n",
    "            return True\n",
    "        i = i + 1\n",
    "    return False\n",
    "\n",
    "n = int(input())\n",
    "print(is_perfect_square(n))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `16` | `True` |\n",
    "| `15` | `False` |\n",
    "| `1` | `True` |\n",
    "| `0` | `True` |\n",
    "| `2` | `False` |\n",
    "| `144` | `True` |\n",
    "\n",
    "</details>"
   ],
   "id": "t03p-020"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 7. Armstrong numbers of any length\n",
    "\n",
    "Write a function that checks whether a number is an Armstrong number with any number of digits: `is_armstrong(n)`. A number with d digits is an Armstrong number when the sum of its digits, each raised to the power d, equals the number. Example: 9474 = 9⁴ + 4⁴ + 7⁴ + 4⁴.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `153` | `True` |\n",
    "| `9474` | `True` |\n",
    "| `9475` | `False` |\n",
    "| `5` | `True` |\n",
    "| `10` | `False` |\n",
    "| `370` | `True` |"
   ],
   "id": "t03p-021"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your function (and any helper function) here\n",
    "\n",
    "\n",
    "n = int(input())\n",
    "print(is_armstrong(n))"
   ],
   "id": "t03p-022"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "First count the digits (a small helper function). Then use the digit trick `% 10` and `// 10`, and add `digit ** digits` each time. Save the original number before the loop destroys it.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "def count_digits(n):\n",
    "    count = 0\n",
    "    while n > 0:\n",
    "        count = count + 1\n",
    "        n = n // 10\n",
    "    return count\n",
    "\n",
    "def is_armstrong(n):\n",
    "    digits = count_digits(n)\n",
    "    original = n\n",
    "    total = 0\n",
    "    while n > 0:\n",
    "        total = total + (n % 10) ** digits\n",
    "        n = n // 10\n",
    "    return total == original\n",
    "\n",
    "n = int(input())\n",
    "print(is_armstrong(n))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `153` | `True` |\n",
    "| `9474` | `True` |\n",
    "| `9475` | `False` |\n",
    "| `5` | `True` |\n",
    "| `10` | `False` |\n",
    "| `370` | `True` |\n",
    "\n",
    "</details>"
   ],
   "id": "t03p-023"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Medium-Hard"
   ],
   "id": "t03p-024"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 8. Primes up to 200\n",
    "\n",
    "Print all the prime numbers between 1 and 200 and count how many there are. Print the primes on one line separated by spaces, then `Count:` and the number on the next line."
   ],
   "id": "t03p-025"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t03p-026"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Reuse `is_prime(n)` from solved problem 16. Loop over `range(1, 201)`, print each prime with `end=\" \"` and add 1 to a counter. An empty `print()` ends the line.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "def is_prime(n):\n",
    "    if n < 2:\n",
    "        return False\n",
    "    divisor = 2\n",
    "    while divisor * divisor <= n:\n",
    "        if n % divisor == 0:\n",
    "            return False\n",
    "        divisor = divisor + 1\n",
    "    return True\n",
    "\n",
    "count = 0\n",
    "for number in range(1, 201):\n",
    "    if is_prime(number):\n",
    "        print(number, end=\" \")\n",
    "        count = count + 1\n",
    "print()\n",
    "print(\"Count:\", count)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 \n",
    "Count: 46\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t03p-027"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 9. Power with recursion\n",
    "\n",
    "Write `power_recursive(base, exponent)` using recursion. The exponent is 0 or more.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `2, 10` | `1024` |\n",
    "| `5, 0` | `1` |\n",
    "| `3, 4` | `81` |\n",
    "| `7, 1` | `7` |"
   ],
   "id": "t03p-028"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your function here\n",
    "\n",
    "\n",
    "base = int(input())\n",
    "exponent = int(input())\n",
    "print(power_recursive(base, exponent))"
   ],
   "id": "t03p-029"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Base case: any number to the power 0 is 1. Recursive case: `base` times the power with `exponent - 1`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "def power_recursive(base, exponent):\n",
    "    if exponent == 0:                                  # base case\n",
    "        return 1\n",
    "    return base * power_recursive(base, exponent - 1)  # recursive case\n",
    "\n",
    "base = int(input())\n",
    "exponent = int(input())\n",
    "print(power_recursive(base, exponent))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `2, 10` | `1024` |\n",
    "| `5, 0` | `1` |\n",
    "| `3, 4` | `81` |\n",
    "| `7, 1` | `7` |\n",
    "\n",
    "</details>"
   ],
   "id": "t03p-030"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 10. Halving until odd\n",
    "\n",
    "Write a function that returns how many times you can divide a number by 2 before it becomes odd: `count_halvings(n)`, for a positive whole number. Example: 12, 6, 3 takes 2 divisions.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `8` | `3` |\n",
    "| `12` | `2` |\n",
    "| `7` | `0` |\n",
    "| `1` | `0` |\n",
    "| `40` | `3` |"
   ],
   "id": "t03p-031"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your function here\n",
    "\n",
    "\n",
    "n = int(input())\n",
    "print(count_halvings(n))"
   ],
   "id": "t03p-032"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "You do not know the number of repeats in advance, so use `while`. The condition is `n % 2 == 0`; inside, divide with `//` and count.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "def count_halvings(n):\n",
    "    count = 0\n",
    "    while n % 2 == 0:\n",
    "        n = n // 2\n",
    "        count = count + 1\n",
    "    return count\n",
    "\n",
    "n = int(input())\n",
    "print(count_halvings(n))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `8` | `3` |\n",
    "| `12` | `2` |\n",
    "| `7` | `0` |\n",
    "| `1` | `0` |\n",
    "| `40` | `3` |\n",
    "\n",
    "</details>"
   ],
   "id": "t03p-033"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 11. Largest digit\n",
    "\n",
    "Write a function that finds the largest digit of a number: `largest_digit(n)`.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `482915` | `9` |\n",
    "| `7` | `7` |\n",
    "| `1000` | `1` |\n",
    "| `0` | `0` |\n",
    "| `5550` | `5` |"
   ],
   "id": "t03p-034"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your function here\n",
    "\n",
    "\n",
    "n = int(input())\n",
    "print(largest_digit(n))"
   ],
   "id": "t03p-035"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Keep the best digit so far in a variable that starts at 0. Take the digits one by one with `% 10` and `// 10`, and replace the best one when a digit is larger.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "def largest_digit(n):\n",
    "    largest = 0\n",
    "    while n > 0:\n",
    "        digit = n % 10\n",
    "        if digit > largest:\n",
    "            largest = digit\n",
    "        n = n // 10\n",
    "    return largest\n",
    "\n",
    "n = int(input())\n",
    "print(largest_digit(n))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `482915` | `9` |\n",
    "| `7` | `7` |\n",
    "| `1000` | `1` |\n",
    "| `0` | `0` |\n",
    "| `5550` | `5` |\n",
    "\n",
    "</details>"
   ],
   "id": "t03p-036"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 12. Number guessing game\n",
    "\n",
    "Write a \"number guessing game\" with `while`: the program has a secret number and asks the user until they guess correctly, giving \"too high\" or \"too low\" hints, and finally prints the number of attempts."
   ],
   "id": "t03p-037"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "secret = 37\n",
    "# Write your game here"
   ],
   "id": "t03p-038"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Repeat until valid: `while guess != secret:`. Inside the loop read the guess with `int(input(...))`, add 1 to the attempts, then compare. To play here, type your guesses in the input box, one per line.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "secret = 37\n",
    "attempts = 0\n",
    "guess = 0\n",
    "\n",
    "while guess != secret:\n",
    "    guess = int(input(\"Your guess: \"))\n",
    "    attempts = attempts + 1\n",
    "    if guess > secret:\n",
    "        print(\"Too high\")\n",
    "    elif guess < secret:\n",
    "        print(\"Too low\")\n",
    "\n",
    "print(\"Correct! Attempts:\", attempts)\n",
    "```\n",
    "\n",
    "**Sample run** (typed: `50`, `25`, `37`)\n",
    "\n",
    "```\n",
    "Your guess: 50\n",
    "Too high\n",
    "Your guess: 25\n",
    "Too low\n",
    "Your guess: 37\n",
    "Correct! Attempts: 3\n",
    "```\n",
    "\n",
    "Your messages can be worded differently. What matters is that the loop ends only on the right guess and the number of attempts is correct.\n",
    "\n",
    "</details>"
   ],
   "id": "t03p-039"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 13. Diamond of stars\n",
    "\n",
    "Print a diamond pattern of stars using loops. Use `rows = 4`: the lines have 1, 3, 5, 7, 5, 3 and 1 stars, and each line is centered."
   ],
   "id": "t03p-040"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rows = 4\n",
    "# Write your code here"
   ],
   "id": "t03p-041"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The top half is the pyramid of solved problem 27. The bottom half is the same loop counting down: `range(rows - 1, 0, -1)`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "rows = 4\n",
    "\n",
    "for i in range(1, rows + 1):\n",
    "    print(\" \" * (rows - i) + \"*\" * (2 * i - 1))\n",
    "\n",
    "for i in range(rows - 1, 0, -1):\n",
    "    print(\" \" * (rows - i) + \"*\" * (2 * i - 1))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "   *\n",
    "  ***\n",
    " *****\n",
    "*******\n",
    " *****\n",
    "  ***\n",
    "   *\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t03p-042"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 14. Same digit sum\n",
    "\n",
    "Write a function that returns `True` if two numbers have exactly the same digits sum: `same_digit_sum(a, b)`. Example: 123 and 60 both give 6.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `123, 321` | `True` |\n",
    "| `123, 60` | `True` |\n",
    "| `19, 28` | `True` |\n",
    "| `15, 25` | `False` |\n",
    "| `0, 0` | `True` |\n",
    "| `99, 18` | `False` |"
   ],
   "id": "t03p-043"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your functions here\n",
    "\n",
    "\n",
    "a = int(input())\n",
    "b = int(input())\n",
    "print(same_digit_sum(a, b))"
   ],
   "id": "t03p-044"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Write `sum_of_digits(n)` first (solved problem 13), then compare the two results inside `same_digit_sum`. One function calls the other.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "def sum_of_digits(n):\n",
    "    total = 0\n",
    "    while n > 0:\n",
    "        total = total + n % 10\n",
    "        n = n // 10\n",
    "    return total\n",
    "\n",
    "def same_digit_sum(a, b):\n",
    "    return sum_of_digits(a) == sum_of_digits(b)\n",
    "\n",
    "a = int(input())\n",
    "b = int(input())\n",
    "print(same_digit_sum(a, b))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `123, 321` | `True` |\n",
    "| `123, 60` | `True` |\n",
    "| `19, 28` | `True` |\n",
    "| `15, 25` | `False` |\n",
    "| `0, 0` | `True` |\n",
    "| `99, 18` | `False` |\n",
    "\n",
    "</details>"
   ],
   "id": "t03p-045"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 15. Sum of a series\n",
    "\n",
    "Write a function that calculates the sum of the series `1/1 + 1/2 + 1/3 + ... + 1/n`: `harmonic(n)`. The last line prints the result rounded to 3 decimal places.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `4` | `2.083` |\n",
    "| `1` | `1.0` |\n",
    "| `2` | `1.5` |\n",
    "| `10` | `2.929` |\n",
    "| `100` | `5.187` |"
   ],
   "id": "t03p-046"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your function here\n",
    "\n",
    "\n",
    "n = int(input())\n",
    "print(round(harmonic(n), 3))"
   ],
   "id": "t03p-047"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "An accumulator that starts at 0. Loop `i` from 1 to `n` and add `1 / i` each time.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "def harmonic(n):\n",
    "    total = 0\n",
    "    for i in range(1, n + 1):\n",
    "        total = total + 1 / i\n",
    "    return total\n",
    "\n",
    "n = int(input())\n",
    "print(round(harmonic(n), 3))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `4` | `2.083` |\n",
    "| `1` | `1.0` |\n",
    "| `2` | `1.5` |\n",
    "| `10` | `2.929` |\n",
    "| `100` | `5.187` |\n",
    "\n",
    "</details>"
   ],
   "id": "t03p-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": "t03p-049"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
