{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Using list as a queue" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [], "source": [ "stock_prices = []" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Every elements is pushed when i try to add element at index 0" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [], "source": [ "stock_prices.insert(0, 182.7)\n", "stock_prices.insert(0, 183.6)\n", "stock_prices.insert(0, 185.4)" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[185.4, 183.6, 182.7]" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stock_prices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Queue is FIFO data structure, so when i pop, first element will remove" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "182.7" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stock_prices.pop()" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "183.6" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stock_prices.pop()" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "185.4" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stock_prices.pop()" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "queue is empty\n" ] } ], "source": [ "try:\n", " stock_prices.pop()\n", "except:\n", " print(\"queue is empty\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using collections.deque as a queue" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [], "source": [ "from collections import deque\n", "q = deque()" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__add__',\n", " '__bool__',\n", " '__class__',\n", " '__class_getitem__',\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": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(q)" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [], "source": [ "q.appendleft(9)\n", "q.appendleft(10)\n", "q.appendleft(11)" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "deque([11, 10, 9])" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "q" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "q.pop()" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "q.pop()" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "q.pop()" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Queue is empty\n" ] } ], "source": [ "try:\n", " q.pop()\n", "except:\n", " print(\"Queue is empty\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.7 64-bit", "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.10.7" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "10e9aac84282539994078db1be2f537695a8865e78eb5e3c29f0b2ce90162212" } } }, "nbformat": 4, "nbformat_minor": 2 }