{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "649d5cfe",
   "metadata": {},
   "source": [
    "# 17-14 · Indeksy, wiersze i kolumny\n",
    "\n",
    "Praktyka do [indeksów, wierszy i kolumn](.. /.. /site/chapters/glava-17/17-14-indeksy-stroki-stolbcy.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce0aef9d",
   "metadata": {},
   "source": [
    "## Cel\n",
    "\n",
    "Przekonwertować indeks komórki 0..8 na (row, column) i odwrotnie."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "983359b4",
   "metadata": {},
   "source": [
    "## Sprawa robocza"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "80c29c93",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T10:12:52.112934Z",
     "iopub.status.busy": "2026-09-03T10:12:52.112809Z",
     "iopub.status.idle": "2026-09-03T10:12:52.134433Z",
     "shell.execute_reply": "2026-09-03T10:12:52.133893Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 -> (0, 0)\n",
      "1 -> (0, 1)\n",
      "2 -> (0, 2)\n",
      "3 -> (1, 0)\n",
      "4 -> (1, 1)\n",
      "5 -> (1, 2)\n",
      "6 -> (2, 0)\n",
      "7 -> (2, 1)\n",
      "8 -> (2, 2)\n"
     ]
    }
   ],
   "source": [
    "def index_to_row_col(index):\n",
    "    return index // 3, index % 3\n",
    "\n",
    "def row_col_to_index(row, column):\n",
    "    return row * 3 + column\n",
    "\n",
    "for i in range(9):\n",
    "    print(i, \"->\", index_to_row_col(i))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "effab813",
   "metadata": {},
   "source": [
    "## Podstawowa praktyka Quest ★ – round-trip dla wszystkich 9 komórek"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "05676a09",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T10:12:52.135624Z",
     "iopub.status.busy": "2026-09-03T10:12:52.135461Z",
     "iopub.status.idle": "2026-09-03T10:12:52.139380Z",
     "shell.execute_reply": "2026-09-03T10:12:52.138854Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Прямое и обратное преобразование верны для всех 9 клеток.\n"
     ]
    }
   ],
   "source": [
    "roundtrip_ok = all(row_col_to_index(*index_to_row_col(i)) == i for i in range(9))\n",
    "assert index_to_row_col(4) == (1, 1)\n",
    "assert row_col_to_index(2, 2) == 8\n",
    "assert roundtrip_ok is True\n",
    "print(\"Прямое и обратное преобразование верны для всех 9 клеток.\")"
   ]
  }
 ],
 "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
}
