summaryrefslogtreecommitdiff
path: root/Beginning_C_By_Ivon_Horton/chapter14.ipynb
blob: 42a4f7a782f606e3ee1de2fcb741330cc73bbf75 (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
{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 14: Advanced and Specialized Topics"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 14.1, page no. 592"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\"\"\"\n",
      "Classifying wide characters\n",
      "\"\"\"\n",
      "\n",
      "import string\n",
      "import sys\n",
      "\n",
      "print \"Enter a character: \",\n",
      "ch = raw_input()\n",
      "\n",
      "if ch.isalnum():\n",
      "    if ch.isdigit():\n",
      "        print \"You entered the digit \", ch\n",
      "    elif ch.islower():\n",
      "        print \"You entered a lower case \", ch.upper()\n",
      "    elif ch.isupper():\n",
      "        print \"You entered a upper case \", ch.lower()\n",
      "    elif ch in string.punctuation:\n",
      "        print \"You entered a punctuation character \", ch\n",
      "    else:\n",
      "        print \"You entered \", ch, \"but I don't know what it is!\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter a character: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "g\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " You entered a lower case  G\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 14.2, page no. 596"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\"\"\"\n",
      "Finding occurrences of one wide character string in another\n",
      "\"\"\"\n",
      "\n",
      "print \"Enter a string of less than 100 characters: \",\n",
      "text = raw_input()\n",
      "\n",
      "print \"Enter the string sought: \",\n",
      "sought = raw_input()\n",
      "sought = sought.lower()\n",
      "count = 0\n",
      "\n",
      "sought_len = len(sought)\n",
      "\n",
      "for i in range(len(text)):\n",
      "    substr = text[i:sought_len+i]\n",
      "    if substr == sought:\n",
      "        count += 1\n",
      "\n",
      "print \"First string entered: \", text\n",
      "print \"Second string entered: \", sought\n",
      "print \"The second string was found in first %d times\" %count\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter a string of less than 100 characters: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Smith, where Jones had had, \"had\", had had \"had had\". Enter the string sought (less than 40 characters): Had\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Enter the string sought: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "had\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " First string entered:  Smith, where Jones had had, \"had\", had had \"had had\". Enter the string sought (less than 40 characters): Had\n",
        "Second string entered:  had\n",
        "The second string was found in first 7 times\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 14.3, page no. 606"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\"\"\"\n",
      "Working with complex numbers\n",
      "\"\"\"\n",
      "\n",
      "cx = 1.0 + 3.0j\n",
      "cy = 1.0 - 4.0j\n",
      "\n",
      "sum = cx + cy\n",
      "diff = cx - cy\n",
      "product = cx * cy\n",
      "quotient = cy/cy\n",
      "conjugate = cx.conjugate()\n",
      "\n",
      "print \"Complex numbers are supported\"\n",
      "print \"Working with complex numbers: \"\n",
      "print \"Starting values: cx = %.2f%+.2fi cy = %.2f%+.2fi\" %(cx.real, cx.imag, cy.real, cy.imag)\n",
      "print \"The sum cx + cy = %.2f%+.2fi\" %(sum.real, sum.imag)\n",
      "print \"The difference cx - cy = %.2f%+.2fi\" %(diff.real, diff.imag)\n",
      "print \"The product cx * cy = %.2f%+.2fi\" %(product.real, product.imag)\n",
      "print \"The quotient cx / cy = %.2f%+.2fi\" %(quotient.real, quotient.imag)\n",
      "print \"The conjugate of cx = %.2f%+.2fi\" %(conjugate.real, conjugate.imag)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Complex numbers are supported\n",
        "Working with complex numbers: \n",
        "Starting values: cx = 1.00+3.00i cy = 1.00-4.00i\n",
        "The sum cx + cy = 2.00-1.00i\n",
        "The difference cx - cy = 0.00+7.00i\n",
        "The product cx * cy = 13.00-1.00i\n",
        "The quotient cx / cy = 1.00-0.00i\n",
        "The conjugate of cx = 1.00-3.00i\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 14.4, page no. 610"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\"\"\"\n",
      "Joining threads\n",
      "\"\"\"\n",
      "\n",
      "from threading import Thread\n",
      "\n",
      "def get_data(pdata):\n",
      "    pd = pdata\n",
      "    print \"The get_data thread received: data.a=%d and data.b=%d\" %(pd[0], pd[1])\n",
      "    pd[0] *= 3\n",
      "    print \"The get_data thread makes it: data.a=%d and data.b=%d \" %(pd[0], pd[1])\n",
      " \n",
      "def process_data(pdata):\n",
      "    pd = pdata\n",
      "    print \"The process_data thread received: data.a=%d and data.b=%d \"%(pd[0], pd[1])\n",
      " \n",
      "mydata = [123, 345]\n",
      "print \"Before starting the get_data thread: mydata.a=%d and mydata. b=%d\" %(mydata[0], mydata[1])\n",
      "print \"get_data thread started.\"\n",
      "t1 = Thread(target=get_data, args=(mydata,))\n",
      "t1.start()\n",
      "Thread.join(t1)\n",
      "print \"process_data thread started.\"\n",
      "t2 = Thread(target=process_data, args=(mydata,))\n",
      "t2.start()\n",
      "Thread.join(t2)\n",
      "print \"After both threads finish executing: mydata.a=%d and mydata. b=%d\" %(mydata[0], mydata[1])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Before starting the get_data thread: mydata.a=123 and mydata. b=345\n",
        "get_data thread started.\n",
        "The get_data thread received: data.a=123 and data.b=345\n",
        "The get_data thread makes it: data.a=369 and data.b=345 \n",
        "process_data thread started.\n",
        "The process_data thread received: data.a=369 and data.b=345 \n",
        "After both threads finish executing: mydata.a=369 and mydata. b=345\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 14.5, page no. 616"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\"\"\"\n",
      "Thread operations\n",
      "\"\"\"\n",
      "\n",
      "from threading import Thread\n",
      "import math\n",
      "import time\n",
      "\n",
      "thread_count = 5\n",
      "\n",
      "thread_id = []\n",
      "task = 0\n",
      "\n",
      "def execute_task():\n",
      "    global task\n",
      "    task += 1\n",
      "    print \"Task %d started.\" %task\n",
      "    time.sleep(1)\n",
      "    for i in range(1000000):\n",
      "        x = math.sqrt(3.1415926)\n",
      "    print \"Task %d finished\" %task\n",
      " \n",
      "\n",
      "for i in range(thread_count):\n",
      "    thread_id.append(Thread(target=execute_task))\n",
      "    thread_id[i].start()\n",
      "    \n",
      "for j in range(thread_count):\n",
      "    Thread.join(thread_id[j])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Task 1 started.\n",
        "Task 2 started.\n",
        "Task 3 started.\n",
        "Task 4 started.\n",
        "Task 5 started.\n",
        "Task 5 finished\n",
        "Task 5 finished\n",
        "Task 5 finished\n",
        "Task 5 finished\n",
        "Task 5 finished\n"
       ]
      }
     ],
     "prompt_number": 9
    }
   ],
   "metadata": {}
  }
 ]
}