{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b410b469",
   "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",
    "- **7 Extremely Easy Questions** (Questions 1-7)\n",
    "- **5 Intermediate Questions** (Questions 8-12)\n",
    "- **3 Extremely Hard Questions** (Questions 13-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": "ba39cad6",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = 'student name'\n",
    "roll_number = 'student roll number'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f28e850f",
   "metadata": {},
   "source": [
    "---\n",
    "## EXTREMELY EASY QUESTIONS (1-7)\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e547c82",
   "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": "36a2e573",
   "metadata": {},
   "outputs": [],
   "source": [
    "def is_even(n: int) -> bool:\n",
    "    return n % 2 == 0\n",
    "\n",
    "# is_even(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0755656a",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert is_even(4) == True\n",
    "assert is_even(7) == False\n",
    "assert is_even(0) == True"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a8e419f",
   "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": "a76f979a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def max_of_two(a: int, b: int) -> int:\n",
    "    if a >= b:\n",
    "        return a\n",
    "    return b\n",
    "\n",
    "# max_of_two(5, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "551b1888",
   "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": "82a70438",
   "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": "d2f140d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def count_elements(lst: list) -> int:\n",
    "    count = 0\n",
    "    for _ in lst:\n",
    "        count += 1\n",
    "    return count\n",
    "\n",
    "# count_elements([1, 2, 3, 4, 5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b25898fb",
   "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": "00cc468f",
   "metadata": {},
   "source": [
    "### 4. Sum of all elements in a list\n",
    "**Return:** The sum of all numbers in the list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e314c5f9",
   "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": "bdbf5760",
   "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": "3d73859e",
   "metadata": {},
   "source": [
    "### 5. Check if number is positive\n",
    "**Return:** `'Positive'` if n>0, `'Negative'` if n<0, `'Zero'` otherwise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "944931c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def sign_check(n: int) -> str:\n",
    "    if n > 0:\n",
    "        return 'Positive'\n",
    "    elif n < 0:\n",
    "        return 'Negative'\n",
    "    else:\n",
    "        return 'Zero'\n",
    "\n",
    "# sign_check(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d552309",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert sign_check(5) == 'Positive'\n",
    "assert sign_check(-2) == 'Negative'\n",
    "assert sign_check(0) == 'Zero'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87e2bd7c",
   "metadata": {},
   "source": [
    "### 6. Return first element of a list or `None` if empty\n",
    "**Return:** First element or `None`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7637a78",
   "metadata": {},
   "outputs": [],
   "source": [
    "def first_element(lst: list):\n",
    "    if len(lst) == 0:\n",
    "        return None\n",
    "    return lst[0]\n",
    "\n",
    "# first_element([1, 2, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80762fd0",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert first_element([1, 2, 3]) == 1\n",
    "assert first_element([]) is None\n",
    "assert first_element(['a', 'b']) == 'a'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f8589a9",
   "metadata": {},
   "source": [
    "### 7. Return last element of a list or `None` if empty\n",
    "**Return:** Last element or `None`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "913d00f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def last_element(lst: list):\n",
    "    if not lst:\n",
    "        return None\n",
    "    return lst[-1]\n",
    "\n",
    "# last_element([1, 2, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64d1975c",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert last_element([1, 2, 3]) == 3\n",
    "assert last_element([]) is None\n",
    "assert last_element(['x']) == 'x'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f759865f",
   "metadata": {},
   "source": [
    "---\n",
    "## INTERMEDIATE QUESTIONS (8-12)\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a6a3bec",
   "metadata": {},
   "source": [
    "### 8. Grade classifier based on marks\n",
    "Return grade:\n",
    "- 90 and above: 'A'\n",
    "- 80-89: 'B'\n",
    "- 70-79: 'C'\n",
    "- 60-69: 'D'\n",
    "- Below 60: 'F'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca55e7ae",
   "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": "13ea28a7",
   "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": "51b1b94b",
   "metadata": {},
   "source": [
    "### 9. Filter positive numbers from a list\n",
    "Return a list containing only positive numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38491a0a",
   "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": "b82edba5",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert filter_positive([-5, 3, -1, 7, 0, 2]) == [3, 7, 2]\n",
    "assert filter_positive([-1, -2]) == []\n",
    "assert filter_positive([1, 2]) == [1, 2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fdee1ee",
   "metadata": {},
   "source": [
    "### 10. Reverse a list using a loop\n",
    "Return a new list reversed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "321a7c67",
   "metadata": {},
   "outputs": [],
   "source": [
    "def reverse_list(lst: list) -> list:\n",
    "    rev = []\n",
    "    for i in range(len(lst)-1, -1, -1):\n",
    "        rev.append(lst[i])\n",
    "    return rev\n",
    "\n",
    "# reverse_list([1,2,3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3b5d547",
   "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": "b00260ec",
   "metadata": {},
   "source": [
    "### 11. Find the maximum element in a list\n",
    "Return the largest value, or `None` if list empty"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9bdeae9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def find_max(lst: list[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": "0821c710",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert find_max([3, 7, 2, 9, 1]) == 9\n",
    "assert find_max([5, 5, 5]) == 5\n",
    "assert find_max([]) is None"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ec9c8f1",
   "metadata": {},
   "source": [
    "### 12. Multiplication table for a given number\n",
    "Return first 10 multiples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e4597126",
   "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(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5804b7c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert multiplication_table(3) == [3, 6, 9, 12, 15, 18, 21, 24, 27, 30]\n",
    "assert multiplication_table(1) == [1,2,3,4,5,6,7,8,9,10]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55e7c374",
   "metadata": {},
   "source": [
    "---\n",
    "## EXTREMELY HARD QUESTIONS (13-15)\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16032655",
   "metadata": {},
   "source": [
    "### 13. Find all primes up to n\n",
    "Return list of primes from 2..n inclusive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad380a7e",
   "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": "0717f719",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert find_primes(10) == [2, 3, 5, 7]\n",
    "assert find_primes(1) == []"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3af635a5",
   "metadata": {},
   "source": [
    "### 14. Find the second largest unique element in a list\n",
    "Return second largest unique number or `None` if not present"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df754232",
   "metadata": {},
   "outputs": [],
   "source": [
    "def second_largest(lst: list[int]):\n",
    "    unique = []\n",
    "    for num in lst:\n",
    "        if num not in unique:\n",
    "            unique.append(num)\n",
    "    if len(unique) < 2:\n",
    "        return None\n",
    "    first = second = float('-inf')\n",
    "    for num in unique:\n",
    "        if num > first:\n",
    "            second = first\n",
    "            first = num\n",
    "        elif num > second:\n",
    "            second = num\n",
    "    return second\n",
    "\n",
    "# second_largest([10,5,8,12,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9955ed3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert second_largest([10, 5, 8, 12, 3]) == 10\n",
    "assert second_largest([5,5,3]) == 3\n",
    "assert second_largest([7]) is None"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcd6cc4c",
   "metadata": {},
   "source": [
    "### 15. Generate Pascal's Triangle up to n rows\n",
    "Return list of rows (each row is a list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67188ed3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_pascals_triangle(n: int) -> list[list[int]]:\n",
    "    if n <= 0:\n",
    "        return []\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",
    "                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": "c7cd1282",
   "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
}
