Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/02advanced/01life-game/_samples/count_neighboars.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"nbformat": 4,
"nbformat_minor": 2,
"metadata": {},
"cells": [
{
"metadata": {},
"source": [
"def count_neighbors(board, i, j):\n",
" cnt = 0\n",
" for k in range(i - 1, i + 2):\n",
" for l in range(j - 1, j + 2):\n",
" if 0 <= k < len(board) and 0 <= l < len(board[k]):\n",
" cnt += board[k][l]\n",
" return cnt - board[i][j]\n",
"\n",
"\n",
"board = [[1, 1, 0], [1, 0, 0], [0, 0, 0]]\n",
"print(count_neighbors(board, 0, 0))"
],
"cell_type": "code",
"outputs": [],
"execution_count": null
}
]
}
33 changes: 33 additions & 0 deletions docs/02advanced/01life-game/_samples/count_neighbors_draft.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"nbformat": 4,
"nbformat_minor": 2,
"metadata": {},
"cells": [
{
"metadata": {},
"source": [
"def count_neighbors(board, i, j):\n",
" cnt = 0\n",
" for k in range(i - 1, i + 2):\n",
" for l in range(j - 1, j + 2):\n",
" cnt += board[k][l]\n",
" return cnt - board[i][j]\n",
"\n",
"\n",
"board = [[1, 1, 0], [1, 0, 0], [0, 0, 0]]\n",
"print(count_neighbors(board, 1, 1))"
],
"cell_type": "code",
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"3\n"
]
}
],
"execution_count": null
}
]
}
50 changes: 50 additions & 0 deletions docs/02advanced/01life-game/_samples/is_alive.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"nbformat": 4,
"nbformat_minor": 2,
"metadata": {},
"cells": [
{
"metadata": {},
"source": [
"def count_neighbors(board, i, j):\n",
" cnt = 0\n",
" for k in range(i - 1, i + 2):\n",
" for l in range(j - 1, j + 2):\n",
" if 0 <= k < len(board) and 0 <= l < len(board[k]):\n",
" cnt += board[k][l]\n",
" return cnt - board[i][j]\n",
"\n",
"\n",
"def is_alive(board, i, j):\n",
" neighbors_cnt = count_neighbors(board, i, j)\n",
" if board[i][j] == 0:\n",
" if neighbors_cnt == 3:\n",
" return 1\n",
" else:\n",
" return 0\n",
" else:\n",
" if neighbors_cnt in [2, 3]:\n",
" return 1\n",
" elif neighbors_cnt <= 1:\n",
" return 0\n",
" elif neighbors_cnt >= 4:\n",
" return 0\n",
"\n",
"\n",
"board = [[1, 1, 0], [1, 0, 0], [0, 0, 0]]\n",
"print(is_alive(board, 0, 0))"
],
"cell_type": "code",
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1\n"
]
}
],
"execution_count": null
}
]
}
2,848 changes: 2,848 additions & 0 deletions docs/02advanced/01life-game/_samples/life_game.ipynb

Large diffs are not rendered by default.

107 changes: 107 additions & 0 deletions docs/02advanced/01life-game/_samples/life_game_func.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{
"nbformat": 4,
"nbformat_minor": 2,
"metadata": {},
"cells": [
{
"metadata": {},
"source": [
"!pip install ita"
],
"cell_type": "code",
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Defaulting to user installation because normal site-packages is not writeable\n",
"Requirement already satisfied: ita in /home/w/.local/lib/python3.10/site-packages (0.2.12)\n",
"Requirement already satisfied: matplotlib in /home/w/.local/lib/python3.10/site-packages (from ita) (3.6.0)\n",
"Requirement already satisfied: numpy in /home/w/.local/lib/python3.10/site-packages (from ita) (1.23.3)\n",
"Requirement already satisfied: packaging>=20.0 in /home/w/.local/lib/python3.10/site-packages (from matplotlib->ita) (21.3)\n",
"Requirement already satisfied: contourpy>=1.0.1 in /home/w/.local/lib/python3.10/site-packages (from matplotlib->ita) (1.0.5)\n",
"Requirement already satisfied: fonttools>=4.22.0 in /home/w/.local/lib/python3.10/site-packages (from matplotlib->ita) (4.37.4)\n",
"Requirement already satisfied: cycler>=0.10 in /home/w/.local/lib/python3.10/site-packages (from matplotlib->ita) (0.11.0)\n",
"Requirement already satisfied: pillow>=6.2.0 in /usr/lib/python3/dist-packages (from matplotlib->ita) (9.0.1)\n",
"Requirement already satisfied: kiwisolver>=1.0.1 in /home/w/.local/lib/python3.10/site-packages (from matplotlib->ita) (1.4.4)\n",
"Requirement already satisfied: python-dateutil>=2.7 in /home/w/.local/lib/python3.10/site-packages (from matplotlib->ita) (2.8.2)\n",
"Requirement already satisfied: pyparsing>=2.2.1 in /usr/lib/python3/dist-packages (from matplotlib->ita) (2.4.7)\n",
"Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil>=2.7->matplotlib->ita) (1.16.0)\n"
]
}
],
"execution_count": null
},
{
"metadata": {},
"source": [
"import ita"
],
"cell_type": "code",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"source": [
"def count_neighbors(board, i, j):\n",
" cnt = 0\n",
" for k in range(i - 1, i + 2):\n",
" for l in range(j - 1, j + 2):\n",
" if 0 <= k < len(board) and 0 <= l < len(board[k]):\n",
" cnt += board[k][l]\n",
" return cnt - board[i][j]\n",
"\n",
"\n",
"def is_alive(board, i, j):\n",
" neighbors_cnt = count_neighbors(board, i, j)\n",
" if board[i][j] == 0:\n",
" if neighbors_cnt == 3:\n",
" return 1\n",
" else:\n",
" return 0\n",
" else:\n",
" if neighbors_cnt in [2, 3]:\n",
" return 1\n",
" elif neighbors_cnt <= 1:\n",
" return 0\n",
" elif neighbors_cnt >= 4:\n",
" return 0\n",
"\n",
"\n",
"def next_generation(board):\n",
" row = len(board)\n",
" column = len(board[0])\n",
" next_board = ita.array.make2d(row, column)\n",
" for i in range(len(board)):\n",
" for j in range(len(board[0])):\n",
" next_board[i][j] = is_alive(board, i, j)\n",
" return next_board\n",
"\n",
"\n",
"def life_game(board, n):\n",
" results = ita.array.make1d(n)\n",
" results[0] = board\n",
" for i in range(1, n):\n",
" results[i] = next_generation(results[i - 1])\n",
" return results\n",
"\n",
"\n",
"board = [[1, 1, 0], [1, 0, 0], [0, 0, 0]]\n",
"n = 20\n",
"print(life_game(board, n))"
],
"cell_type": "code",
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[[1, 1, 0], [1, 0, 0], [0, 0, 0]], [[1, 1, 0], [1, 1, 0], [0, 0, 0]], [[1, 1, 0], [1, 1, 0], [0, 0, 0]], [[1, 1, 0], [1, 1, 0], [0, 0, 0]], [[1, 1, 0], [1, 1, 0], [0, 0, 0]], [[1, 1, 0], [1, 1, 0], [0, 0, 0]], [[1, 1, 0], [1, 1, 0], [0, 0, 0]], [[1, 1, 0], [1, 1, 0], [0, 0, 0]], [[1, 1, 0], [1, 1, 0], [0, 0, 0]], [[1, 1, 0], [1, 1, 0], [0, 0, 0]], [[1, 1, 0], [1, 1, 0], [0, 0, 0]], [[1, 1, 0], [1, 1, 0], [0, 0, 0]], [[1, 1, 0], [1, 1, 0], [0, 0, 0]], [[1, 1, 0], [1, 1, 0], [0, 0, 0]], [[1, 1, 0], [1, 1, 0], [0, 0, 0]], [[1, 1, 0], [1, 1, 0], [0, 0, 0]], [[1, 1, 0], [1, 1, 0], [0, 0, 0]], [[1, 1, 0], [1, 1, 0], [0, 0, 0]], [[1, 1, 0], [1, 1, 0], [0, 0, 0]], [[1, 1, 0], [1, 1, 0], [0, 0, 0]]]\n"
]
}
],
"execution_count": null
}
]
}
98 changes: 98 additions & 0 deletions docs/02advanced/01life-game/_samples/next_generation.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"nbformat": 4,
"nbformat_minor": 2,
"metadata": {},
"cells": [
{
"metadata": {},
"source": [
"!pip install ita"
],
"cell_type": "code",
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Defaulting to user installation because normal site-packages is not writeable\n",
"Requirement already satisfied: ita in /home/w/.local/lib/python3.10/site-packages (0.2.12)\n",
"Requirement already satisfied: numpy in /home/w/.local/lib/python3.10/site-packages (from ita) (1.23.3)\n",
"Requirement already satisfied: matplotlib in /home/w/.local/lib/python3.10/site-packages (from ita) (3.6.0)\n",
"Requirement already satisfied: cycler>=0.10 in /home/w/.local/lib/python3.10/site-packages (from matplotlib->ita) (0.11.0)\n",
"Requirement already satisfied: packaging>=20.0 in /home/w/.local/lib/python3.10/site-packages (from matplotlib->ita) (21.3)\n",
"Requirement already satisfied: pillow>=6.2.0 in /usr/lib/python3/dist-packages (from matplotlib->ita) (9.0.1)\n",
"Requirement already satisfied: kiwisolver>=1.0.1 in /home/w/.local/lib/python3.10/site-packages (from matplotlib->ita) (1.4.4)\n",
"Requirement already satisfied: pyparsing>=2.2.1 in /usr/lib/python3/dist-packages (from matplotlib->ita) (2.4.7)\n",
"Requirement already satisfied: fonttools>=4.22.0 in /home/w/.local/lib/python3.10/site-packages (from matplotlib->ita) (4.37.4)\n",
"Requirement already satisfied: contourpy>=1.0.1 in /home/w/.local/lib/python3.10/site-packages (from matplotlib->ita) (1.0.5)\n",
"Requirement already satisfied: python-dateutil>=2.7 in /home/w/.local/lib/python3.10/site-packages (from matplotlib->ita) (2.8.2)\n",
"Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil>=2.7->matplotlib->ita) (1.16.0)\n"
]
}
],
"execution_count": null
},
{
"metadata": {},
"source": [
"import ita"
],
"cell_type": "code",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"source": [
"def count_neighbors(board, i, j):\n",
" cnt = 0\n",
" for k in range(i - 1, i + 2):\n",
" for l in range(j - 1, j + 2):\n",
" if 0 <= k < len(board) and 0 <= l < len(board[k]):\n",
" cnt += board[k][l]\n",
" return cnt - board[i][j]\n",
"\n",
"\n",
"def is_alive(board, i, j):\n",
" neighbors_cnt = count_neighbors(board, i, j)\n",
" if board[i][j] == 0:\n",
" if neighbors_cnt == 3:\n",
" return 1\n",
" else:\n",
" return 0\n",
" else:\n",
" if neighbors_cnt in [2, 3]:\n",
" return 1\n",
" elif neighbors_cnt <= 1:\n",
" return 0\n",
" elif neighbors_cnt >= 4:\n",
" return 0\n",
"\n",
"\n",
"def next_generation(board):\n",
" row = len(board)\n",
" column = len(board[0])\n",
" next_board = ita.array.make2d(row, column)\n",
" for i in range(len(board)):\n",
" for j in range(len(board[0])):\n",
" next_board[i][j] = is_alive(board, i, j)\n",
" return next_board\n",
"\n",
"\n",
"board = [[1, 1, 0], [1, 0, 0], [0, 0, 0]]\n",
"print(next_generation(board))"
],
"cell_type": "code",
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[1, 1, 0], [1, 1, 0], [0, 0, 0]]\n"
]
}
],
"execution_count": null
}
]
}
6 changes: 6 additions & 0 deletions docs/02advanced/01life-game/alive.drawio.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions docs/02advanced/01life-game/birth.drawio.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions docs/02advanced/01life-game/board_example.drawio.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions docs/02advanced/01life-game/dead.drawio.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading