{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3b350b7a",
   "metadata": {},
   "source": [
    "# Control Flow Assessment (Instructor Solutions)\n",
    "\n",
    "This notebook contains **15 Python practice problems** focused on **if/elif/else statements, for loops, and lists**.\n",
    "\n",
    "**Difficulty Distribution:**\n",
    "- **4 Extremely Easy Questions** (Questions 1-4)\n",
    "- **6 Intermediate Questions** (Questions 5-10)\n",
    "- **5 Extremely Hard Questions** (Questions 11-15)\n",
    "\n",
    "Each question includes:\n",
    "- Function definition with correct return statements\n",
    "- Clear explanation of what the function should return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc951a38",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = 'student name'\n",
    "roll_number = 'student roll number'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "283bacce",
   "metadata": {},
   "source": [
    "### 1. Check if a number is even\n",
    "**Return:** `True` if the number is even, `False` otherwise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be1f8b36",
   "metadata": {},
   "outputs": [],
   "source": [
    "def is_even(n: int) -> bool:\n",
    "    if n % 2 == 0:\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "# is_even(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16f5c7ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert is_even(4) == True\n",
    "assert is_even(7) == False\n",
    "assert is_even(0) == True"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e631070",
   "metadata": {},
   "source": [
    "### 2. Return the maximum of two numbers\n",
    "**Return:** The larger of two numbers. If they are equal, return either."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6acd4f86",
   "metadata": {},
   "outputs": [],
   "source": [
    "def max_of_two(a: int, b: int) -> int:\n",
    "    if a > b:\n",
    "        return a\n",
    "    else:\n",
    "        return b\n",
    "\n",
    "# max_of_two(5, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34eaf221",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert max_of_two(5, 10) == 10\n",
    "assert max_of_two(20, 15) == 20\n",
    "assert max_of_two(7, 7) == 7"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e6374fe",
   "metadata": {},
   "source": [
    "### 3. Count elements in a list\n",
    "**Return:** The number of elements in the given list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aade60bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "def count_elements(lst: list) -> int:\n",
    "    count = 0\n",
    "    for item in lst:\n",
    "        count += 1\n",
    "    return count\n",
    "\n",
    "# count_elements([1, 2, 3, 4, 5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c89e0702",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert count_elements([1, 2, 3, 4, 5]) == 5\n",
    "assert count_elements([]) == 0\n",
    "assert count_elements(['a', 'b', 'c']) == 3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9562ae80",
   "metadata": {},
   "source": [
    "### 4. Return sum of all elements in a list\n",
    "**Return:** The sum of all numbers in the list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b431351",
   "metadata": {},
   "outputs": [],
   "source": [
    "def sum_list(lst: list[int]) -> int:\n",
    "    total = 0\n",
    "    for num in lst:\n",
    "        total += num\n",
    "    return total\n",
    "\n",
    "# sum_list([1, 2, 3, 4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e089e3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert sum_list([1, 2, 3, 4]) == 10\n",
    "assert sum_list([10, 20, 30]) == 60\n",
    "assert sum_list([]) == 0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b301f016",
   "metadata": {},
   "source": [
    "### 5. Grade classifier based on marks\n",
    "Given marks, return grade:\n",
    "- 90 and above: 'A'\n",
    "- 80-89: 'B'\n",
    "- 70-79: 'C'\n",
    "- 60-69: 'D'\n",
    "- Below 60: 'F'\n",
    "\n",
    "**Return:** A single character string representing the grade"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd8c6e6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def classify_grade(marks: int) -> str:\n",
    "    if marks >= 90:\n",
    "        return 'A'\n",
    "    elif marks >= 80:\n",
    "        return 'B'\n",
    "    elif marks >= 70:\n",
    "        return 'C'\n",
    "    elif marks >= 60:\n",
    "        return 'D'\n",
    "    else:\n",
    "        return 'F'\n",
    "\n",
    "# classify_grade(85)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ede85b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert classify_grade(95) == 'A'\n",
    "assert classify_grade(85) == 'B'\n",
    "assert classify_grade(75) == 'C'\n",
    "assert classify_grade(65) == 'D'\n",
    "assert classify_grade(55) == 'F'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9397877",
   "metadata": {},
   "source": [
    "### 6. Filter positive numbers from a list\n",
    "**Return:** A new list containing only positive numbers from the input list\n",
    "**Example:** Input `[-5, 3, -1, 7, 0, 2]` → Output `[3, 7, 2]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "edb4bb53",
   "metadata": {},
   "outputs": [],
   "source": [
    "def filter_positive(lst: list[int]) -> list[int]:\n",
    "    result = []\n",
    "    for num in lst:\n",
    "        if num > 0:\n",
    "            result.append(num)\n",
    "    return result\n",
    "\n",
    "# filter_positive([-5, 3, -1, 7, 0, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6244167f",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert filter_positive([-5, 3, -1, 7, 0, 2]) == [3, 7, 2]\n",
    "assert filter_positive([-1, -2, -3]) == []\n",
    "assert filter_positive([1, 2, 3]) == [1, 2, 3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22ca7231",
   "metadata": {},
   "source": [
    "### 7. Reverse a list using a loop\n",
    "**Return:** A new list with elements in reverse order\n",
    "**Example:** Input `[1, 2, 3, 4]` → Output `[4, 3, 2, 1]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bafbb66a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def reverse_list(lst: list) -> list:\n",
    "    reversed_lst = []\n",
    "    for i in range(len(lst) - 1, -1, -1):\n",
    "        reversed_lst.append(lst[i])\n",
    "    return reversed_lst\n",
    "\n",
    "# reverse_list([1, 2, 3, 4, 5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17bed0a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert reverse_list([1, 2, 3, 4]) == [4, 3, 2, 1]\n",
    "assert reverse_list([5]) == [5]\n",
    "assert reverse_list([]) == []"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55769736",
   "metadata": {},
   "source": [
    "### 8. Find the maximum element in a list\n",
    "**Return:** The largest number in the list\n",
    "**Example:** Input `[3, 7, 2, 9, 1]` → Output `9`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cecaea10",
   "metadata": {},
   "outputs": [],
   "source": [
    "def find_max(lst: list[int]) -> int:\n",
    "    if not lst:\n",
    "        return None\n",
    "    max_val = lst[0]\n",
    "    for num in lst:\n",
    "        if num > max_val:\n",
    "            max_val = num\n",
    "    return max_val\n",
    "\n",
    "# find_max([3, 7, 2, 9, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bdb435be",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert find_max([3, 7, 2, 9, 1]) == 9\n",
    "assert find_max([5, 5, 5]) == 5\n",
    "assert find_max([-10, -5, -20]) == -5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4dc758ea",
   "metadata": {},
   "source": [
    "### 9. Count occurrences of a specific element in a list\n",
    "**Return:** The number of times the element appears in the list\n",
    "**Example:** Input `([1, 2, 3, 2, 4, 2], 2)` → Output `3`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8395541f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def count_occurrences(lst: list, element) -> int:\n",
    "    count = 0\n",
    "    for item in lst:\n",
    "        if item == element:\n",
    "            count += 1\n",
    "    return count\n",
    "\n",
    "# count_occurrences([1, 2, 3, 2, 4, 2], 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17713692",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert count_occurrences([1, 2, 3, 2, 4, 2], 2) == 3\n",
    "assert count_occurrences(['a', 'b', 'a', 'c'], 'a') == 2\n",
    "assert count_occurrences([1, 2, 3], 5) == 0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f483e3cd",
   "metadata": {},
   "source": [
    "### 10. Create a multiplication table for a given number\n",
    "**Return:** A list containing the first 10 multiples of the number\n",
    "**Example:** Input `3` → Output `[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea0a0f0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def multiplication_table(n: int) -> list[int]:\n",
    "    result = []\n",
    "    for i in range(1, 11):\n",
    "        result.append(n * i)\n",
    "    return result\n",
    "\n",
    "# multiplication_table(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aaa75ba4",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert multiplication_table(3) == [3, 6, 9, 12, 15, 18, 21, 24, 27, 30]\n",
    "assert multiplication_table(5) == [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]\n",
    "assert multiplication_table(1) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "659c7022",
   "metadata": {},
   "source": [
    "### 11. Find all prime numbers up to n using loops\n",
    "A prime number is a number greater than 1 that has no positive divisors other than 1 and itself.\n",
    "**Return:** A list of all prime numbers from 2 to n (inclusive)\n",
    "**Example:** Input `10` → Output `[2, 3, 5, 7]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "221e6353",
   "metadata": {},
   "outputs": [],
   "source": [
    "def find_primes(n: int) -> list[int]:\n",
    "    primes = []\n",
    "    for num in range(2, n + 1):\n",
    "        is_prime = True\n",
    "        for i in range(2, int(num ** 0.5) + 1):\n",
    "            if num % i == 0:\n",
    "                is_prime = False\n",
    "                break\n",
    "        if is_prime:\n",
    "            primes.append(num)\n",
    "    return primes\n",
    "\n",
    "# find_primes(20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc9c4b73",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert find_primes(10) == [2, 3, 5, 7]\n",
    "assert find_primes(20) == [2, 3, 5, 7, 11, 13, 17, 19]\n",
    "assert find_primes(1) == []"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d435457",
   "metadata": {},
   "source": [
    "### 12. Find the second largest element in a list\n",
    "**Return:** The second largest unique number in the list. If there is no second largest, return `None`\n",
    "**Example:** Input `[10, 5, 8, 12, 3]` → Output `10`\n",
    "**Note:** For `[5, 5, 3]`, the answer is `3` (unique values are considered)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ccff1c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "def second_largest(lst: list[int]) -> int:\n",
    "    if len(lst) < 2:\n",
    "        return None\n",
    "\n",
    "    # Remove duplicates by converting to set and back to list\n",
    "    unique_nums = []\n",
    "    for num in lst:\n",
    "        if num not in unique_nums:\n",
    "            unique_nums.append(num)\n",
    "\n",
    "    if len(unique_nums) < 2:\n",
    "        return None\n",
    "\n",
    "    # Find first and second largest\n",
    "    first = second = float('-inf')\n",
    "    for num in unique_nums:\n",
    "        if num > first:\n",
    "            second = first\n",
    "            first = num\n",
    "        elif num > second:\n",
    "            second = num\n",
    "\n",
    "    return second\n",
    "\n",
    "# second_largest([10, 5, 8, 12, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42536929",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert second_largest([10, 5, 8, 12, 3]) == 10\n",
    "assert second_largest([5, 5, 3]) == 3\n",
    "assert second_largest([1]) == None\n",
    "assert second_largest([7, 7, 7]) == None"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db08829a",
   "metadata": {},
   "source": [
    "### 13. Flatten a nested list (one level deep)\n",
    "**Return:** A single flat list from a list of lists\n",
    "**Example:** Input `[[1, 2], [3, 4], [5]]` → Output `[1, 2, 3, 4, 5]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d870dd1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def flatten_list(nested_lst: list[list]) -> list:\n",
    "    flat = []\n",
    "    for sublist in nested_lst:\n",
    "        for item in sublist:\n",
    "            flat.append(item)\n",
    "    return flat\n",
    "\n",
    "# flatten_list([[1, 2], [3, 4], [5]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00ccdd1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert flatten_list([[1, 2], [3, 4], [5]]) == [1, 2, 3, 4, 5]\n",
    "assert flatten_list([['a'], ['b', 'c'], ['d']]) == ['a', 'b', 'c', 'd']\n",
    "assert flatten_list([[], [1], []]) == [1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4cfa7b64",
   "metadata": {},
   "source": [
    "### 14. Find all pairs in a list that sum to a target value\n",
    "**Return:** A list of tuples, where each tuple contains two numbers from the list that sum to the target\n",
    "**Example:** Input `([1, 2, 3, 4, 5], 6)` → Output `[(1, 5), (2, 4)]`\n",
    "**Note:** Each pair should appear only once and should not use the same element twice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9d5e891",
   "metadata": {},
   "outputs": [],
   "source": [
    "def find_pairs_with_sum(lst: list[int], target: int) -> list[tuple]:\n",
    "    pairs = []\n",
    "    for i in range(len(lst)):\n",
    "        for j in range(i + 1, len(lst)):\n",
    "            if lst[i] + lst[j] == target:\n",
    "                pairs.append((lst[i], lst[j]))\n",
    "    return pairs\n",
    "\n",
    "# find_pairs_with_sum([1, 2, 3, 4, 5], 6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79a5bf2f",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert find_pairs_with_sum([1, 2, 3, 4, 5], 6) == [(1, 5), (2, 4)]\n",
    "assert find_pairs_with_sum([1, 1, 1, 1], 2) == [(1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1)]\n",
    "assert find_pairs_with_sum([1, 2, 3], 10) == []"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f577ebe9",
   "metadata": {},
   "source": [
    "### 15. Generate Pascal's Triangle up to n rows\n",
    "Pascal's Triangle: Each number is the sum of the two numbers directly above it.\n",
    "```\n",
    "Row 1:     1\n",
    "Row 2:    1 1\n",
    "Row 3:   1 2 1\n",
    "Row 4:  1 3 3 1\n",
    "Row 5: 1 4 6 4 1\n",
    "```\n",
    "**Return:** A list of lists, where each inner list represents a row of Pascal's Triangle\n",
    "**Example:** Input `5` → Output `[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc9957fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_pascals_triangle(n: int) -> list[list[int]]:\n",
    "    if n <= 0:\n",
    "        return []\n",
    "\n",
    "    triangle = []\n",
    "    for i in range(n):\n",
    "        row = []\n",
    "        for j in range(i + 1):\n",
    "            if j == 0 or j == i:\n",
    "                row.append(1)\n",
    "            else:\n",
    "                # Sum of two numbers from previous row\n",
    "                row.append(triangle[i-1][j-1] + triangle[i-1][j])\n",
    "        triangle.append(row)\n",
    "    return triangle\n",
    "\n",
    "# generate_pascals_triangle(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db58d2f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert generate_pascals_triangle(1) == [[1]]\n",
    "assert generate_pascals_triangle(3) == [[1], [1, 1], [1, 2, 1]]\n",
    "assert generate_pascals_triangle(5) == [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
