|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 3, |
| 6 | + "id": "6198607b", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "#array based version of linked nodes based code in doubly-LL5.py\n", |
| 11 | + "\n", |
| 12 | + "class ArrayList:\n", |
| 13 | + " class ALIterator:\n", |
| 14 | + " def __init__(self, index=0, list=None):\n", |
| 15 | + " self.current = index-1\n", |
| 16 | + " self.list = list\n", |
| 17 | + " def __eq__( self, rhs ):\n", |
| 18 | + " return self.current == rhs.current\n", |
| 19 | + " def __ne__( self, rhs ):\n", |
| 20 | + " return self.current != rhs.current\n", |
| 21 | + " def getObject( self ):\n", |
| 22 | + " return self.list.Array[self.current]\n", |
| 23 | + " def __iter__(self): # this is not mandatory, why\n", |
| 24 | + " return self\n", |
| 25 | + " def __next__( self ):\n", |
| 26 | + " if self.current != self.list.ALSize-1:\n", |
| 27 | + " self.current = self.current + 1\n", |
| 28 | + " return self\n", |
| 29 | + " else:\n", |
| 30 | + " raise StopIteration\n", |
| 31 | + " class ALRIterator:\n", |
| 32 | + " def __init__(self, index, list=None):\n", |
| 33 | + " self.current = index+1\n", |
| 34 | + " self.list = list\n", |
| 35 | + "\n", |
| 36 | + " def __eq__( self, rhs ):\n", |
| 37 | + " return self.current == rhs.current\n", |
| 38 | + "\n", |
| 39 | + " def __ne__( self, rhs ):\n", |
| 40 | + " return self.current != rhs.current\n", |
| 41 | + "\n", |
| 42 | + " def getObject( self ):\n", |
| 43 | + " return self.list.Array[self.current]\n", |
| 44 | + "\n", |
| 45 | + " def __iter__(self): # this is mandatory, why\n", |
| 46 | + " return self\n", |
| 47 | + "\n", |
| 48 | + " def __next__( self ):\n", |
| 49 | + " if self.current != 0:\n", |
| 50 | + " self.current = self.current - 1\n", |
| 51 | + " return self\n", |
| 52 | + " else:\n", |
| 53 | + " raise StopIteration\n", |
| 54 | + "\n", |
| 55 | + " def __init__(self, ARSize=100):\n", |
| 56 | + " self.ARSize = ARSize\n", |
| 57 | + " self.ALSize = 0\n", |
| 58 | + " self.Array = [None] * self.ARSize\n", |
| 59 | + "\n", |
| 60 | + " def __iter__(self):\n", |
| 61 | + " return self.ALIterator(0, self)\n", |
| 62 | + "\n", |
| 63 | + " def __reversed__(self):\n", |
| 64 | + " return self.ALRIterator(self.ALSize - 1, self)\n", |
| 65 | + "\n", |
| 66 | + " def begining(self):\n", |
| 67 | + " return self.ALIterator(0, self)\n", |
| 68 | + "\n", |
| 69 | + " def end(self):\n", |
| 70 | + " return self.ALIterator(self.ALSize, self)\n", |
| 71 | + "\n", |
| 72 | + " def rbegining(self):\n", |
| 73 | + " return self.ALRIterator(self.ALSize - 1, self)\n", |
| 74 | + "\n", |
| 75 | + " def rend(self):\n", |
| 76 | + " return self.ALRIterator(-1, self)\n", |
| 77 | + "\n", |
| 78 | + " def isFull(self):\n", |
| 79 | + " return self.ALSize == self.ARSize\n", |
| 80 | + "\n", |
| 81 | + " def isEmpty(self):\n", |
| 82 | + " return self.ALSize == 0\n", |
| 83 | + "\n", |
| 84 | + " def size(self):\n", |
| 85 | + " return self.ALSize\n", |
| 86 | + "\n", |
| 87 | + " def append(self, o):\n", |
| 88 | + " if not self.isFull():\n", |
| 89 | + " self.Array[self.ALSize] = o\n", |
| 90 | + " self.ALSize = self.ALSize + 1\n", |
| 91 | + "\n", |
| 92 | + " def remove(self, index):\n", |
| 93 | + " if type(index) is self.ALIterator:\n", |
| 94 | + " index = index.current\n", |
| 95 | + "\n", |
| 96 | + " if index < 0 or index >= self.ALSize:\n", |
| 97 | + " raise Exception(\"Invalid Index\")\n", |
| 98 | + " j = index\n", |
| 99 | + " while j < self.ALSize:\n", |
| 100 | + " self.Array[j] = self.Array[j + 1]\n", |
| 101 | + " j = j + 1\n", |
| 102 | + " self.ALSize = self.ALSize - 1\n", |
| 103 | + "\n", |
| 104 | + " def Display(self):\n", |
| 105 | + " j = 0\n", |
| 106 | + " while j < self.ALSize:\n", |
| 107 | + " print(self.Array[j])\n", |
| 108 | + " j = j + 1\n", |
| 109 | + " print()\n", |
| 110 | + "\n", |
| 111 | + "\n" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "code", |
| 116 | + "execution_count": 4, |
| 117 | + "id": "3f748641", |
| 118 | + "metadata": {}, |
| 119 | + "outputs": [ |
| 120 | + { |
| 121 | + "name": "stdout", |
| 122 | + "output_type": "stream", |
| 123 | + "text": [ |
| 124 | + "Display 10 items\n", |
| 125 | + "10\n", |
| 126 | + "20\n", |
| 127 | + "30\n", |
| 128 | + "70\n", |
| 129 | + "50\n", |
| 130 | + "60\n", |
| 131 | + "80\n", |
| 132 | + "90\n", |
| 133 | + "40\n", |
| 134 | + "\n", |
| 135 | + "Display without (removed) first two items\n", |
| 136 | + "30\n", |
| 137 | + "70\n", |
| 138 | + "50\n", |
| 139 | + "60\n", |
| 140 | + "80\n", |
| 141 | + "90\n", |
| 142 | + "40\n", |
| 143 | + "\n", |
| 144 | + "\n", |
| 145 | + "OUTPUT of 'for d in AL:'\n", |
| 146 | + "30\n", |
| 147 | + "70\n", |
| 148 | + "50\n", |
| 149 | + "60\n", |
| 150 | + "80\n", |
| 151 | + "90\n", |
| 152 | + "40\n", |
| 153 | + "\n", |
| 154 | + "OUTPUT of 'for d in reversed(AL):'\n", |
| 155 | + "40\n", |
| 156 | + "90\n", |
| 157 | + "80\n", |
| 158 | + "60\n", |
| 159 | + "50\n", |
| 160 | + "70\n", |
| 161 | + "30\n", |
| 162 | + "\n", |
| 163 | + "OUTPUT through manual next calls\n", |
| 164 | + "first through itm1: 30\n", |
| 165 | + "next through itm1: 70\n", |
| 166 | + "first through itm2: 30\n", |
| 167 | + "next through itm2: 70\n", |
| 168 | + "next through itm2: 50\n", |
| 169 | + "next through itm1: 50\n", |
| 170 | + "Removing current data\n", |
| 171 | + "Removed\n", |
| 172 | + "next through itm2: 80\n", |
| 173 | + "next through itm2: 90\n", |
| 174 | + "next through itm2: 40\n", |
| 175 | + "next through itm1: 80\n", |
| 176 | + "\n", |
| 177 | + "OUTPUT through beginning and end\n", |
| 178 | + "40\n", |
| 179 | + "\n", |
| 180 | + "OUTPUT through rbeginning and rend\n", |
| 181 | + "30\n", |
| 182 | + "\n" |
| 183 | + ] |
| 184 | + } |
| 185 | + ], |
| 186 | + "source": [ |
| 187 | + "def main():\n", |
| 188 | + " AL = ArrayList()\n", |
| 189 | + " AL.append(10)\n", |
| 190 | + " AL.append(20)\n", |
| 191 | + " AL.append(30)\n", |
| 192 | + " AL.append(70)\n", |
| 193 | + " AL.append(50)\n", |
| 194 | + " AL.append(60)\n", |
| 195 | + " AL.append(80)\n", |
| 196 | + " AL.append(90)\n", |
| 197 | + " AL.append(40)\n", |
| 198 | + " print(\"Display 10 items\")\n", |
| 199 | + " AL.Display()\n", |
| 200 | + " AL.remove(0)\n", |
| 201 | + " AL.remove(0)\n", |
| 202 | + " print(\"Display without (removed) first two items\")\n", |
| 203 | + " AL.Display()\n", |
| 204 | + " print()\n", |
| 205 | + " print(\"OUTPUT of 'for d in AL:'\")\n", |
| 206 | + " for d in AL:\n", |
| 207 | + " print(d.getObject())\n", |
| 208 | + " print()\n", |
| 209 | + " print(\"OUTPUT of 'for d in reversed(AL):'\")\n", |
| 210 | + " for d in reversed(AL):\n", |
| 211 | + " print(d.getObject())\n", |
| 212 | + " print()\n", |
| 213 | + " print(\"OUTPUT through manual next calls\")\n", |
| 214 | + " itm1 = iter(AL)\n", |
| 215 | + " itm2 = AL.begining()\n", |
| 216 | + " print(\"first through itm1: \" + str(next(itm1).getObject()))\n", |
| 217 | + " print(\"next through itm1: \" + str(next(itm1).getObject()))\n", |
| 218 | + " print(\"first through itm2: \" + str(next(itm2).getObject()))\n", |
| 219 | + " print(\"next through itm2: \" + str(next(itm2).getObject()))\n", |
| 220 | + " print(\"next through itm2: \" + str(next(itm2).getObject()))\n", |
| 221 | + " print(\"next through itm1: \" + str(next(itm1).getObject()))\n", |
| 222 | + " print(\"Removing current data\")\n", |
| 223 | + " AL.remove(itm1)\n", |
| 224 | + " print(\"Removed\")\n", |
| 225 | + " print(\"next through itm2: \" + str(next(itm2).getObject()))\n", |
| 226 | + " print(\"next through itm2: \" + str(next(itm2).getObject()))\n", |
| 227 | + " print(\"next through itm2: \" + str(next(itm2).getObject()))\n", |
| 228 | + " # due to remove above, print(\"next through itm2: \" + str(next(itm2).getObject()))\n", |
| 229 | + " print(\"next through itm1: \" + str(next(itm1).getObject()))\n", |
| 230 | + " print()\n", |
| 231 | + " print(\"OUTPUT through beginning and end\")\n", |
| 232 | + " i = AL.begining()\n", |
| 233 | + " while i != AL.end():\n", |
| 234 | + " i = next(i)\n", |
| 235 | + " print(i.getObject())\n", |
| 236 | + " print()\n", |
| 237 | + " print(\"OUTPUT through rbeginning and rend\")\n", |
| 238 | + " i = AL.rbegining()\n", |
| 239 | + " while i != AL.rend():\n", |
| 240 | + " t = next(i)\n", |
| 241 | + " print(t.getObject())\n", |
| 242 | + " print()\n", |
| 243 | + "main()" |
| 244 | + ] |
| 245 | + }, |
| 246 | + { |
| 247 | + "cell_type": "code", |
| 248 | + "execution_count": null, |
| 249 | + "id": "15081011", |
| 250 | + "metadata": {}, |
| 251 | + "outputs": [], |
| 252 | + "source": [] |
| 253 | + } |
| 254 | + ], |
| 255 | + "metadata": { |
| 256 | + "kernelspec": { |
| 257 | + "display_name": "Python 3 (ipykernel)", |
| 258 | + "language": "python", |
| 259 | + "name": "python3" |
| 260 | + }, |
| 261 | + "language_info": { |
| 262 | + "codemirror_mode": { |
| 263 | + "name": "ipython", |
| 264 | + "version": 3 |
| 265 | + }, |
| 266 | + "file_extension": ".py", |
| 267 | + "mimetype": "text/x-python", |
| 268 | + "name": "python", |
| 269 | + "nbconvert_exporter": "python", |
| 270 | + "pygments_lexer": "ipython3", |
| 271 | + "version": "3.10.9" |
| 272 | + } |
| 273 | + }, |
| 274 | + "nbformat": 4, |
| 275 | + "nbformat_minor": 5 |
| 276 | +} |
0 commit comments