{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "3410e01a",
      "metadata": {},
      "source": [
        "# Control Flow Practice Questions (Instructor Solutions)\n",
        "\n",
        "This notebook contains 10 short control-flow practice problems with clear, testable solutions."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b2f3fc82",
      "metadata": {},
      "outputs": [],
      "source": [
        "name = \"student name\"\n",
        "roll_number = \"student roll number\""
      ]
    },
    {
      "cell_type": "markdown",
      "id": "5f8c14f2",
      "metadata": {},
      "source": [
        "### 1. Write a program to check if a number is **positive, negative, or zero**."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b7c16284",
      "metadata": {},
      "outputs": [],
      "source": [
        "def check_number(n: int) -> str:\n",
        "    \"\"\"Return 'positive', 'negative' or 'zero' for the given number.\"\"\"\n",
        "    if n > 0:\n",
        "        return 'positive'\n",
        "    if n < 0:\n",
        "        return 'negative'\n",
        "    return 'zero'\n",
        "\n",
        "# check_number(10)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9259b2ec",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Assertions\n",
        "assert check_number(10) == 'positive'\n",
        "assert check_number(-5) == 'negative'\n",
        "assert check_number(0) == 'zero'"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "269b03e5",
      "metadata": {},
      "source": [
        "### 2. Write a program that prints all numbers from **1 to 10** using a `for` loop."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a856a39d",
      "metadata": {},
      "outputs": [],
      "source": [
        "def numbers_1_to_10() -> list[int]:\n",
        "    \"\"\"Return the numbers 1..10 using a for loop (collected into a list for testability).\"\"\"\n",
        "    out = []\n",
        "    for i in range(1, 11):\n",
        "        out.append(i)\n",
        "    return out\n",
        "\n",
        "# numbers_1_to_10()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "85201dfa",
      "metadata": {},
      "outputs": [],
      "source": [
        "assert numbers_1_to_10() == list(range(1, 11))"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cff29bcc",
      "metadata": {},
      "source": [
        "### 3. Write a program that prints only **even numbers** from 1 to 20."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "48672919",
      "metadata": {},
      "outputs": [],
      "source": [
        "def even_1_to_20() -> list[int]:\n",
        "    out = []\n",
        "    for i in range(1, 21):\n",
        "        if i % 2 == 0:\n",
        "            out.append(i)\n",
        "    return out\n",
        "\n",
        "# even_1_to_20()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "df2d918d",
      "metadata": {},
      "outputs": [],
      "source": [
        "assert even_1_to_20() == [2,4,6,8,10,12,14,16,18,20]"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "6bd1ddc6",
      "metadata": {},
      "source": [
        "### 4. Write a program that checks if a given year is a **leap year**."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b3ab6d3b",
      "metadata": {},
      "outputs": [],
      "source": [
        "def is_leap_year(year: int) -> bool:\n",
        "    \"\"\"Return True if year is a leap year (Gregorian rules).\"\"\"\n",
        "    if year % 400 == 0:\n",
        "        return True\n",
        "    if year % 100 == 0:\n",
        "        return False\n",
        "    return year % 4 == 0\n",
        "\n",
        "# is_leap_year(2024)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a94c49f3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# tests\n",
        "assert is_leap_year(2000) is True\n",
        "assert is_leap_year(1900) is False\n",
        "assert is_leap_year(2024) is True\n",
        "assert is_leap_year(2023) is False"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "964b3649",
      "metadata": {},
      "source": [
        "### 5. Write a program that keeps asking for a password until the user enters \"python\"."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "049cae9c",
      "metadata": {},
      "outputs": [],
      "source": [
        "def ask_password(simulated_inputs: list[str] | None = None) -> str:\n",
        "    \"\"\"Loop until password 'python' is entered.\n",
        "    If simulated_inputs is provided (for tests), consume values from it instead of input().\"\"\"\n",
        "    target = 'python'\n",
        "    if simulated_inputs is not None:\n",
        "        for attempt in simulated_inputs:\n",
        "            if attempt == target:\n",
        "                return attempt\n",
        "        # if not found, raise for test clarity\n",
        "        raise ValueError('simulated inputs did not contain the correct password')\n",
        "    # interactive mode (won't be run in automated tests)\n",
        "    while True:\n",
        "        val = input('Enter password: ')\n",
        "        if val == target:\n",
        "            return val\n",
        "\n",
        "# ask_password(['nope','123','python'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "bae237c9",
      "metadata": {},
      "outputs": [],
      "source": [
        "# tests (non-interactive)\n",
        "assert ask_password(['nope','123','python']) == 'python'"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "57197c2b",
      "metadata": {},
      "source": [
        "### 6. Write a program that counts the number of vowels in a given word."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c88b8f75",
      "metadata": {},
      "outputs": [],
      "source": [
        "def count_vowels(word: str) -> int:\n",
        "    vowels = set('aeiouAEIOU')\n",
        "    count = 0\n",
        "    for ch in word:\n",
        "        if ch in vowels:\n",
        "            count += 1\n",
        "    return count\n",
        "\n",
        "# count_vowels('hello')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cfa3fdc0",
      "metadata": {},
      "outputs": [],
      "source": [
        "assert count_vowels('hello') == 2\n",
        "assert count_vowels('rhythm') == 0\n",
        "assert count_vowels('AEIOU') == 5"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "f59e9410",
      "metadata": {},
      "source": [
        "### 7. Write a program that prints the multiplication table for a given number."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d08a8ddf",
      "metadata": {},
      "outputs": [],
      "source": [
        "def multiplication_table(n: int, upto: int = 10) -> list[str]:\n",
        "    \"\"\"Return multiplication table lines for n from 1..upto as strings.\"\"\"\n",
        "    lines = []\n",
        "    for i in range(1, upto + 1):\n",
        "        lines.append(f'{} x {} = {}'.format(n, i, n * i))\n",
        "    return lines\n",
        "\n",
        "# multiplication_table(3, 5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "014c179f",
      "metadata": {},
      "outputs": [],
      "source": [
        "# brief test\n",
        "assert multiplication_table(3, 5)[0] == '3 x 1 = 3'\n",
        "assert multiplication_table(3, 5)[-1] == '3 x 5 = 15'"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ed5e2ee9",
      "metadata": {},
      "source": [
        "### 8. Write a program to **sum all numbers** from 1 to 100 using a loop."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4bf8d1f5",
      "metadata": {},
      "outputs": [],
      "source": [
        "def sum_1_to_100_loop() -> int:\n",
        "    total = 0\n",
        "    for i in range(1, 101):\n",
        "        total += i\n",
        "    return total\n",
        "\n",
        "# sum_1_to_100_loop()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4e18ea4e",
      "metadata": {},
      "outputs": [],
      "source": [
        "assert sum_1_to_100_loop() == 5050"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "bc9079c8",
      "metadata": {},
      "source": [
        "### 9. Write a program to find the **largest number** from a list of 5 numbers entered by the user."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "59e8076f",
      "metadata": {},
      "outputs": [],
      "source": [
        "def largest_of_five(nums: list[float]) -> float:\n",
        "    if len(nums) != 5:\n",
        "        raise ValueError('expected list of 5 numbers')\n",
        "    largest = nums[0]\n",
        "    for v in nums[1:]:\n",
        "        if v > largest:\n",
        "            largest = v\n",
        "    return largest\n",
        "\n",
        "# largest_of_five([1,2,3,4,5])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "22114f46",
      "metadata": {},
      "outputs": [],
      "source": [
        "# test\n",
        "assert largest_of_five([1,2,3,4,5]) == 5\n",
        "assert largest_of_five([10,9,8,7,6]) == 10"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "d21467a9",
      "metadata": {},
      "source": [
        "### 10. Bonus Fun One 🎭: Ask the user’s mood — if \"happy\", print \"Keep smiling!\"; if \"sad\", print \"Cheer up, Python believes in you!\"; else, print \"Processing emotions... try again.\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "0d982d56",
      "metadata": {},
      "outputs": [],
      "source": [
        "def mood_response(mood: str) -> str:\n",
        "    m = mood.strip().lower()\n",
        "    if m == 'happy':\n",
        "        return 'Keep smiling!'\n",
        "    if m == 'sad':\n",
        "        return 'Cheer up, Python believes in you!'\n",
        "    return 'Processing emotions... try again.'\n",
        "\n",
        "# mood_response('happy')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5e5e8bb9",
      "metadata": {},
      "outputs": [],
      "source": [
        "# tests\n",
        "assert mood_response('happy') == 'Keep smiling!'\n",
        "assert mood_response(' sad ') == 'Cheer up, Python believes in you!'\n",
        "assert mood_response('confused') == 'Processing emotions... try again.'"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
