{ "cells": [ { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "class Stack:\n", "\n", " # Python list implementation of stacks\n", "\n", " def __init__(self):\n", " # construct an empty stack\n", " self._data = []\n", "\n", " def __str__(self):\n", " # string representation of internal state of the stack\n", " return \"TOP: \"+ str(self._data)\n", " \n", " def __len__(self):\n", " # returns number of items in stack\n", " return len(self._data)\n", "\n", " def is_empty(self):\n", " # returns True if stack is empty and False otherwise\n", " return self._data == []\n", "\n", " def top(self):\n", " # returns top item of stack it stack is not empty; otherwise returns None\n", " return self._data[0]\n", "\n", " def pop(self):\n", " # similar to top, except this also deletes top item from stack.\n", " x = self._data[0]\n", " self._data.remove(x)\n", " return x\n", " \n", " def push(self,e):\n", " # puts new item e on top of stack\n", " self._data = [e] + self._data\n", " " ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "s1 = Stack()" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "s1.push(5)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TOP: [5]\n" ] } ], "source": [ "print(s1)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "s1.push(8)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TOP: [8, 5]\n" ] } ], "source": [ "print(s1)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "s1.push(3)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TOP: [3, 8, 5]\n" ] } ], "source": [ "print(s1)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "print(s1.pop())" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TOP: [8, 5]\n" ] } ], "source": [ "print(s1)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "s = [1,2,3,4,5,6,7,8,9,10]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[3]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 5, 6]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[3:6]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[0]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "s.remove(s[0])" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 3, 4, 5, 6, 7, 8, 9, 10]\n" ] } ], "source": [ "print(s)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(s)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "s = \"Sunderraman\"" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('ram')" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "s = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'[1, 2, 3]'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(s)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "input a string (q to quit): abc\n", "reverse of string: cba\n", "input a string (q to quit): this is a long sentence\n", "reverse of string: ecnetnes gnol a si siht\n", "input a string (q to quit): hello \n", "reverse of string: olleh\n", "input a string (q to quit): q\n" ] } ], "source": [ "def main():\n", " # implements the Reverse program\n", " while True:\n", " s = input(\"input a string (q to quit): \").strip()\n", " if s == 'q':\n", " break\n", " st = Stack()\n", " for c in s:\n", " st.push(c)\n", " t = \"\"\n", " while True:\n", " if st.is_empty():\n", " break\n", " t = t + st.pop()\n", " print(\"reverse of string: \"+t)\n", " \n", "\n", "main()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }