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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
|
{
"metadata": {
"name": "",
"signature": "sha256:cc6cd32b3760ef9f8d727f2cbad62d4127abffa00d3c9ad693debfc7914ad190"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Chapter 6: Applications with Strings and Text"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6.1, page no. 221"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"print \"The character \\\\0 is used to terminate a string.\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The character \\0 is used to terminate a string.\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6.2, page no. 223"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"str1 = \"To be or not to be\"\n",
"str2 = \",that is a question\"\n",
"\n",
"print \"Length of the string \\\"\", str1, \"\\\" is \", len(str1), \" characters\"\n",
"print \"Length of the string \\\"\", str2, \"\\\" is \", len(str2), \" characters\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Length of the string \" To be or not to be \" is 18 characters\n",
"Length of the string \" ,that is a question \" is 19 characters\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6.3, page no. 225"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"str1 = \"Computers do what you tell them to do, not what you want them to do.\"\n",
"str2 = \"When you put something in memory, remember where you put it.\"\n",
"str3 = \"Never test for a condition you don't know what to do with.\"\n",
"\n",
"print \"There are 3 strings.\"\n",
"print \"The string: \"\n",
"print str1\n",
"print \"contains \", len(str1), \" characters\"\n",
"print \"The string: \"\n",
"print str2\n",
"print \"contains \", len(str3), \" characters\"\n",
"print \"The string: \"\n",
"print str3\n",
"print \"contains \", len(str3), \" characters\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"There are 3 strings.\n",
"The string: \n",
"Computers do what you tell them to do, not what you want them to do.\n",
"contains 68 characters\n",
"The string: \n",
"When you put something in memory, remember where you put it.\n",
"contains 58 characters\n",
"The string: \n",
"Never test for a condition you don't know what to do with.\n",
"contains 58 characters\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6.4, page no. 230"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"preamble = \"The joke is: \"\n",
"str1 = \"My dog hasn\\'t got any nose.\\n\"\n",
"str2 = \"How does your dog smell then?\\n\"\n",
"str3 = \"My dog smells horrible.\\n\"\n",
"joke = str1 + str2 + str3\n",
"\n",
"print preamble\n",
"print joke"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The joke is: \n",
"My dog hasn't got any nose.\n",
"How does your dog smell then?\n",
"My dog smells horrible.\n",
"\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6.5, page no. 234"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"print \"Type in the first word (maximum 20 characters): \",\n",
"word1 = raw_input()\n",
"print \"Type in the second word (maximum 20 characters): \",\n",
"word2 = raw_input()\n",
"\n",
"if(word1 == word2):\n",
" print \"You have entered identical words\"\n",
"else:\n",
" print \"%s precedes %s\\n\" %((word2 if(word1 > word2) else word1), (word1 if(word1 > word2) else word2)) "
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6.6, page no. 240 "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"str1 = \"This string contains the holy grail.\"\n",
"str2 = \"the holy grail\"\n",
"str3 = \"the holy grill\"\n",
" \n",
"if str2 in str1:\n",
" print \"\\\"%s\\\" was found in \\\"%s\\\"\\n\" %(str2, str1)\n",
"else:\n",
" print \"\\n\\\"%s\\\" was not found.\" %str2\n",
" \n",
"if not(str3 in str1):\n",
" print \"\\\"%s\\\" was not found.\\n\" % str3\n",
"else:\n",
" print \"\\nWe shouldn't get to here!\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\"the holy grail\" was found in \"This string contains the holy grail.\"\n",
"\n",
"\"the holy grill\" was not found.\n",
"\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6.7, page no. 243"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"print \"Enter some prose that is less than 1000 characters (go on typing hit enter to terminate):\"\n",
"str1 = raw_input()\n",
"\n",
"if len(str1) > 1000:\n",
" print \"Maximum permitted input length exceeded.\"\n",
"\n",
"list_str1 = str1.split(\" \")\n",
"\n",
"print \"The words in the prose that you entered are: \"\n",
"\n",
"for i in range(0, len(list_str1)):\n",
" print list_str1[i], \"\\t\",\n",
" if i % 5 == 0:\n",
" print \"\\n\"\n",
"\n",
"print \"Words found \", len(list_str1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter some prose that is less than 1000 characters (go on typing hit enter to terminate):\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"y father's family name being Pirrip, and my Christian name Philip, my infant tongue could make of both names nothing longer or more explicit than Pip. So, I called myself Pip, and came to be called Pip.\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The words in the prose that you entered are: \n",
"y \t\n",
"\n",
"father's \tfamily \tname \tbeing \tPirrip, \t\n",
"\n",
"and \tmy \tChristian \tname \tPhilip, \t\n",
"\n",
"my \tinfant \ttongue \tcould \tmake \t\n",
"\n",
"of \tboth \tnames \tnothing \tlonger \t\n",
"\n",
"or \tmore \texplicit \tthan \tPip. \t\n",
"\n",
"So, \tI \tcalled \tmyself \tPip, \t\n",
"\n",
"and \tcame \tto \tbe \tcalled \t\n",
"\n",
"Pip. \tWords found 37\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6.8, page no. 249"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"\n",
"nLetters = 0\n",
"nDigits = 0\n",
"nPunct = 0\n",
"print \"Enter an interesting string of less than 100 characters: \"\n",
"str1 = raw_input()\n",
"\n",
"for char in str1:\n",
" if char.isalpha():\n",
" nLetters += 1\n",
" elif char.isdigit():\n",
" nDigits += 1\n",
" elif char == \" \":\n",
" pass\n",
" else:\n",
" nPunct += 1\n",
"\n",
"print \"Your string contained %d letters, %d digits and %d punctuation characters. \" %(nLetters, nDigits, nPunct)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter an interesting string of less than 100 characters: \n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"I was born on the 3rd of October 1895, which is long ago.\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Your string contained 38 letters, 5 digits and 2 punctuation characters. \n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6.9, page no. 251"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"print \"Enter the string to be searched (less than 100 characters): \",\n",
"text = raw_input()\n",
" \n",
"print \"Enter the string sought (less than 40 characters):\",\n",
"substring = raw_input()\n",
" \n",
"print \"First string entered: \", text\n",
"print \"Second string entered: \", substring\n",
" \n",
"text = text.upper()\n",
"substring = substring.upper() \n",
"print \"The second string %s found in the first. \" %(\"was not\" if not substring in text else \"was\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter the string to be searched (less than 100 characters): "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Cry havoc, and let slip the dogs of war.\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" Enter the string sought (less than 40 characters):"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"The Dogs of War\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" First string entered: Cry havoc, and let slip the dogs of war.\n",
"Second string entered: The Dogs of War\n",
"The second string was found in the first. \n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 6.10, page no. 259"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"import numpy\n",
"\n",
"print \"Enter text (hit enter to exit): \",\n",
"text = raw_input()\n",
"\n",
"text_split = text.split()\n",
"distinct_words = []\n",
"\n",
"for i in text_split:\n",
" if i not in distinct_words:\n",
" distinct_words.append(i)\n",
"\n",
"word_count = numpy.zeros(len(distinct_words))\n",
"\n",
"for i in range(len(distinct_words)):\n",
" for j in range(len(text_split)):\n",
" if distinct_words[i] == text_split[j]:\n",
" word_count[i] += 1\n",
"\n",
"for i in range(len(distinct_words)):\n",
" print distinct_words[i], int(word_count[i])\n",
" if i == 5:\n",
" print \"\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter text (hit enter to exit): "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"When I makes tea I makes tea, as old mother Grogan said. And when I makes water I makes water. Begob, ma'am, says Mrs Cahill, God send you don't make them in the same pot.\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" When 1\n",
"I 4\n",
"makes 4\n",
"tea 1\n",
"tea, 1\n",
"as 1\n",
"\n",
"old 1\n",
"mother 1\n",
"Grogan 1\n",
"said. 1\n",
"And 1\n",
"when 1\n",
"water 1\n",
"water. 1\n",
"Begob, 1\n",
"ma'am, 1\n",
"says 1\n",
"Mrs 1\n",
"Cahill, 1\n",
"God 1\n",
"send 1\n",
"you 1\n",
"don't 1\n",
"make 1\n",
"them 1\n",
"in 1\n",
"the 1\n",
"same 1\n",
"pot. 1\n"
]
}
],
"prompt_number": 21
}
],
"metadata": {}
}
]
}
|