Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding postfix stack application #21

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
398 changes: 398 additions & 0 deletions Python15_Stack.ipynb
Original file line number Diff line number Diff line change
@@ -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": [
"<a href=\"https://colab.research.google.com/github/killuen/cp4all/blob/main/Python15_Stack.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"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": []
}
]
}
Loading