{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "22161c76",
   "metadata": {},
   "source": [
    "# Set Operations \n",
    "\n",
    "Sets support mathematical operations that make it easy to compare two collections. Each operation has both an operator shorthand and a method form which produce the same result:\n",
    "\n",
    "| Operation            | Operator | Method                       | Description                                      |\n",
    "|----------------------|----------|------------------------------|--------------------------------------------------|\n",
    "| Union                | `a \\| b`  | `a.union(b)`                 | All elements from both sets                      |\n",
    "| Intersection         | `a & b`  | `a.intersection(b)`          | Only elements common to both sets                |\n",
    "| Difference           | `a - b`  | `a.difference(b)`            | Elements in `a` but not in `b`                   |\n",
    "| Symmetric Difference | `a ^ b`  | `a.symmetric_difference(b)`  | Elements in either set, but not in both          |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc2768db",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "from pathlib import Path\n",
    "\n",
    "# Find project root by looking for _config.yml\n",
    "current = Path.cwd()\n",
    "for parent in [current, *current.parents]:\n",
    "    if (parent / '_config.yml').exists():\n",
    "        project_root = parent\n",
    "        break\n",
    "else:\n",
    "    project_root = Path.cwd().parent.parent\n",
    "\n",
    "# Add project root to path\n",
    "sys.path.insert(0, str(project_root))\n",
    "\n",
    "# Import shared teaching helpers and cell magics\n",
    "from shared import thinkpython, diagram, jupyturtle, structshape\n",
    "from shared.download import download\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b9dae9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = {1, 2, 3}\n",
    "b = {3, 4, 5}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7ff8e79",
   "metadata": {},
   "source": [
    "Note that, **operator** forms (`|`, `&`, `-`, `^`) require **set operands**. Method forms are more flexible and can take any iterable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "b74eae75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5}\n",
      "unsupported operand type(s) for |: 'set' and 'list'\n"
     ]
    }
   ],
   "source": [
    "print(a.union([3, 4, 5]))    # works: method accepts iterable\n",
    "\n",
    "try:\n",
    "    a | [3, 4, 5]            # TypeError: right side is a list\n",
    "except TypeError as e:\n",
    "    print(e)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27d1077e",
   "metadata": {},
   "source": [
    "## Union \n",
    "\n",
    "All elements from both sets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "aeafeb88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5}"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a | b        # {1, 2, 3, 4, 5}\n",
    "a.union(b)   # same"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "083b51d6",
   "metadata": {},
   "source": [
    "## Intersection\n",
    "\n",
    "Only elements in both sets."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "aef3bac1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{3}"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a & b              # {3}\n",
    "a.intersection(b)  # same"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ebb441c",
   "metadata": {},
   "source": [
    "## Difference\n",
    "\n",
    "Elements in a but not in b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "74da1325",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2}"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a - b              # {1, 2}\n",
    "a.difference(b)    # same"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f538100",
   "metadata": {},
   "source": [
    "## Symmetric Difference\n",
    "\n",
    "Elements in either set, but not both"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "64af9751",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 4, 5}"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a ^ b                       # {1, 2, 4, 5}\n",
    "a.symmetric_difference(b)   # same"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2a1f2d8",
   "metadata": {
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Exercise: Basic Set Operations\n",
    "# 1. Create A = {1, 2, 3, 4} and B = {3, 4, 5, 6}.\n",
    "# 2. Print A | B, A & B, A - B, and A ^ B.\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f17f5d63",
   "metadata": {
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [],
   "source": [
    "A = {1, 2, 3, 4}\n",
    "B = {3, 4, 5, 6}\n",
    "\n",
    "print(\"Union:\", A | B)\n",
    "print(\"Intersection:\", A & B)\n",
    "print(\"Difference A-B:\", A - B)\n",
    "print(\"Symmetric Difference:\", A ^ B)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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
}
