{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "898a9023",
   "metadata": {},
   "source": [
    "# 16-29 · Lista zadań\n",
    "\n",
    "Praktyka do sekcji [„Mini-projekt: lista zadań”"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45acb813",
   "metadata": {},
   "source": [
    "## Cel\n",
    "\n",
    "Sprawdź czyste funkcje listy zadań i ich trwałość."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "63fc9cb5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T21:45:24.927770Z",
     "iopub.status.busy": "2026-08-18T21:45:24.927490Z",
     "iopub.status.idle": "2026-08-18T21:45:24.949126Z",
     "shell.execute_reply": "2026-08-18T21:45:24.948654Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Купить молоко', 'Позвонить']\n",
      "['Купить молоко', 'Позвонить']\n",
      "['Позвонить']\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "from pathlib import Path\n",
    "\n",
    "def load_tasks(path):\n",
    "    if not path.exists():\n",
    "        return []\n",
    "    with path.open(\"r\", encoding=\"utf-8\") as f:\n",
    "        return json.load(f)\n",
    "\n",
    "def save_tasks(path, tasks):\n",
    "    with path.open(\"w\", encoding=\"utf-8\") as f:\n",
    "        json.dump(tasks, f, ensure_ascii=False, indent=2)\n",
    "\n",
    "def add_task(tasks, text):\n",
    "    if not text.strip():\n",
    "        return tasks\n",
    "    return tasks + [text.strip()]\n",
    "\n",
    "def remove_task(tasks, index):\n",
    "    return tasks[:index] + tasks[index + 1:]\n",
    "\n",
    "todo_path = Path(\"todo_test.json\")\n",
    "todo_path.unlink(missing_ok=True)\n",
    "\n",
    "tasks = load_tasks(todo_path)\n",
    "tasks = add_task(tasks, \"Купить молоко\")\n",
    "tasks = add_task(tasks, \"   \")\n",
    "tasks = add_task(tasks, \"Позвонить\")\n",
    "save_tasks(todo_path, tasks)\n",
    "\n",
    "loaded_tasks = load_tasks(todo_path)\n",
    "tasks_after_remove = remove_task(loaded_tasks, 0)\n",
    "print(tasks)\n",
    "print(loaded_tasks)\n",
    "print(tasks_after_remove)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Cartesian Python 3.14",
   "language": "python",
   "name": "cartesian-python314"
  },
  "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.14.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
