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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
{
"metadata": {
"name": "",
"signature": "sha256:5c730697085cba47efcd5224cc57d70aca1160f9a5e192f67596eb53b4b5b4af"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Chapter 9: Arrays, Strings & Vectors"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"example 9.1, page no. 157"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"number = [55, 40, 80, 65, 71]\n",
"print \"Given List: \",\n",
"for num in number:\n",
" print num,\n",
"number = sorted(number)[: : -1]\n",
"print \"\\nSorted List: \",\n",
"for num in number:\n",
" print num,"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Given List: 55 40 80 65 71 \n",
"Sorted List: 80 71 65 55 40\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"example 9.2, page no. 160"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"rows = 20\n",
"columns = 20\n",
"product = [[0 for x in range(rows)] for x in xrange(columns)]\n",
"print \"Multiplication Table\"\n",
"for i in range(10, rows):\n",
" for j in range(10, columns):\n",
" product[i][j] = i*j\n",
" print \" \", product[i][j],\n",
" print \" \""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Multiplication Table\n",
" 100 110 120 130 140 150 160 170 180 190 \n",
" 110 121 132 143 154 165 176 187 198 209 \n",
" 120 132 144 156 168 180 192 204 216 228 \n",
" 130 143 156 169 182 195 208 221 234 247 \n",
" 140 154 168 182 196 210 224 238 252 266 \n",
" 150 165 180 195 210 225 240 255 270 285 \n",
" 160 176 192 208 224 240 256 272 288 304 \n",
" 170 187 204 221 238 255 272 289 306 323 \n",
" 180 198 216 234 252 270 288 306 324 342 \n",
" 190 209 228 247 266 285 304 323 342 361 \n"
]
}
],
"prompt_number": 17
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"example 9.3, page no. 163"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"name = [\"Madras\", \"Delhi\", \"Ahmedabad\", \"Calcutta\", \"Bombay\"]\n",
"name = sorted(name)\n",
"\n",
"for n in name:\n",
" print n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Ahmedabad\n",
"Bombay\n",
"Calcutta\n",
"Delhi\n",
"Madras\n"
]
}
],
"prompt_number": 18
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"example 9.4, page no. 164"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"aString = \"Object language\"\n",
"print \"Original String : \", aString\n",
"print \"Length of String: \", len(aString)\n",
"for i in range(len(aString)):\n",
" print \"Character at position: %d is %c\" %(i+1,aString[i])\n",
"\n",
"aString = aString.split(\" \")\n",
"aString.insert(1, \"-Oriented\") \n",
"aString = \" \".join(aString)\n",
"print \"Modified String: \", aString\n",
"aString = aString + \" improves security.\"\n",
"print \"Appended String: \", aString\n",
"\n",
"# Note : In python, string is immutable so we can not edit/update it."
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Original String : Object language\n",
"Length of String: 15\n",
"Character at position: 1 is O\n",
"Character at position: 2 is b\n",
"Character at position: 3 is j\n",
"Character at position: 4 is e\n",
"Character at position: 5 is c\n",
"Character at position: 6 is t\n",
"Character at position: 7 is \n",
"Character at position: 8 is l\n",
"Character at position: 9 is a\n",
"Character at position: 10 is n\n",
"Character at position: 11 is g\n",
"Character at position: 12 is u\n",
"Character at position: 13 is a\n",
"Character at position: 14 is g\n",
"Character at position: 15 is e\n",
"Modified String: Object -Oriented language\n",
"Appended String: Object -Oriented language improves security.\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"example 9.5, page no. 166"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#vector is an array only in Python & commandline arguments cannot be taken in IPython Notebook\n",
"\n",
"v1 = []\n",
"v1.append(\"Ada\")\n",
"v1.append(\"BASIC\")\n",
"v1.append(\"COBOL\")\n",
"v1.append(\"C++\")\n",
"v1.append(\"FORTRAN\")\n",
"v1.append(\"Python\")\n",
"\n",
"print \"List of Languages: \"\n",
"for ele in v1:\n",
" print ele"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"List of Languages: \n",
"Ada\n",
"BASIC\n",
"COBOL\n",
"C++\n",
"FORTRAN\n",
"Python\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"example 9.6, page no. 169"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\"\"\"\n",
"There is no wrapper class in Python.\n",
"\"\"\"\n",
"\n",
"principalAmount = float(raw_input(\"Enter Principal Amount: \"))\n",
"interestRate = float(raw_input(\"Enter Interest Rate: \"))\n",
"numYears = float(raw_input(\"Enter Number of Years: \"))\n",
"\n",
"def loan(p, r, n):\n",
" year = 1\n",
" my_sum = p\n",
" while year<=n:\n",
" my_sum = my_sum*(1+r)\n",
" year += 1\n",
" return my_sum\n",
"\n",
"def printline():\n",
" for i in range(1, 30):\n",
" print \"-\",\n",
" print \"\"\n",
"\n",
"value = loan(principalAmount, interestRate, numYears)\n",
"printline()\n",
"print \"Final Value: \", round(value,2)\n",
"printline()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Principal Amount: 5000\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Interest Rate: 0.15\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Number of Years: 4\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"- - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n",
"Final Value: 8745.03\n",
"- - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"example 9.7, page no. 195"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\"\"\"\n",
"There is no inbuilt module to use stack as it is done in the book \"java.util.stack\". We will use normal lists as stack in Python\n",
"\"\"\"\n",
"\n",
"stack = []\n",
"stack.append(10)\n",
"stack.append(20)\n",
"stksum1 = stack.pop()\n",
"stksum2 = stack.pop()\n",
"stksum = stksum1 + stksum2\n",
"print \"The top most element from the stack is: \", stksum2\n",
"print \"The next top most element from the stack is: \", stksum1\n",
"print \"The sum of two elements from the stack is: \", stksum"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The top most element from the stack is: 10\n",
"The next top most element from the stack is: 20\n",
"The sum of two elements from the stack is: 30\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"example 9.8, page no. 196"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\"\"\"\n",
"Enum is not supported by default in Python 2.X version. It has been backported to various versions of Python.\n",
"But, the package has to be installed seperately.\n",
"Using normal class here.\n",
"\"\"\"\n",
"\n",
"class WorkingDays:\n",
" Sunday = 0\n",
" Monday = 1\n",
" Tuesday = 2\n",
" Wednesday = 3\n",
" Thursday = 4\n",
" Friday = 5\n",
" Saturday = 6\n",
"\n",
"def weekend(d):\n",
" if d == WorkingDays.Sunday:\n",
" print \"Value = Sunday is a Holiday\"\n",
" elif d == WorkingDays.Monday:\n",
" print \"Value = Monday is a working day\"\n",
" elif d == WorkingDays.Tuesday:\n",
" print \"Value = Tuesday is a working day\"\n",
" elif d == WorkingDays.Wednesday:\n",
" print \"Value = Wednesday is a working day\"\n",
" elif d == WorkingDays.Thursday:\n",
" print \"Value = Thursday is a working day\" \n",
" elif d == WorkingDays.Friday:\n",
" print \"Value = Friday is a working day\"\n",
" else:\n",
" print \"Value = Saturday is a working day\"\n",
"\n",
"for i in range(WorkingDays.Sunday, WorkingDays.Saturday+1):\n",
" weekend(i)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Value = Sunday is a Holiday\n",
"Value = Monday is a working day\n",
"Value = Tuesday is a working day\n",
"Value = Wednesday is a working day\n",
"Value = Thursday is a working day\n",
"Value = Friday is a working day\n",
"Value = Saturday is a working day\n"
]
}
],
"prompt_number": 18
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"example 9.9, page no. 199"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\"\"\"\n",
"There is no concept of annotations in Python\n",
"\"\"\"\n",
"\n",
"class MySingle:\n",
" value = 0\n",
" def __init__(self, x):\n",
" self.value = 100\n",
" def display(self):\n",
" print \"The value is: \", self.value\n",
"\n",
"ob = MySingle(100)\n",
"try:\n",
" ob.display()\n",
"except:\n",
" print \"No such method found...\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The value is: 100\n"
]
}
],
"prompt_number": 50
}
],
"metadata": {}
}
]
}
|