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
|
{
"metadata": {
"name": "",
"signature": "sha256:bfcc6bf3e86f2ff3b103be284b1b027f6faa9020241e866b9b4398fd89f8923c"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Chapter 12, Strings and String Functions"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 12-1, Page Number: 331"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# The control string is just a string\n",
"\n",
"format = \"%s\"\n",
"message = \"hello\"\n",
"\n",
"print format % message"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"hello\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 12-2, Page Number: 334"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# The control string is just a string - part 2\n",
"\n",
"format = \"%.15s\"\n",
"message = \"hellohellohello\"\n",
"\n",
"# Read in the number of characters to be displayed from \"message\"\n",
"print \"How many characters do you want displayed?\",\n",
"size = 5\n",
"print size\n",
"\n",
"# Convert size to 2-digit number in format[2] and format[3]\n",
"\n",
"format = \"%.\" + str(size / 10) + str(size % 10) + \"s\"\n",
"print \"The format is %s\"%format\n",
"\n",
"# Print the string\n",
"print format % message"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"How many characters do you want displayed? 5\n",
"The format is %.05s\n",
"hello\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 12-3, Page Number 336"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# sprintf function \n",
"\n",
"# There is no sprintf functionality in python\n",
"# However an alternate way to solve the problem is...\n",
"\n",
"print \"Please enter a number:\",\n",
"number = 17\n",
"print number\n",
"\n",
"string = \"The number is %d\"%number\n",
"\n",
"print \"%s\"%string\n",
"\n",
"# OR \n",
"\n",
"print string"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Please enter a number: 17\n",
"The number is 17\n",
"The number is 17\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 12-4, Page Number 337"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Variable control string using the sprintf function\n",
"\n",
"message = \"hellohellohello\"\n",
"\n",
"# Read in the number of characters to be displayed from message \n",
"print \"How many characters do you want displayed?\",\n",
"size = 7\n",
"print size\n",
"\n",
"# Create format in \"format\" using \"size\"\n",
"format = \"%%.%ds\\n\"%size\n",
"print \"The format is: %s\"%format\n",
"\n",
"# Print the string\n",
"print format % message"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"How many characters do you want displayed? 7\n",
"The format is: %.7s\n",
"\n",
"hellohe\n",
"\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 12-5, Page Number 338"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# A terse string copy function\n",
"\n",
"def strcpy(copy, original):\n",
"\t\"\"\" copies one string into another \"\"\"\n",
"\tcopy = original\n",
"\treturn copy\n",
"\n",
"string1 = \"\"\n",
"string1 = strcpy(string1, \"This is a message\")\n",
"print \"String 1 = %s\"%string1\n",
"\n",
"string2 = \"\"\n",
"string2 = strcpy(string2, string1)\n",
"print \"String 2 = %s\"%string2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"String 1 = This is a message\n",
"String 2 = This is a message\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 12-7, Page Number 344"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"string_point = \"hello\"\n",
"\n",
"string_var1 = string_point\n",
"\n",
"print \"%s\\n%s\"%(string_point,string_var1)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"hello\n",
"hello\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 12-8, Page Number 346"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# strcat function in python\n",
"# simple addition of strings results in concatenation in python\n",
"\n",
"def strcat(string1, string2) :\n",
"\t\"\"\" concatenates the string and returns the concatenated string \"\"\"\n",
"\tstring2 = string1 + string2\n",
"\treturn string2\n",
"\n",
"string1 = \"hello\"\n",
"string2 = strcat(string1,\" there\")\n",
"print \"string1 = %s\\nstring2 = %s\"%(string1,string2)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"string1 = hello\n",
"string2 = hello there\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 12-9, Page Number 348"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# strcat version 2\n",
"\n",
"def strcat(string1, string2) :\n",
"\tstring1 = string1 + string2\n",
"\tstring2 = string1\n",
"\treturn string1,string2\n",
"\n",
"string1 = \"hello\"\n",
"string1,string2 = strcat(string1, \" there\")\n",
"\n",
"print \"string1 = %s\\nstring2 = %s\"%(string1,string2)\n",
"\n",
"if string1 == string2 :\n",
"\tprint \"string2 points to string1\"\n",
"else :\n",
"\tprint \"string2 does not point to string1\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"string1 = hello there\n",
"string2 = hello there\n",
"string2 points to string1\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 12-10, Page Number 351"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Alphabetizing program using dynamically allocated arrays\n",
"\n",
"# Macro definition\n",
"MAX_ARRAY_SIZE = 20\t\t# The array may have up to 20 elements\n",
"MAX_STRING_SIZE = 10 \t# Each string may have up to 10 characters\n",
"# Message flags for \"print_array\"\n",
"ORIGINAL = 0\n",
"SORTED = 1\n",
"\n",
"words = [ \"cat\", \"pig\", \"dog\", \"earlobe\", \"bingo\", \"elbow\", \"likely\", \"radar\" ]\n",
"array_size = 0\n",
"\n",
"def get_size(size) :\n",
"\t\"\"\" Read the size of the array into size (until a value is entered) \"\"\"\n",
"\tprint \"How many words? \",\n",
"\tsize = 8\n",
"\tprint size\n",
"\twhile size < 1 or size > MAX_ARRAY_SIZE :\n",
"\t\tprint \"How many words? \",\n",
"\t\tsize = 8\n",
"\t\tprint size\n",
"\tglobal array_size\n",
"\tarray_size = size\n",
"\n",
"def get_array(array,size) :\n",
"\t\"\"\" Read in the string array called 'array' of size 'size' \"\"\"\n",
"\tprint \"Please enter the words:\"\n",
"\tfor i in array :\n",
"\t\tprint i\n",
"\t\n",
"def print_array(array,size,message) :\n",
"\t\"\"\" Print out string array 'array' of size 'size'. 'message' is a flag indicating which message to display(ORIGINAL or SORTED) \"\"\"\n",
"\tprintout = [ \"\\nThe original array is:\", \"\\nThe sorted array is:\", \"\\n\" ]\n",
"\t# Print the appropriate message\n",
"\tprint \"%s\"%printout[message]\n",
"\n",
"\tfor i in array :\n",
"\t\tprint i\n",
"\n",
"def pointer_sort(array,size) :\n",
"\t\"\"\" sort string array \"\"\"\n",
"\tfor i in range (size-1) :\n",
"\t\tfor j in range (i + 1, size) :\n",
"\t\t\tif array[i] > array[j] :\n",
"\t\t\t\tswap(i,j)\n",
"\n",
"def swap(i, j) :\n",
"\t\"\"\" Exchange strings \"\"\"\n",
"\tglobal words\n",
"\twords[i],words[j] = words[j],words[i]\n",
"\n",
"get_size(array_size)\t\t# Read in the array size\n",
"get_array(words, array_size)\t# Read in the string array \"words\" of size \"array_size\"\n",
"print_array(words, array_size, ORIGINAL)\t# Echo the original array\n",
"pointer_sort(words, array_size)\t\n",
"print_array(words, array_size, SORTED) # Print the sorted array"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"How many words? 8\n",
"Please enter the words:\n",
"cat\n",
"pig\n",
"dog\n",
"earlobe\n",
"bingo\n",
"elbow\n",
"likely\n",
"radar\n",
"\n",
"The original array is:\n",
"cat\n",
"pig\n",
"dog\n",
"earlobe\n",
"bingo\n",
"elbow\n",
"likely\n",
"radar\n",
"\n",
"The sorted array is:\n",
"bingo\n",
"cat\n",
"dog\n",
"earlobe\n",
"elbow\n",
"likely\n",
"pig\n",
"radar\n"
]
}
],
"prompt_number": 10
}
],
"metadata": {}
}
]
}
|