summaryrefslogtreecommitdiff
path: root/Programming_With_Java_A_Primer/chapter18.ipynb
blob: 3760aefe07edcb1e96b7b8b43efc32cb97ac8090 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
{
 "metadata": {
  "name": "",
  "signature": "sha256:b65111374697402ea20166f271bb1488f1e63cc13c5da33961c35513244b0d0c"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 18: Java Collections"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 18.1, page no. 348"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\"\"\"\n",
      "Note: there is no such class in Python. We will perform normal operations on array\n",
      "\"\"\"\n",
      "\n",
      "arrayList = []\n",
      "print \"Initial size of arrayList: \", len(arrayList)\n",
      "arrayList.append(\"A\")\n",
      "arrayList.append(\"B\")\n",
      "arrayList.append(\"C\")\n",
      "arrayList.append(\"D\")\n",
      "arrayList.insert(2, \"E\")\n",
      "print \"Changed contents of arraylist by adding element at the given index: \", arrayList\n",
      "arrayList.pop(3)\n",
      "arrayList.remove(\"A\")\n",
      "print \"Changed contents of arraylist by removing element from the list: \", arrayList"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Initial size of arrayList:  0\n",
        "Changed contents of arraylist by adding element at the given index:  ['A', 'B', 'E', 'C', 'D']\n",
        "Changed contents of arraylist by removing element from the list:  ['B', 'E', 'D']\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 18.2, page no. 349"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\"\"\"\n",
      "Note: There is no such collection in Python. We will dqueue instead, which is very much similar\n",
      "\"\"\"\n",
      "\n",
      "from collections import deque\n",
      "\n",
      "d = deque()\n",
      "\n",
      "class MyStack():\n",
      "    def push1(self, o):\n",
      "        d.appendleft(o)\n",
      "    def push2(self, o):\n",
      "        d.append(o)\n",
      "    def bottom(self):\n",
      "        return d.pop()\n",
      "    def pop(self):\n",
      "        return d.popleft()\n",
      "\n",
      "class Car():\n",
      "    car1 = \"Benz\"\n",
      "    car2 = \"Toyoto\"\n",
      "    car3 = \"Qualis\"\n",
      "    car4 = \"Santro\"\n",
      "\n",
      "class Bird():\n",
      "    bird1 = \"parrot\"\n",
      "    bird2 = \"duck\"\n",
      "    bird3 = \"raven\"\n",
      "\n",
      "myCar = Car()\n",
      "myBird = Bird()\n",
      "s = MyStack()\n",
      "s.push1(myCar.car1)\n",
      "s.push2(myBird.bird3)\n",
      "myCar = s.pop()\n",
      "print \"The first element in the list: \", myCar\n",
      "print \"The last element in the list: \", myBird.bird3"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The first element in the list:  Benz\n",
        "The last element in the list:  raven\n"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 18.3, page no. 351"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\"\"\"\n",
      "Note: there is no collection hash set in Python. We will solve it in different manner\n",
      "\"\"\"\n",
      "\n",
      "hs = set()\n",
      "\n",
      "hs.add(\"D\")\n",
      "hs.add(\"A\")\n",
      "hs.add(\"C\")\n",
      "hs.add(\"B\")\n",
      "hs.add(\"E\")\n",
      "\n",
      "#answer will differ due to difference in built-in techniques\n",
      "print \"The elements available in the hash set are: \", hs"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The elements available in the hash set are:  set(['A', 'C', 'B', 'E', 'D'])\n"
       ]
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#There is no TreeSet or similar collection in Python (can be done using normal list like 18.1) so skipping example 18.4"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 25
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 18.5, page no. 353"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\"\"\"\n",
      "Note: there is no vector in Python. Doing using normal lists.\n",
      "\"\"\"\n",
      "\n",
      "fruits = []\n",
      "\n",
      "fruits.append(\"Apple\")\n",
      "fruits.append(\"Orange\")\n",
      "fruits.append(\"Grapes\")\n",
      "fruits.append(\"Pine\")\n",
      "\n",
      "for ele in fruits:\n",
      "    print ele"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Apple\n",
        "Orange\n",
        "Grapes\n",
        "Pine\n"
       ]
      }
     ],
     "prompt_number": 26
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 18.6, page no. 354"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\"\"\"\n",
      "In python we can use list itself as stack\n",
      "\"\"\"\n",
      "\n",
      "st = []\n",
      "\n",
      "st.append(\"Java\")\n",
      "st.append(\"latest\")\n",
      "st.append(\"Edition\")\n",
      "st.append(\"-fifth\")\n",
      "\n",
      "print \"The elements in the Stack: \", st\n",
      "print \"The element at the top: \", st[-1:]\n",
      "print \"The element poped out of the stack: \", st.pop()\n",
      "print \"The element in a stack after pop out element: \", st\n",
      "try:\n",
      "    \"The result of searching: \", st.index(\"r u\")\n",
      "except ValueError:\n",
      "    print \"The result of searching: \", -1"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The elements in the Stack:  ['Java', 'latest', 'Edition', '-fifth']\n",
        "The element at the top:  ['-fifth']\n",
        "The element poped out of the stack:  -fifth\n",
        "The element in a stack after pop out element:  ['Java', 'latest', 'Edition']\n",
        "The result of searching:  -1\n"
       ]
      }
     ],
     "prompt_number": 29
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 18.7, page no. 355"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\"\"\"\n",
      "Note: we will use dictionaries\n",
      "\"\"\"\n",
      "\n",
      "ht ={}\n",
      "\n",
      "ht['item1'] = \"Apple\"\n",
      "ht['item2'] = \"Orange\"\n",
      "ht['item3'] = \"Grapes\"\n",
      "ht['item4'] = \"Pine\"\n",
      "\n",
      "for key in ht.iterkeys():\n",
      "    print ht[key]\n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Orange\n",
        "Grapes\n",
        "Apple\n",
        "Pine\n"
       ]
      }
     ],
     "prompt_number": 38
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 18.8, page no. 357"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#Answers will differ due to difference in built-in techniques of the classes\n",
      "\n",
      "from random import shuffle\n",
      "\n",
      "l = []\n",
      "\n",
      "l.append(\"java\")\n",
      "l.append(\"is\")\n",
      "l.append(\"platform\")\n",
      "l.append(\"independent\")\n",
      "\n",
      "l_r = list(reversed(l))\n",
      "l_r = sorted(l_r)\n",
      "\n",
      "print \"List sorted in reverse order is: \"\n",
      "for ele in l_r:\n",
      "    print ele,\n",
      "\n",
      "print \"\\nList shuffled: \"\n",
      "shuffle(l_r)\n",
      "for ele in l_r:\n",
      "    print ele,\n",
      "\n",
      "print \"\\nMinimum: \", min(l_r)\n",
      "print \"Maximum: \", max(l_r)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "List sorted in reverse order is: \n",
        "independent is java platform \n",
        "List shuffled: \n",
        "java independent is platform \n",
        "Minimum:  independent\n",
        "Maximum:  platform\n"
       ]
      }
     ],
     "prompt_number": 2
    }
   ],
   "metadata": {}
  }
 ]
}