{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "00af8acc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total Sales: 59520\n",
      "Average Per Unit Sale: 188.95\n",
      "Top Product: Backpack\n",
      "Sales By Category: {'Education': 36600, 'Office': 11670, 'Lifestyle': 11250}\n"
     ]
    }
   ],
   "source": [
    "products = [\n",
    "  {\"product_name\": \"Pen\", \"Category\": \"Education\", \"units_sold\": 20, \"unit_price\": 10},\n",
    "  {\"product_name\": \"Notebook\", \"Category\": \"Education\", \"units_sold\": 50, \"unit_price\": 40},\n",
    "  {\"product_name\": \"Desk Lamp\", \"Category\": \"Office\", \"units_sold\": 15, \"unit_price\": 350},\n",
    "  {\"product_name\": \"Backpack\", \"Category\": \"Education\", \"units_sold\": 30, \"unit_price\": 800},\n",
    "  {\"product_name\": \"Water Bottle\", \"Category\": \"Lifestyle\", \"units_sold\": 45, \"unit_price\": 250},\n",
    "  {\"product_name\": \"Calculator\", \"Category\": \"Education\", \"units_sold\": 12, \"unit_price\": 600},\n",
    "  {\"product_name\": \"Stapler\", \"Category\": \"Office\", \"units_sold\": 25, \"unit_price\": 120},\n",
    "  {\"product_name\": \"Marker Pens\", \"Category\": \"Education\", \"units_sold\": 40, \"unit_price\": 80},\n",
    "  {\"product_name\": \"Scissors\", \"Category\": \"Office\", \"units_sold\": 18, \"unit_price\": 90},\n",
    "  {\"product_name\": \"File Folder\", \"Category\": \"Office\", \"units_sold\": 60, \"unit_price\": 30}\n",
    "]\n",
    "\n",
    "\n",
    "def total_sales(products):\n",
    "    total = 0\n",
    "\n",
    "    for product in products:\n",
    "        total += product[\"units_sold\"] * product[\"unit_price\"]\n",
    "\n",
    "    return total\n",
    "\n",
    "\n",
    "def avg_per_sale(products):\n",
    "    total_units = 0\n",
    "\n",
    "    for product in products:\n",
    "        total_units += product[\"units_sold\"]\n",
    "\n",
    "    return total_sales(products) / total_units\n",
    "\n",
    "\n",
    "def top_sales_product(products):\n",
    "    highest_sale = 0\n",
    "    top_product = \"\"\n",
    "\n",
    "    for product in products:\n",
    "        sale = product[\"units_sold\"] * product[\"unit_price\"]\n",
    "\n",
    "        if sale > highest_sale:\n",
    "            highest_sale = sale\n",
    "            top_product = product[\"product_name\"]\n",
    "\n",
    "    return top_product\n",
    "\n",
    "\n",
    "def sale_by_category(products):\n",
    "    category_sales = {}\n",
    "\n",
    "    for product in products:\n",
    "        category = product[\"Category\"]\n",
    "        sale = product[\"units_sold\"] * product[\"unit_price\"]\n",
    "\n",
    "        if category in category_sales:\n",
    "            category_sales[category] += sale\n",
    "        else:\n",
    "            category_sales[category] = sale\n",
    "\n",
    "    return category_sales\n",
    "\n",
    "\n",
    "print(\"Total Sales:\", total_sales(products))\n",
    "print(\"Average Per Unit Sale:\", round(avg_per_sale(products), 2))\n",
    "print(\"Top Product:\", top_sales_product(products))\n",
    "print(\"Sales By Category:\", sale_by_category(products))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3b13237c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Student Averages:\n",
      "Ankur : 85.0\n",
      "Bhavna : 91.33\n",
      "Chirag : 75.67\n",
      "Divya : 90.67\n",
      "\n",
      "Class Average: 85.67\n",
      "Top Student: Bhavna\n",
      "Top Average: 91.33\n",
      "\n",
      "Categories:\n",
      "Excellent (>=90) : ['Bhavna', 'Divya']\n",
      "Good (80-89) : ['Ankur']\n",
      "Average (70-79) : ['Chirag']\n",
      "Needs Improvement (<70) : []\n"
     ]
    }
   ],
   "source": [
    "students_data = [\n",
    "    {\"student_name\": \"Ankur\", \"grades\": {\"Math\": 85.0, \"Science\": 92.0, \"English\": 78.0}},\n",
    "    {\"student_name\": \"Bhavna\", \"grades\": {\"Math\": 95.0, \"Science\": 88.0, \"English\": 91.0}},\n",
    "    {\"student_name\": \"Chirag\", \"grades\": {\"Math\": 70.0, \"Science\": 75.0, \"English\": 82.0}},\n",
    "    {\"student_name\": \"Divya\", \"grades\": {\"Math\": 88.0, \"Science\": 94.0, \"English\": 90.0}}\n",
    "]\n",
    "\n",
    "\n",
    "def calculate_student_averages(data):\n",
    "    averages = {}\n",
    "\n",
    "    for student in data:\n",
    "        grades = student[\"grades\"].values()\n",
    "\n",
    "        averages[student[\"student_name\"]] = sum(grades) / len(student[\"grades\"])\n",
    "\n",
    "    return averages\n",
    "\n",
    "\n",
    "def get_class_average(student_averages):\n",
    "    return sum(student_averages.values()) / len(student_averages)\n",
    "\n",
    "\n",
    "def find_top_performers(student_averages):\n",
    "    highest = max(student_averages.values())\n",
    "\n",
    "    for name, average in student_averages.items():\n",
    "        if average == highest:\n",
    "            return name, highest\n",
    "\n",
    "\n",
    "def categorize_students(student_averages):\n",
    "    categories = {\n",
    "        \"Excellent (>=90)\": [],\n",
    "        \"Good (80-89)\": [],\n",
    "        \"Average (70-79)\": [],\n",
    "        \"Needs Improvement (<70)\": []\n",
    "    }\n",
    "\n",
    "    for name, average in student_averages.items():\n",
    "\n",
    "        if average >= 90:\n",
    "            categories[\"Excellent (>=90)\"].append(name)\n",
    "\n",
    "        elif average >= 80:\n",
    "            categories[\"Good (80-89)\"].append(name)\n",
    "\n",
    "        elif average >= 70:\n",
    "            categories[\"Average (70-79)\"].append(name)\n",
    "\n",
    "        else:\n",
    "            categories[\"Needs Improvement (<70)\"].append(name)\n",
    "\n",
    "    return categories\n",
    "\n",
    "\n",
    "def display_insights(data):\n",
    "\n",
    "    averages = calculate_student_averages(data)\n",
    "\n",
    "    print(\"Student Averages:\")\n",
    "\n",
    "    for name, average in averages.items():\n",
    "        print(name, \":\", round(average, 2))\n",
    "\n",
    "    print(\"\\nClass Average:\", round(get_class_average(averages), 2))\n",
    "\n",
    "    top_student, top_average = find_top_performers(averages)\n",
    "\n",
    "    print(\"Top Student:\", top_student)\n",
    "    print(\"Top Average:\", round(top_average, 2))\n",
    "\n",
    "    print(\"\\nCategories:\")\n",
    "\n",
    "    categories = categorize_students(averages)\n",
    "\n",
    "    for category, students in categories.items():\n",
    "        print(category, \":\", students)\n",
    "\n",
    "\n",
    "display_insights(students_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "975a5299",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total Books: 4\n",
      "Available Books: 2\n",
      "Overdue Books:\n",
      "1984 - 2026-03-15\n",
      "Book checked out successfully\n",
      "Book returned successfully\n",
      "Total Books: 4\n",
      "Available Books: 2\n",
      "Overdue Books:\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "\n",
    "library_books = [\n",
    "    {\n",
    "        \"title\": \"To Kill a Mockingbird\",\n",
    "        \"author\": \"Harper Lee\",\n",
    "        \"checked_out\": False,\n",
    "        \"due_date\": None\n",
    "    },\n",
    "    {\n",
    "        \"title\": \"1984\",\n",
    "        \"author\": \"George Orwell\",\n",
    "        \"checked_out\": True,\n",
    "        \"due_date\": \"2026-03-15\"\n",
    "    },\n",
    "    {\n",
    "        \"title\": \"The Great Gatsby\",\n",
    "        \"author\": \"F. Scott Fitzgerald\",\n",
    "        \"checked_out\": True,\n",
    "        \"due_date\": \"2026-04-10\"\n",
    "    },\n",
    "    {\n",
    "        \"title\": \"Moby Dick\",\n",
    "        \"author\": \"Herman Melville\",\n",
    "        \"checked_out\": False,\n",
    "        \"due_date\": None\n",
    "    }\n",
    "]\n",
    "\n",
    "\n",
    "def get_available_books_count(books):\n",
    "    count = 0\n",
    "\n",
    "    for book in books:\n",
    "        if book[\"checked_out\"] == False:\n",
    "            count += 1\n",
    "\n",
    "    return count\n",
    "\n",
    "\n",
    "def get_overdue_books(books, current_date_str):\n",
    "    overdue_books = []\n",
    "\n",
    "    current_date = datetime.strptime(current_date_str, \"%Y-%m-%d\")\n",
    "\n",
    "    for book in books:\n",
    "\n",
    "        if book[\"checked_out\"] and book[\"due_date\"]:\n",
    "\n",
    "            due_date = datetime.strptime(book[\"due_date\"], \"%Y-%m-%d\")\n",
    "\n",
    "            if due_date < current_date:\n",
    "                overdue_books.append(book)\n",
    "\n",
    "    return overdue_books\n",
    "\n",
    "\n",
    "def checkout_book(books, title, due_date_str):\n",
    "\n",
    "    for book in books:\n",
    "\n",
    "        if book[\"title\"].lower() == title.lower():\n",
    "\n",
    "            if book[\"checked_out\"]:\n",
    "                print(\"Book already checked out\")\n",
    "                return\n",
    "\n",
    "            book[\"checked_out\"] = True\n",
    "            book[\"due_date\"] = due_date_str\n",
    "\n",
    "            print(\"Book checked out successfully\")\n",
    "            return\n",
    "\n",
    "    print(\"Book not found\")\n",
    "\n",
    "\n",
    "def return_book(books, title):\n",
    "\n",
    "    for book in books:\n",
    "\n",
    "        if book[\"title\"].lower() == title.lower():\n",
    "\n",
    "            book[\"checked_out\"] = False\n",
    "            book[\"due_date\"] = None\n",
    "\n",
    "            print(\"Book returned successfully\")\n",
    "            return\n",
    "\n",
    "    print(\"Book not found\")\n",
    "\n",
    "\n",
    "def display_library_summary(books, current_date_str):\n",
    "\n",
    "    print(\"Total Books:\", len(books))\n",
    "\n",
    "    print(\n",
    "        \"Available Books:\",\n",
    "        get_available_books_count(books)\n",
    "    )\n",
    "\n",
    "    print(\"Overdue Books:\")\n",
    "\n",
    "    overdue = get_overdue_books(books, current_date_str)\n",
    "\n",
    "    for book in overdue:\n",
    "        print(book[\"title\"], \"-\", book[\"due_date\"])\n",
    "\n",
    "\n",
    "today_date = \"2026-04-01\"\n",
    "\n",
    "display_library_summary(library_books, today_date)\n",
    "\n",
    "checkout_book(\n",
    "    library_books,\n",
    "    \"To Kill a Mockingbird\",\n",
    "    \"2026-04-20\"\n",
    ")\n",
    "\n",
    "return_book(\n",
    "    library_books,\n",
    "    \"1984\"\n",
    ")\n",
    "\n",
    "display_library_summary(library_books, today_date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d2a878fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Employee: Rajesh Deshpande\n",
      "Hours: 15.92\n",
      "Present: 2\n",
      "Absent: 0\n",
      "\n",
      "Employee: Ananya Sharma\n",
      "Hours: 17.0\n",
      "Present: 2\n",
      "Absent: 0\n",
      "\n",
      "Employee: Vijay Kumar\n",
      "Hours: 8.0\n",
      "Present: 1\n",
      "Absent: 1\n",
      "\n",
      "Highest Working Employee: Ananya Sharma\n",
      "Hours: 17.0\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "\n",
    "employees = [\n",
    "    {\n",
    "        \"name\": \"Rajesh Deshpande\",\n",
    "        \"attendance\": {\n",
    "            \"2024-08-15\": (\"09:00\", \"17:00\"),\n",
    "            \"2024-08-16\": (\"09:15\", \"17:10\")\n",
    "        }\n",
    "    },\n",
    "    {\n",
    "        \"name\": \"Ananya Sharma\",\n",
    "        \"attendance\": {\n",
    "            \"2024-08-15\": (\"09:00\", \"17:30\"),\n",
    "            \"2024-08-16\": (\"08:45\", \"17:15\")\n",
    "        }\n",
    "    },\n",
    "    {\n",
    "        \"name\": \"Vijay Kumar\",\n",
    "        \"attendance\": {\n",
    "            \"2024-08-15\": (\"09:00\", \"17:00\")\n",
    "        }\n",
    "    }\n",
    "]\n",
    "\n",
    "\n",
    "def calculate_hours(start, end):\n",
    "    start = datetime.strptime(start, \"%H:%M\")\n",
    "    end = datetime.strptime(end, \"%H:%M\")\n",
    "\n",
    "    return (end - start).seconds / 3600\n",
    "\n",
    "\n",
    "def employee_hours(employee):\n",
    "    total = 0\n",
    "\n",
    "    for date, time in employee[\"attendance\"].items():\n",
    "        start, end = time\n",
    "        total += calculate_hours(start, end)\n",
    "\n",
    "    return total\n",
    "\n",
    "\n",
    "def attendance_report(employees):\n",
    "    all_dates = set()\n",
    "\n",
    "    for employee in employees:\n",
    "        all_dates.update(employee[\"attendance\"].keys())\n",
    "\n",
    "    total_days = len(all_dates)\n",
    "\n",
    "    highest_name = \"\"\n",
    "    highest_hours = 0\n",
    "\n",
    "    for employee in employees:\n",
    "\n",
    "        hours = employee_hours(employee)\n",
    "        present = len(employee[\"attendance\"])\n",
    "        absent = total_days - present\n",
    "\n",
    "        print(\"\\nEmployee:\", employee[\"name\"])\n",
    "        print(\"Hours:\", round(hours, 2))\n",
    "        print(\"Present:\", present)\n",
    "        print(\"Absent:\", absent)\n",
    "\n",
    "        if hours > highest_hours:\n",
    "            highest_hours = hours\n",
    "            highest_name = employee[\"name\"]\n",
    "\n",
    "    print(\"\\nHighest Working Employee:\", highest_name)\n",
    "    print(\"Hours:\", round(highest_hours, 2))\n",
    "\n",
    "\n",
    "attendance_report(employees)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f72c7ae",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0a7985c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b94051a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3cffdc14",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
