summaryrefslogtreecommitdiff
path: root/Computer_Programming_Theory_and_Practice_/Chapter7.ipynb
blob: 0d79c17f24cc6b73e783ac23e4080795d56227d6 (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
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
554
555
556
557
{
 "metadata": {
  "name": "",
  "signature": "sha256:37ae6cc76fe6f4a8cc8dba62582a6a103f99da5c7579d1a782cb95241f542fd8"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 1 , Page number: CP-148"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Program to count the occurence of a particular in a string\n",
      "# Variable declaration\n",
      "\n",
      "st = \"MISSISSIPPI\"\n",
      "ch = \"S\"\n",
      "count = 0\n",
      "\n",
      "print \"Enter the string :\",st\n",
      "print \"Which character to be counted ?\",ch\n",
      "\n",
      "# Loop to check occurrence of aa character\n",
      "\n",
      "l = len(st)\n",
      "\n",
      "for i in st:\n",
      "    if i == ch:\n",
      "        count = count + 1\n",
      "\n",
      "print \"The character \" + ch + \" occurs %d times\" % count        "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the string : MISSISSIPPI\n",
        "Which character to be counted ? S\n",
        "The character S occurs 4 times\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 2 , Page number: CP-149"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Program to count the number of vowels in a sentence\n",
      "# Variable declaration\n",
      "\n",
      "st = \"This is a book\"\n",
      "count = 0\n",
      "\n",
      "print \"Enter the sentence :\"\n",
      "print st\n",
      "\n",
      "# Loop to count the vowels in the string\n",
      "\n",
      "for i in st:\n",
      "    if i == \"A\":\n",
      "        count = count + 1\n",
      "    elif i == \"E\":\n",
      "        count = count + 1\n",
      "    elif i == \"I\":\n",
      "        count = count + 1\n",
      "    elif i == \"O\":\n",
      "        count = count + 1\n",
      "    elif i == \"U\":\n",
      "        count = count + 1\n",
      "    elif i == \"a\":\n",
      "        count = count + 1\n",
      "    elif i == \"e\":\n",
      "        count = count + 1\n",
      "    elif i == \"i\":\n",
      "        count = count + 1\n",
      "    elif i == \"o\":\n",
      "        count = count + 1\n",
      "    elif i == \"u\":\n",
      "        count = count + 1\n",
      "\n",
      "print \"%d vowels are present in the sentence\" % count\n",
      "        \n",
      "        "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the sentence :\n",
        "This is a book\n",
        "5 vowels are present in the sentence\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 3 , Page number: CP-150"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Program to test whether a given string is a palindrome or not\n",
      "# Variable declaration\n",
      "\n",
      "st = \"HYDERABAD\"\n",
      "rst = \"\"\n",
      "i = 0\n",
      "j = len(st) - 1\n",
      "\n",
      "print \"Enter the string :\",st\n",
      "\n",
      "# Palindrome or not \n",
      "\n",
      "rst = reversed(st)\n",
      "\n",
      "if list(st) == list(rst):\n",
      "    print \"%s is a palindrome string \" % st\n",
      "else:\n",
      "    print \"%s is not a palindrome string \" % st"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the string : HYDERABAD\n",
        "HYDERABAD is not a palindrome string \n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4 , Page number: CP-152"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Program to conctenate two strings\n",
      "# Variable declaration\n",
      "\n",
      "st1 = \"NEW \"\n",
      "st2 = \"DELHI\"\n",
      "\n",
      "# input of two strings to be concatenated\n",
      "\n",
      "print \"Enter first string :\",st1\n",
      "print \"Enter second string :\",st2\n",
      "\n",
      "# concatenation of two strings\n",
      "st = st1 + st2\n",
      "\n",
      "print \"Resultant string is \",st    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter first string : NEW \n",
        "Enter second string : DELHI\n",
        "Resultant string is  NEW DELHI\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 5 , Page number: CP-154"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Program to compare two strings\n",
      "# Variable declaration\n",
      "\n",
      "st1 = \"ALPHA\"\n",
      "st2 = \"BETA\"\n",
      "\n",
      "print \"Enter string 1:\",st1\n",
      "print \"Enter string 2:\",st2\n",
      "\n",
      "# compare strings\n",
      "\n",
      "if (cmp(st1,st2)>0):\n",
      "    print \"%s \" + st1 + \"is alphabetically greater string\"\n",
      "else:\n",
      "    print st2 + \" is alphabetically greater string\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter string 1: ALPHA\n",
        "Enter string 2: BETA\n",
        "BETA is alphabetically greater string\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6 , Page number: CP-155"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Program to sort an array of names in alphabetical order\n",
      "# Variable declaration\n",
      "\n",
      "n = 4\n",
      "names = [\"DEEPAK\",\"SHERIN\",\"SONIKA\",\"ARUN\"]\n",
      "\n",
      "print \"How many names ?\",n\n",
      "print \"Enter the 4 names one by one\"\n",
      "for i in names:\n",
      "    print i\n",
      "\n",
      "# Loop to arrange names in alphabetical order\n",
      "\n",
      "for i in range(0,n-1):\n",
      "    for j in range(i+1,n):\n",
      "        if cmp(names[i],names[j])>0:\n",
      "            \n",
      "            temp = names[i]\n",
      "            names[i] = names[j]\n",
      "            names[j] = temp\n",
      "\n",
      "print \"Names in alphabetical order\"\n",
      "for i in names:\n",
      "    print i"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "How many names ? 4\n",
        "Enter the 4 names one by one\n",
        "DEEPAK\n",
        "SHERIN\n",
        "SONIKA\n",
        "ARUN\n",
        "Names in alphabetical order\n",
        "ARUN\n",
        "DEEPAK\n",
        "SHERIN\n",
        "SONIKA\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 7 , Page number: CP-157"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Program to convert a line from lower case to upper case\n",
      "# Variable declaretion\n",
      "\n",
      "import sys\n",
      "\n",
      "st = ['l','o','g','i','c','a','l',' ','t','h','i','n','k','i','n','g',' ','i','s',' ','a',' ','m','u','s','t',' ','t','o',' ','l','e','a','r','n',' ','p','r','o','g','r','a','m','m','i','n','g']\n",
      "\n",
      "print \"Enter a sentence :\"\n",
      "for i in st:\n",
      "    print i,\n",
      "print     \n",
      "print \"The converted upper case string is\"\n",
      "# loop to convert lower case alphabet to upper case text\n",
      "for i in range(len(st)):\n",
      "    if st[i] >= 'a' and st[i] <= 'z':\n",
      "        st[i] = chr(ord(st[i])-32)\n",
      "\n",
      "\n",
      "    sys.stdout.write(st[i])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter a sentence :\n",
        "l o g i c a l   t h i n k i n g   i s   a   m u s t   t o   l e a r n   p r o g r a m m i n g\n",
        "The converted upper case string is\n",
        "LOGICAL THINKING IS A MUST TO LEARN PROGRAMMING"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9 , Page number: CP-160"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Program to read a text and to omit al occurrences of a particular word\n",
      "# Variable declartion\n",
      "\n",
      "st = \"TO ACCESS THE NAME OF THE CITY IN THE LIST\"\n",
      "i = 0\n",
      "omit = \"THE\"\n",
      "l = len(st)\n",
      "j = 0\n",
      "word = []\n",
      "newst = \"\"\n",
      "onesps = \"\"\n",
      "print \"Enter a sentence :\"\n",
      "print st\n",
      "print \"Enter word to omit :\",omit\n",
      "\n",
      "# loop to omit the given word\n",
      "\n",
      "for i in range(l):\n",
      "    ch = i\n",
      "    if ch == ' ':\n",
      "        for j in word:\n",
      "            j = \" \" \n",
      "            if j == omit:\n",
      "                newst = j\n",
      "                newst = onesps\n",
      "            j = \" \"\n",
      "            j = 0\n",
      "        else:\n",
      "            j = ch\n",
      "            j = j + 1\n",
      "    i = i + 1\n",
      "\n",
      "print \"After omiting the word \" + omit\n",
      "print newst\n",
      "print \"Press any key to continue\"\n",
      "     "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter a sentence :\n",
        "TO ACCESS THE NAME OF THE CITY IN THE LIST\n",
        "Enter word to omit : THE\n",
        "After omiting the word THE\n",
        "\n",
        "Press any key to continue\n"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10 , Page number: CP-161"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Program to calculate the amount to be paid for the telegram\n",
      "# Variable declaration\n",
      "\n",
      "count = 0\n",
      "st = \"Congratulations on your success in Examinations.\"\n",
      "l = len(st)\n",
      "\n",
      "print \"Type the sentence for Telegram\"\n",
      "print st\n",
      "\n",
      "# loop to count number of words\n",
      "\n",
      "for i in range(l):\n",
      "    if st[i] == '?':\n",
      "        count = count + 1\n",
      "\n",
      "if count <= 10:\n",
      "    amt = 5\n",
      "else:\n",
      "    amt = 5 + (count - 10) * 1.25\n",
      "\n",
      "print \"Amount to be paid for telegram = Rs.%0.2f\" % amt    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Type the sentence for Telegram\n",
        "Congratulations on your success in Examinations.\n",
        "Amount to be paid for telegram = Rs.5.00\n"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 11 , Page number: CP-163"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Program to count number of lines,words and characters\n",
      "# Variable declaration\n",
      "\n",
      "txt = \"What is a string? How do you initialize it? Explain with example.$\"\n",
      "st = \"\"\n",
      "i = 0\n",
      "lns = 0\n",
      "wds = 0\n",
      "chs = 0\n",
      "\n",
      "print \"Enter the text, type $ at end.\"\n",
      "print txt\n",
      "\n",
      "# loop to count lines,words and characters in text\n",
      "\n",
      "while txt[i] != '$':\n",
      "    # switch case statements\n",
      "    if txt[i] == ' ':\n",
      "        wds = wds + 1\n",
      "        chs = chs + 1\n",
      "    elif txt[i] == '.':\n",
      "        wds = wds + 1\n",
      "        lns = lns + 1\n",
      "        chs = chs + 1\n",
      "    elif txt[i] == '?':\n",
      "        lns = lns + 1\n",
      "    else: # default\n",
      "        chs = chs + 1\n",
      "\n",
      "    i = i + 1\n",
      "\n",
      "print \"Number of char (incl. blanks) =\",chs\n",
      "print \"Number of words =\",wds\n",
      "print \"Number of lines =\",lns\n",
      "\n",
      "\n",
      "        "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the text, type $ at end.\n",
        "What is a string? How do you initialize it? Explain with example.$\n",
        "Number of char (incl. blanks) = 63\n",
        "Number of words = 12\n",
        "Number of lines = 3\n"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}