Skip to content

Commit

Permalink
better explanation
Browse files Browse the repository at this point in the history
  • Loading branch information
shafu0x committed Mar 3, 2024
1 parent b7caf6e commit c06075e
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions content/03_stack.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 11,
"id": "0e704969",
"metadata": {},
"outputs": [],
Expand All @@ -62,20 +62,26 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 73,
"id": "102cacf7",
"metadata": {},
"outputs": [],
"source": [
"class Stack:\n",
" def __init__(self): self.items = []\n",
" def __str__ (self): return \"\\n\".join(map(str, self.items[::-1]))\n",
" def __str__ (self): \n",
" ws = []\n",
" for i, item in enumerate(self.items[::-1]):\n",
" if i == 0 : ws.append(f\"{item} <first\")\n",
" elif i == len(self.items)-1: ws.append(f\"{item} <last\") \n",
" else : ws.append(str(item))\n",
" return \"\\n\".join(ws)\n",
" \n",
" def push(self, value): \n",
" if len(self.items) == MAXIMUM_STACK_SIZE-1: raise Exception(\"Stack overflow\")\n",
" self.items.append(value) \n",
" \n",
" def pop(self, n=0):\n",
" def pop(self, n=-1):\n",
" if len(self.items) < n: raise Exception(\"Stack overflow\")\n",
" del self.items[n]"
]
Expand All @@ -90,7 +96,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 74,
"id": "aaaf2494",
"metadata": {},
"outputs": [],
Expand All @@ -108,17 +114,17 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 75,
"id": "8771e0a0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"1 <first\n",
"4\n",
"2\n"
"2 <last\n"
]
}
],
Expand All @@ -135,47 +141,39 @@
"id": "1da65b97",
"metadata": {},
"source": [
"We pop one of the stack"
"We pop one of the stack. Which is `1`."
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 76,
"id": "41cf0942",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"outputs": [],
"source": [
"print(stack.pop())"
"stack.pop()"
]
},
{
"cell_type": "markdown",
"id": "fb3d028a",
"metadata": {},
"source": [
"Now only 3 values are left on the stack"
"Now only 2 values are left on the stack. Notice how we removed `1`."
]
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 77,
"id": "b1612ec9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"4\n"
"4 <first\n",
"2 <last\n"
]
}
],
Expand Down

0 comments on commit c06075e

Please sign in to comment.