From ef5a26a897059349193ee5e0991d73a1de66e1d6 Mon Sep 17 00:00:00 2001 From: killuen Date: Thu, 15 Oct 2020 13:10:44 +0800 Subject: [PATCH 1/2] Created using Colaboratory --- Python15_Stack.ipynb | 398 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 398 insertions(+) create mode 100644 Python15_Stack.ipynb diff --git a/Python15_Stack.ipynb b/Python15_Stack.ipynb new file mode 100644 index 0000000..23266a4 --- /dev/null +++ b/Python15_Stack.ipynb @@ -0,0 +1,398 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Python15 - Stack.ipynb", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WYDSZcGSKwqH" + }, + "source": [ + "# Stack\n", + "- last in first out (LIFO) data structure\n", + "- essential data: data, top\n", + "- top is the position when the next item will be inserted or removed\n", + "- can be implemented using array or linked list" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "RgRsqtYFKrH0", + "outputId": "cae94c74-de91-44d8-a25e-95121205d2a7", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 158 + } + }, + "source": [ + "# Stack class\n", + "class Stack:\n", + "\n", + " def __init__(self): # encapsulation\n", + " self.data = []\n", + " self.top = -1 # assume/take -1 to be invalid index\n", + "\n", + " def is_empty(self):\n", + " return self.top == -1\n", + "\n", + " def push(self, value): # insert\n", + " self.top = 0\n", + " self.data.insert(0, value)\n", + " print(value, \"inserted\")\n", + "\n", + " def pop(self): # delete\n", + " if self.is_empty():\n", + " print(\"Cannot delete from empty stack\")\n", + " return -1 # signify error\n", + " else:\n", + " value = self.data[self.top]\n", + " del self.data[self.top]\n", + " #print(value, \"deleted\")\n", + " if len(self.data) == 0:\n", + " self.top = -1\n", + " return value\n", + "\n", + " def peek(self): # return top item without removing it\n", + " if self.is_empty():\n", + " return \"Empty stack\"\n", + " else:\n", + " return self.data[self.top]\n", + "\n", + " def display(self):\n", + " if self.is_empty():\n", + " print(\"Empty stack\")\n", + " else:\n", + " print(\"Contents: \", end='')\n", + " for item in self.data:\n", + " print(item, end=' ')\n", + " print()\n", + "\n", + "# main\n", + "stack = Stack()\n", + "stack.push(35)\n", + "stack.push(88)\n", + "stack.push(19)\n", + "#print(stack.data, stack.top)\n", + "stack.display()\n", + "stack.pop()\n", + "stack.display()\n", + "print(stack.peek())\n", + "stack.pop()\n", + "stack.pop()\n", + "stack.pop()\n", + "stack.display()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "35 inserted\n", + "88 inserted\n", + "19 inserted\n", + "Contents: 19 88 35 \n", + "Contents: 88 35 \n", + "88\n", + "Cannot delete from empty stack\n", + "Empty stack\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xZnIB3u5jMmL" + }, + "source": [ + "Implement stack using append instead of insert" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "5r8vGR4Ga0Cj", + "outputId": "04049849-ce0d-4f50-c4f5-3ed9a815a6f7", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 84 + } + }, + "source": [ + "class Stack:\n", + "\n", + " def __init__(self):\n", + " self.data = []\n", + "\n", + " def is_empty(self):\n", + " return len(self.data) == 0 # self.data == []\n", + "\n", + " def push(self, value): # insert\n", + " self.data.append(value)\n", + "\n", + " def pop(self): # delete\n", + " if self.is_empty():\n", + " return \"Cannot delete from empty stack\"\n", + " return self.data.pop()\n", + "\n", + " def peek(self):\n", + " return self.data[len(self.data)-1] # self.data[-1]\n", + "\n", + " def size(self):\n", + " return len(self.data)\n", + "\n", + " def display(self):\n", + " if self.is_empty():\n", + " print(\"Empty stack\")\n", + " else:\n", + " print(\"Contents: \", end='')\n", + " for i in range(len(self.data)):\n", + " print(self.data[i], end=' ')\n", + " print()\n", + "\n", + "\n", + "# main\n", + "stack = Stack()\n", + "stack.push(35)\n", + "stack.push(88)\n", + "stack.push(19)\n", + "#print(stack.data, stack.top)\n", + "stack.display()\n", + "stack.pop()\n", + "stack.display()\n", + "print(stack.peek())\n", + "stack.pop()\n", + "stack.pop()\n", + "stack.pop()\n", + "stack.display()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Contents: 35 88 19 \n", + "Contents: 35 88 \n", + "88\n", + "Empty stack\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "3RQ4PGggc6sH", + "outputId": "59ed5874-503d-4efc-e098-dde36820d5be", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 191 + } + }, + "source": [ + "# Stack application - reverse\n", + "#items = 'stressed'\n", + "items = list('stressed')\n", + "print(items)\n", + "s = Stack()\n", + "for item in items:\n", + " s.push(item)\n", + "for i in range(len(s.data)):\n", + " print(s.pop(), end='')" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['s', 't', 'r', 'e', 's', 's', 'e', 'd']\n", + "s inserted\n", + "t inserted\n", + "r inserted\n", + "e inserted\n", + "s inserted\n", + "s inserted\n", + "e inserted\n", + "d inserted\n", + "desserts" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "J2suTbJtjZeX" + }, + "source": [ + "35 // 2 = 17 r 1\n", + "\n", + "17 // 2 = 8 r 1\n", + "\n", + "8 // 2 = 4 r 0\n", + "\n", + "4 // 2 = 2 r 0\n", + "\n", + "2 // 2 = 1 r 0\n", + "\n", + "1\n", + "\n", + "35 = 100011" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ufT0KeA1c9ZR", + "outputId": "a378c655-38b7-4680-8cb2-8f4354db51f5", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 121 + } + }, + "source": [ + "# Stack application - decimal to binary conversion\n", + "dec = 35\n", + "s = Stack()\n", + "while dec >= 2:\n", + " rem = dec % 2\n", + " s.push(rem)\n", + " dec //= 2 # dec = dec // 2\n", + "print(1, end='')\n", + "while not s.is_empty():\n", + " print(s.pop(), end='')" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "1 inserted\n", + "1 inserted\n", + "0 inserted\n", + "0 inserted\n", + "0 inserted\n", + "100011" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ZeBmJh0ykq9h" + }, + "source": [ + "# Stack application - check balanced expression\n", + "expr = '(2 + 3) * 4'" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "ACko8RHWp5E0", + "outputId": "cd30daba-ad40-40cd-b344-3301bcf2296d", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 283 + } + }, + "source": [ + "# Stack application - calculating postfix\n", + "postfix = \"231+*5-\"\n", + "pfl = []\n", + "stack = Stack()\n", + "for i in range(len(postfix)):\n", + " pfl.append(postfix[i])\n", + "\n", + "j = 0\n", + "for i in range(len(pfl)):\n", + " if pfl[i].isdigit():\n", + " stack.push(int(pfl[i]))\n", + " else:\n", + " num2, num1 = stack.pop(), stack.pop()\n", + "\n", + " result = 0\n", + " if pfl[i] == \"+\":\n", + " result = num1 + num2\n", + "\n", + " if pfl[i] == \"-\":\n", + " result = num1 - num2\n", + "\n", + " if pfl[i] == \"*\":\n", + " result = num1 * num2\n", + "\n", + " if pfl[i] == \"/\":\n", + " result = num1 / num2\n", + "\n", + " stack.push(result)\n", + " stack.display()\n", + "\n", + "stack.display()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2 inserted\n", + "Contents: 2 \n", + "3 inserted\n", + "Contents: 3 2 \n", + "1 inserted\n", + "Contents: 1 3 2 \n", + "4 inserted\n", + "Contents: 4 2 \n", + "8 inserted\n", + "Contents: 8 \n", + "5 inserted\n", + "Contents: 5 8 \n", + "3 inserted\n", + "Contents: 3 \n", + "Contents: 3 \n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "gDCbnB3Bp8Qj" + }, + "source": [ + "" + ], + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file From 37f370d70e25a75ab147caa331d8b02e7ea7dd83 Mon Sep 17 00:00:00 2001 From: killuen Date: Fri, 21 Jan 2022 04:01:26 +0800 Subject: [PATCH 2/2] Update Python15_Stack.ipynb --- computing_sh/Python15_Stack.ipynb | 213 ++++++++++++++++++------------ 1 file changed, 126 insertions(+), 87 deletions(-) diff --git a/computing_sh/Python15_Stack.ipynb b/computing_sh/Python15_Stack.ipynb index b884d10..cd3cd8d 100644 --- a/computing_sh/Python15_Stack.ipynb +++ b/computing_sh/Python15_Stack.ipynb @@ -1,16 +1,4 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "name": "Python15 - Stack.ipynb", - "provenance": [] - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - } - }, "cells": [ { "cell_type": "markdown", @@ -27,14 +15,31 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { - "id": "RgRsqtYFKrH0", - "outputId": "e11a015e-ee05-47fd-fc3b-5a8333378769", "colab": { "base_uri": "https://localhost:8080/", "height": 151 - } + }, + "id": "RgRsqtYFKrH0", + "outputId": "e11a015e-ee05-47fd-fc3b-5a8333378769" }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "35 inserted\n", + "88 inserted\n", + "19 inserted\n", + "Contents: 19 88 35 \n", + "Contents: 88 35 \n", + "88\n", + "Cannot delete from empty stack\n", + "Empty stack\n" + ] + } + ], "source": [ "# Stack class\n", "class Stack:\n", @@ -92,23 +97,6 @@ "stack.pop()\n", "stack.pop()\n", "stack.display()" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "35 inserted\n", - "88 inserted\n", - "19 inserted\n", - "Contents: 19 88 35 \n", - "Contents: 88 35 \n", - "88\n", - "Cannot delete from empty stack\n", - "Empty stack\n" - ], - "name": "stdout" - } ] }, { @@ -122,14 +110,27 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { - "id": "5r8vGR4Ga0Cj", - "outputId": "04049849-ce0d-4f50-c4f5-3ed9a815a6f7", "colab": { "base_uri": "https://localhost:8080/", "height": 84 - } + }, + "id": "5r8vGR4Ga0Cj", + "outputId": "04049849-ce0d-4f50-c4f5-3ed9a815a6f7" }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Contents: 35 88 19 \n", + "Contents: 35 88 \n", + "88\n", + "Empty stack\n" + ] + } + ], "source": [ "class Stack:\n", "\n", @@ -177,45 +178,22 @@ "stack.pop()\n", "stack.pop()\n", "stack.display()" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Contents: 35 88 19 \n", - "Contents: 35 88 \n", - "88\n", - "Empty stack\n" - ], - "name": "stdout" - } ] }, { "cell_type": "code", + "execution_count": null, "metadata": { - "id": "3RQ4PGggc6sH", - "outputId": "59ed5874-503d-4efc-e098-dde36820d5be", "colab": { "base_uri": "https://localhost:8080/", "height": 191 - } + }, + "id": "3RQ4PGggc6sH", + "outputId": "59ed5874-503d-4efc-e098-dde36820d5be" }, - "source": [ - "# Stack application - reverse\n", - "#items = 'stressed'\n", - "items = list('stressed')\n", - "print(items)\n", - "s = Stack()\n", - "for item in items:\n", - " s.push(item)\n", - "for i in range(len(s.data)):\n", - " print(s.pop(), end='')" - ], - "execution_count": null, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "['s', 't', 'r', 'e', 's', 's', 'e', 'd']\n", @@ -228,9 +206,19 @@ "e inserted\n", "d inserted\n", "desserts" - ], - "name": "stdout" + ] } + ], + "source": [ + "# Stack application - reverse\n", + "#items = 'stressed'\n", + "items = list('stressed')\n", + "print(items)\n", + "s = Stack()\n", + "for item in items:\n", + " s.push(item)\n", + "for i in range(len(s.data)):\n", + " print(s.pop(), end='')" ] }, { @@ -256,29 +244,18 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { - "id": "ufT0KeA1c9ZR", - "outputId": "a378c655-38b7-4680-8cb2-8f4354db51f5", "colab": { "base_uri": "https://localhost:8080/", "height": 121 - } + }, + "id": "ufT0KeA1c9ZR", + "outputId": "a378c655-38b7-4680-8cb2-8f4354db51f5" }, - "source": [ - "# Stack application - decimal to binary conversion\n", - "dec = 35\n", - "s = Stack()\n", - "while dec >= 2:\n", - " rem = dec % 2\n", - " s.push(rem)\n", - " dec //= 2 # dec = dec // 2\n", - "print(1, end='')\n", - "while not s.is_empty():\n", - " print(s.pop(), end='')" - ], - "execution_count": null, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "1 inserted\n", @@ -287,22 +264,84 @@ "0 inserted\n", "0 inserted\n", "100011" - ], - "name": "stdout" + ] } + ], + "source": [ + "# Stack application - decimal to binary conversion\n", + "dec = 35\n", + "s = Stack()\n", + "while dec >= 2:\n", + " rem = dec % 2\n", + " s.push(rem)\n", + " dec //= 2 # dec = dec // 2\n", + "print(1, end='')\n", + "while not s.is_empty():\n", + " print(s.pop(), end='')" ] }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "ZeBmJh0ykq9h" }, + "outputs": [], "source": [ "# Stack application - check balanced expression\n", "expr = '(2 + 3) * 4'" - ], + ] + }, + { + "cell_type": "code", "execution_count": null, - "outputs": [] + "metadata": {}, + "outputs": [], + "source": [ + "# Stack application - calculating postfix\n", + "postfix = \"231+*5-\"\n", + "pfl = []\n", + "stack = Stack()\n", + "for i in range(len(postfix)):\n", + " pfl.append(postfix[i])\n", + "\n", + "j = 0\n", + "for i in range(len(pfl)):\n", + " if pfl[i].isdigit():\n", + " stack.push(int(pfl[i]))\n", + " else:\n", + " num2, num1 = stack.pop(), stack.pop()\n", + "\n", + " result = 0\n", + " if pfl[i] == \"+\":\n", + " result = num1 + num2\n", + "\n", + " if pfl[i] == \"-\":\n", + " result = num1 - num2\n", + "\n", + " if pfl[i] == \"*\":\n", + " result = num1 * num2\n", + "\n", + " if pfl[i] == \"/\":\n", + " result = num1 / num2\n", + "\n", + " stack.push(result)\n", + " stack.display()\n", + "\n", + "stack.display()" + ] } - ] -} \ No newline at end of file + ], + "metadata": { + "colab": { + "name": "Python15 - Stack.ipynb", + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}