{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0ec754d8",
   "metadata": {},
   "source": [
    "# 18-24 · Отмена действий (Undo)\n",
    "\n",
    "Практика к разделу [«Отмена действий (Undo)»](../../site/chapters/glava-18/18-24-otmena-deystviy.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b9ce46b",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Undo убирает ВСЕ элементы одного действия целиком — даже если их несколько."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca754a65",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "aa1eaca3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:52:09.555015Z",
     "iopub.status.busy": "2026-09-03T00:52:09.554884Z",
     "iopub.status.idle": "2026-09-03T00:52:09.584045Z",
     "shell.execute_reply": "2026-09-03T00:52:09.583361Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['rect', 'line1', 'line2', 'line3']\n"
     ]
    }
   ],
   "source": [
    "document = []\n",
    "undo_stack = []\n",
    "\n",
    "\n",
    "def commit_action(document, undo_stack, shapes):\n",
    "    document.extend(shapes)\n",
    "    undo_stack.append(shapes)\n",
    "\n",
    "\n",
    "def undo(document, undo_stack):\n",
    "    if not undo_stack:\n",
    "        return\n",
    "    shapes = undo_stack.pop()\n",
    "    del document[len(document) - len(shapes):]\n",
    "\n",
    "\n",
    "commit_action(document, undo_stack, [\"rect\"])\n",
    "commit_action(document, undo_stack, [\"line1\", \"line2\", \"line3\"])  # один карандашный штрих\n",
    "print(document)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b51221f",
   "metadata": {},
   "source": [
    "## Задание ★ Базовая практика"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "39c647d9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:52:09.585920Z",
     "iopub.status.busy": "2026-09-03T00:52:09.585762Z",
     "iopub.status.idle": "2026-09-03T00:52:09.589194Z",
     "shell.execute_reply": "2026-09-03T00:52:09.588349Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Штрих из трёх отрезков отменяется одним Undo.\n"
     ]
    }
   ],
   "source": [
    "undo(document, undo_stack)\n",
    "assert document == [\"rect\"], \"Undo должен убрать ВСЕ три отрезка штриха, а не один\"\n",
    "assert len(undo_stack) == 1\n",
    "print(\"Штрих из трёх отрезков отменяется одним Undo.\")"
   ]
  }
 ],
 "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
}
