{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "intro",
   "metadata": {},
   "source": [
    "# Practice Questions\n",
    "\n",
    "This notebook contains **10 short and simple Python practice problems**.\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": "student_info",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = 'student name'\n",
    "roll_number = 'student roll number'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c30c434-3d9f-4615-b2a3-8c21ac5d223d",
   "metadata": {},
   "source": [
    "### 1. Check if a number is positive, negative, or zero  \n",
    "**Return:** A string like `'5 is Positive'`, `'-2 is Negative'`, or `'0 is Zero'`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d828395-7dda-45bf-9f89-8684ba3ae587",
   "metadata": {},
   "outputs": [],
   "source": [
    "def check_number(n: int) -> str:\n",
    "    if n > 0:\n",
    "        return f\"{n} is Positive\"\n",
    "    elif n < 0:\n",
    "        return f\"{n} is Negative\"\n",
    "    else:\n",
    "        return f\"{n} is Zero\"\n",
    "\n",
    "# check_number(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f2ddc17",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Tests\n",
    "assert check_number(5) == \"5 is Positive\"\n",
    "assert check_number(-2) == \"-2 is Negative\"\n",
    "assert check_number(0) == \"0 is Zero\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c04547c-5fff-4ae4-88e8-cf76c9f85123",
   "metadata": {},
   "source": [
    "### 2. Return the sum of all numbers from 1 to n  \n",
    "**Example:** Input `5` → `15`  \n",
    "**Return:** Integer sum of all numbers from 1 to n."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e09fc47-d535-4f66-a403-0c08104680ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "def sum_1_to_n(n: int) -> int:\n",
    "    total = 0\n",
    "    for i in range(1, n + 1):\n",
    "        total += i\n",
    "    return total\n",
    "\n",
    "# sum_1_to_n(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e027170",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Tests\n",
    "assert sum_1_to_n(5) == 15\n",
    "assert sum_1_to_n(10) == 55"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a551608-47b8-491e-9f00-0069b211e4e2",
   "metadata": {},
   "source": [
    "### 3. Count how many times a letter appears in a word  \n",
    "**Example:** Input `('banana', 'a')` → `3`  \n",
    "**Return:** The count of that letter in the word."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7d479a4-7d3b-4e5f-b73c-67633292a7cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "def count_letter(word: str, letter: str) -> int:\n",
    "    count = 0\n",
    "    for ch in word:\n",
    "        if ch.lower() == letter.lower():\n",
    "            count += 1\n",
    "    return count\n",
    "\n",
    "# count_letter(\"hello\", \"l\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a84af9d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Tests\n",
    "assert count_letter(\"banana\", \"a\") == 3\n",
    "assert count_letter(\"apple\", \"p\") == 2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "725c4152-740f-469a-b134-aa849a9d43d0",
   "metadata": {},
   "source": [
    "### 4. Return a list of even numbers between 1 and 10  \n",
    "**Return:** `[2, 4, 6, 8, 10]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9de89fdd-0b62-4b14-80d8-d06310e61859",
   "metadata": {},
   "outputs": [],
   "source": [
    "def even_numbers_1_to_10() -> list[int]:\n",
    "    evens = []\n",
    "    for i in range(1, 11):\n",
    "        if i % 2 == 0:\n",
    "            evens.append(i)\n",
    "    return evens\n",
    "\n",
    "# even_numbers_1_to_10()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d31ab80a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Tests\n",
    "assert even_numbers_1_to_10() == [2, 4, 6, 8, 10]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "221ba30e-7f4b-4f69-8b85-f3e14ff06df9",
   "metadata": {},
   "source": [
    "### 5. Return the average of three numbers  \n",
    "**Example:** Input `(4, 8, 10)` → `7.3333...`  \n",
    "**Return:** Arithmetic mean of the three numbers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63d90583-126b-4324-8ea0-a6ef28ebd693",
   "metadata": {},
   "outputs": [],
   "source": [
    "def average_of_three(a: float, b: float, c: float) -> float:\n",
    "    return (a + b + c) / 3\n",
    "\n",
    "# average_of_three(1, 2, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9f62403",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Tests\n",
    "assert round(average_of_three(4, 8, 10), 2) == 7.33\n",
    "assert average_of_three(3, 3, 3) == 3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dfe5287e-ee81-45bb-aeb4-4f3e932828cd",
   "metadata": {},
   "source": [
    "### 6. Load the dataset and display the first 5 rows  \n",
    "**Hint:** Use `pd.read_csv(dataset_path)`  \n",
    "**Return:** The first 5 rows as a DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8f1d333-c42a-4dce-b5fd-082aa6d09a69",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "def load_head(dataset_path: str) -> pd.DataFrame:\n",
    "    df = pd.read_csv(dataset_path)\n",
    "    return df.head()\n",
    "\n",
    "# load_head(\"nba.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6695e5cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "dataset_path = \"nba.csv\"\n",
    "\n",
    "# Tests\n",
    "result = load_head(dataset_path)\n",
    "assert isinstance(result, pd.DataFrame)\n",
    "assert len(result) == 5\n",
    "assert list(result.columns) == ['Name', 'Team', 'Position', 'Height', 'Weight', 'College', 'Salary']\n",
    "assert list(result.index) == [0, 1, 2, 3, 4]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa1f61df-3691-4034-b90d-b06c799d99a7",
   "metadata": {},
   "source": [
    "### 7. Display the number of rows and columns in the dataset  \n",
    "**Return:** A tuple like `(rows, columns)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2a37e811-0ca2-40d2-98a5-979a08dd9184",
   "metadata": {},
   "outputs": [],
   "source": [
    "def dataset_shape(dataset_path: str) -> tuple:\n",
    "    df = pd.read_csv(dataset_path)\n",
    "    return df.shape\n",
    "\n",
    "# dataset_shape(\"nba.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e0ee2cd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "dataset_path = \"nba.csv\"\n",
    "\n",
    "# Tests\n",
    "result = dataset_shape(dataset_path)\n",
    "assert isinstance(result, tuple)\n",
    "assert len(result) == 2\n",
    "assert result[0] > 0\n",
    "assert result[1] == 7\n",
    "assert result == (592, 7)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c7cd80d",
   "metadata": {},
   "source": [
    "### 8. Return all column names from the dataset  \n",
    "**Return:** A list of column names.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b267a67",
   "metadata": {},
   "outputs": [],
   "source": [
    "def column_names(dataset_path: str) -> list:\n",
    "    df = pd.read_csv(dataset_path)\n",
    "    return list(df.columns)\n",
    "\n",
    "# column_names(\"nba.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d320862",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "dataset_path = \"nba.csv\"\n",
    "\n",
    "# Tests\n",
    "result = column_names(dataset_path)\n",
    "assert isinstance(result, list)\n",
    "assert result == ['Name', 'Team', 'Position', 'Height', 'Weight', 'College', 'Salary']\n",
    "assert len(result) == 7"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "366f4806",
   "metadata": {},
   "source": [
    "### 9. Return the average of a numeric column  \n",
    "**Example:** If column name is `'Sales'`, return the mean of that column.  \n",
    "**Hint:** Use `df[column_name].mean()`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0f9c07b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "def average_of_column(dataset_path: str, column_name: str) -> float:\n",
    "    df = pd.read_csv(dataset_path)\n",
    "    return df[column_name].mean()\n",
    "\n",
    "# average_of_column(\"nba.csv\", \"Salary\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b4296778",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True 9218978.288934426 9218978.29 True\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "dataset_path = \"nba.csv\"\n",
    "\n",
    "# Tests\n",
    "salary_avg = average_of_column(dataset_path, 'Salary')\n",
    "weight_avg = average_of_column(dataset_path, 'Weight')\n",
    "assert isinstance(salary_avg, (float, int))\n",
    "assert round(salary_avg, 2) == 9218978.29\n",
    "\n",
    "assert isinstance(weight_avg, (float, int))\n",
    "assert round(weight_avg, 2) == 214.76"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f87d0ef1-f740-46c9-a6a1-2fe14c3d527b",
   "metadata": {},
   "source": [
    "### 10. Return all rows where a given column has a specific value  \n",
    "**Example:** `filter_by_value(dataset_path, 'Region', 'East')`  \n",
    "**Return:** Filtered DataFrame\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9da081e7-91b3-4fc2-8b09-e7c7f9b9fde0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def filter_by_value(dataset_path: str, column_name: str, value) -> pd.DataFrame:\n",
    "    df = pd.read_csv(dataset_path)\n",
    "    filtered = df[df[column_name] == value]\n",
    "    return filtered\n",
    "\n",
    "# filter_by_value(\"nba.csv\", \"Position\", \"PG\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "372fea1f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "229\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "dataset_path = \"nba.csv\"\n",
    "\n",
    "# Tests - Filtering by Position\n",
    "guards = filter_by_value(dataset_path, 'Position', 'G')\n",
    "print(len(guards))\n",
    "assert isinstance(guards, pd.DataFrame)\n",
    "assert len(guards) > 0\n",
    "assert all(guards['Position'] == 'G')\n",
    "assert list(guards.columns) == ['Name', 'Team', 'Position', 'Height', 'Weight', 'College', 'Salary']\n",
    "\n",
    "# Filtering by Team\n",
    "team_result = filter_by_value(dataset_path, 'Team', 'Boston Celtics')\n",
    "assert isinstance(team_result, pd.DataFrame)\n",
    "assert len(team_result) > 0\n",
    "assert all(team_result['Team'] == 'Boston Celtics')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c00c9c8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv (3.13.7)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
