{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Data Structures Tutorial: Stack

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Using a list as a stack

" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "s = []\n", "s.append('https://www.cnn.com/')\n", "s.append('https://www.cnn.com/world')\n", "s.append('https://www.cnn.com/india')\n", "s.append('https://www.cnn.com/china')" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'https://www.cnn.com/china'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.pop()" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'https://www.cnn.com/india'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.pop()" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "['https://www.cnn.com/', 'https://www.cnn.com/world']" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "'https://www.cnn.com/world'" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**The issue with using a list as a stack is that list uses dymanic array internally and when it reaches its capacity it will reallocate a big chunk of memory somewhere else in memory area and copy all the elements. For example in below diagram if a list has a capacity of 10 and we try to insert 11th element, it will not allocate new memory in a different memory region, copy all 10 elements and then insert the 11th element. So overhead here is (1) allocate new memory plus (2) copy all existing elements in new memory area**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Using deque as a stack

" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from collections import deque\n", "stack = deque()" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__add__',\n", " '__bool__',\n", " '__class__',\n", " '__contains__',\n", " '__copy__',\n", " '__delattr__',\n", " '__delitem__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__getitem__',\n", " '__gt__',\n", " '__hash__',\n", " '__iadd__',\n", " '__imul__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__iter__',\n", " '__le__',\n", " '__len__',\n", " '__lt__',\n", " '__mul__',\n", " '__ne__',\n", " '__new__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__reversed__',\n", " '__rmul__',\n", " '__setattr__',\n", " '__setitem__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__',\n", " 'append',\n", " 'appendleft',\n", " 'clear',\n", " 'copy',\n", " 'count',\n", " 'extend',\n", " 'extendleft',\n", " 'index',\n", " 'insert',\n", " 'maxlen',\n", " 'pop',\n", " 'popleft',\n", " 'remove',\n", " 'reverse',\n", " 'rotate']" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(stack)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "deque(['https://www.cnn.com/'])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stack.append(\"https://www.cnn.com/\")\n", "stack" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "deque(['https://www.cnn.com/', 'https://www.cnn.com/world'])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stack.append(\"https://www.cnn.com/world\")\n", "stack" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "deque(['https://www.cnn.com/',\n", " 'https://www.cnn.com/world',\n", " 'https://www.cnn.com/india',\n", " 'https://www.cnn.com/china'])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stack.append(\"https://www.cnn.com/india\")\n", "stack.append(\"https://www.cnn.com/china\")\n", "stack" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'https://www.cnn.com/china'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stack.pop()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "'https://www.cnn.com/india'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stack.pop()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "deque(['https://www.cnn.com/', 'https://www.cnn.com/world'])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stack" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'https://www.cnn.com/world'" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stack.pop()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'https://www.cnn.com/'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stack.pop()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "scrolled": false }, "outputs": [ { "ename": "IndexError", "evalue": "pop from an empty deque", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstack\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mIndexError\u001b[0m: pop from an empty deque" ] } ], "source": [ "stack.pop()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Implement Stack class using a deque

" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "class Stack:\n", " def __init__(self):\n", " self.container = deque()\n", " \n", " def push(self,val):\n", " self.container.append(val)\n", " \n", " def pop(self):\n", " return self.container.pop()\n", " \n", " def peek(self):\n", " return self.container[-1]\n", " \n", " def is_empty(self):\n", " return len(self.container)==0\n", " \n", " def size(self):\n", " return len(self.container)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "s = Stack()\n", "s.push(5)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.is_empty()" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.pop()" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.is_empty()" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "s.push(9)\n", "s.push(34)\n", "s.push(78)\n", "s.push(12)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.peek()" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.pop()" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "78" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.pop()" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "34" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.pop()" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.pop()" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "pop from an empty deque", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ms\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;32m\u001b[0m in \u001b[0;36mpop\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcontainer\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 10\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 11\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mpeek\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mIndexError\u001b[0m: pop from an empty deque" ] } ], "source": [ "s.pop()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }