|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Python: Everything\n", |
| 8 | + "- 5) Stack: Using lists in python as stacks, with an example to reverse a list\n", |
| 9 | + "<br>----------------------------------------------\n", |
| 10 | + "<br> By Hamed Shah-Hosseini\n", |
| 11 | + "<br> https://github.com/ostad-ai/Python-Everything" |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "markdown", |
| 16 | + "metadata": {}, |
| 17 | + "source": [ |
| 18 | + "**Stack:** A linear data structure which uses two functions *push* and *pop*. \n", |
| 19 | + "- The *push* adds the item at the top of stack\n", |
| 20 | + "- The *pop*, returns the item at the top of stack and removes it from stack." |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "code", |
| 25 | + "execution_count": 21, |
| 26 | + "metadata": {}, |
| 27 | + "outputs": [ |
| 28 | + { |
| 29 | + "name": "stdout", |
| 30 | + "output_type": "stream", |
| 31 | + "text": [ |
| 32 | + "The stack after three pushes: ['hello', 'world', 'bye']\n" |
| 33 | + ] |
| 34 | + } |
| 35 | + ], |
| 36 | + "source": [ |
| 37 | + "mystack=[] #define an empty stack as a list\n", |
| 38 | + "# to push items into stack\n", |
| 39 | + "mystack.append('hello')\n", |
| 40 | + "mystack.append('world')\n", |
| 41 | + "mystack.append('bye')\n", |
| 42 | + "print('The stack after three pushes:',mystack)" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "code", |
| 47 | + "execution_count": 16, |
| 48 | + "metadata": {}, |
| 49 | + "outputs": [ |
| 50 | + { |
| 51 | + "name": "stdout", |
| 52 | + "output_type": "stream", |
| 53 | + "text": [ |
| 54 | + "The stack before the pop: ['hello', 'world', 'bye']\n", |
| 55 | + "The item at top: bye\n", |
| 56 | + "The stack after the pop: ['hello', 'world']\n" |
| 57 | + ] |
| 58 | + } |
| 59 | + ], |
| 60 | + "source": [ |
| 61 | + "# the last item is the the first to pop: Last-in First-out\n", |
| 62 | + "print('The stack before the pop:',mystack)\n", |
| 63 | + "item_at_top=mystack.pop()\n", |
| 64 | + "print('The item at top:',item_at_top)\n", |
| 65 | + "print('The stack after the pop:',mystack)" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": 19, |
| 71 | + "metadata": {}, |
| 72 | + "outputs": [ |
| 73 | + { |
| 74 | + "name": "stdout", |
| 75 | + "output_type": "stream", |
| 76 | + "text": [ |
| 77 | + "Original list: [1, 2, 3, 4, 5, 6]\n", |
| 78 | + "Reversed list: [6, 5, 4, 3, 2, 1]\n" |
| 79 | + ] |
| 80 | + } |
| 81 | + ], |
| 82 | + "source": [ |
| 83 | + "# to reverse a list by stack\n", |
| 84 | + "mylist=[1,2,3,4,5,6]\n", |
| 85 | + "#---to push item into stack, use append\n", |
| 86 | + "#---to pop item from stack, use pop\n", |
| 87 | + "stack=[]\n", |
| 88 | + "mylist2=[]\n", |
| 89 | + "for item in mylist:\n", |
| 90 | + " stack.append(item)\n", |
| 91 | + "for i in range(len(stack)):\n", |
| 92 | + " mylist2.append(stack.pop())\n", |
| 93 | + "#---print lists\n", |
| 94 | + "print('Original list:',mylist)\n", |
| 95 | + "print('Reversed list:',mylist2)" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "markdown", |
| 100 | + "metadata": {}, |
| 101 | + "source": [ |
| 102 | + "**Hint:** We could use *reverse* to reverse a list in place:" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "code", |
| 107 | + "execution_count": 25, |
| 108 | + "metadata": {}, |
| 109 | + "outputs": [ |
| 110 | + { |
| 111 | + "name": "stdout", |
| 112 | + "output_type": "stream", |
| 113 | + "text": [ |
| 114 | + "Original list is revered: [6, 5, 4, 3, 2, 1]\n" |
| 115 | + ] |
| 116 | + } |
| 117 | + ], |
| 118 | + "source": [ |
| 119 | + "mylist=[1,2,3,4,5,6]\n", |
| 120 | + "mylist.reverse() #in-place reverse\n", |
| 121 | + "print('Original list is revered: ',mylist)" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "markdown", |
| 126 | + "metadata": {}, |
| 127 | + "source": [ |
| 128 | + "**Hint:** Another way to reverse a list:\n", |
| 129 | + "<br>The original list does not change" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "code", |
| 134 | + "execution_count": 23, |
| 135 | + "metadata": {}, |
| 136 | + "outputs": [ |
| 137 | + { |
| 138 | + "name": "stdout", |
| 139 | + "output_type": "stream", |
| 140 | + "text": [ |
| 141 | + "Original list: [1, 2, 3, 4, 5, 6]\n", |
| 142 | + "Reversed list: [6, 5, 4, 3, 2, 1]\n" |
| 143 | + ] |
| 144 | + } |
| 145 | + ], |
| 146 | + "source": [ |
| 147 | + "mylist=[1,2,3,4,5,6]\n", |
| 148 | + "mylist2=mylist[::-1]\n", |
| 149 | + "print('Original list:',mylist)\n", |
| 150 | + "print('Reversed list:',mylist2)" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "code", |
| 155 | + "execution_count": null, |
| 156 | + "metadata": {}, |
| 157 | + "outputs": [], |
| 158 | + "source": [] |
| 159 | + } |
| 160 | + ], |
| 161 | + "metadata": { |
| 162 | + "kernelspec": { |
| 163 | + "display_name": "Python 3", |
| 164 | + "language": "python", |
| 165 | + "name": "python3" |
| 166 | + }, |
| 167 | + "language_info": { |
| 168 | + "codemirror_mode": { |
| 169 | + "name": "ipython", |
| 170 | + "version": 3 |
| 171 | + }, |
| 172 | + "file_extension": ".py", |
| 173 | + "mimetype": "text/x-python", |
| 174 | + "name": "python", |
| 175 | + "nbconvert_exporter": "python", |
| 176 | + "pygments_lexer": "ipython3", |
| 177 | + "version": "3.8.3" |
| 178 | + } |
| 179 | + }, |
| 180 | + "nbformat": 4, |
| 181 | + "nbformat_minor": 5 |
| 182 | +} |
0 commit comments