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
|
{
"metadata": {
"name": "",
"signature": "sha256:a0a1c93dba0a6936203c3f01d39f2697247a7b54141760d78392371a2c5620f6"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Hour 18: Using Special Data Types and Functions"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 18.1, Page No 297"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# There is no proper concept of Enum(Enumaration) in python so i have implemented in this manner\n",
"def enum(*sequential, **named):\n",
" enums = dict(zip(sequential, range(len(sequential))), **named)\n",
" return type('Enum', (), enums)\n",
"\n",
"\n",
"language = enum(human=100,animal=50,computer=51)\n",
"\n",
"days = enum('SUN','MON','TUE','WED','THU','FRI','SAT')\n",
"\n",
"print \"human=%d, animal=%d, computer=%d\" % (language.human,language.animal,language.computer)\n",
"print \"SUN: %d\" % days.SUN\n",
"print \"MON: %d\" % days.MON\n",
"print \"TUE: %d\" % days.TUE\n",
"print \"WED: %d\" % days.WED\n",
"print \"THU: %d\" % days.THU\n",
"print \"FRI: %d\" % days.FRI\n",
"print \"SAT: %d\" % days.SAT"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"human=100, animal=50, computer=51\n",
"SUN: 0\n",
"MON: 1\n",
"TUE: 2\n",
"WED: 3\n",
"THU: 4\n",
"FRI: 5\n",
"SAT: 6\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 18.2, Page No 298"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# There is no proper concept of Enum(Enumaration) in python so i have implemented in this manner\n",
"def enum(*sequential, **named):\n",
" enums = dict(zip(sequential, range(len(sequential))), **named)\n",
" return type('Enum', (), enums)\n",
"\n",
"units= enum(penny=1,nickel=5,dime=10,quarter=25,dollar=100)\n",
"money_unit=[units.dollar,units.quarter,units.dime,units.nickel,units.penny]\n",
"unit_name=[\"dollar(s)\",\"quarter(s)\",\"dime(s)\",\"nickel(s)\",\"penny(s)\"]\n",
"\n",
"cent=int(raw_input(\"Enter a monetary value in cents:\"))\n",
"\n",
"print \"Which is equivalent to:\"\n",
"tmp=0\n",
"for i in range(5):\n",
" tmp=cent / money_unit[i]\n",
" cent = cent-(tmp*money_unit[i])\n",
" if(tmp):\n",
" print \"%d %s\" % (tmp,unit_name[i]),"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a monetary value in cents:141\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Which is equivalent to:\n",
"1 dollar(s) 1 quarter(s) 1 dime(s) 1 nickel(s) 1 penny(s)\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 18.3, Page No 301"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# There is no proper concept of Enum(Enumaration) in python so i have implemented in this manner\n",
"def enum(*sequential, **named):\n",
" enums = dict(zip(sequential, range(len(sequential))), **named)\n",
" return type('Enum', (), enums)\n",
"\n",
"constants=enum(ITEM_NUM=3,DELT=(ord('a')-ord('A')))\n",
"\n",
"def Convert2Upper(str1,str2):\n",
" str1=list(str1)\n",
" str2=list(i for i in range(len(str1)+1))\n",
" for i in range(len(str1)):\n",
" if ((ord(str1[i])>=97) and (ord(str1[i])<=122)):\n",
" str2[i]=chr(ord(str1[i])-constants.DELT)\n",
" else:\n",
" str2[i]=str1[i]\n",
" str2[i+1]='\\0'\n",
" return str2\n",
" \n",
"moon=[\"Whatever we wear\",\"we become beautiful\",\"moon viewing!\"]\n",
"str1=\"\"\n",
"term=0\n",
"str3=[\"\",\"\",\"\"]\n",
"\n",
"for i in range(constants.ITEM_NUM):\n",
" if(str1==None):\n",
" print \"malloc() failed.\"\n",
" term=1\n",
" i=constants.ITEM_NUM\n",
" str1=Convert2Upper(moon[i],str1)\n",
" print moon[i]\n",
" str3[i]=str1\n",
"print \"\"\n",
"for i in range(len(str3)):\n",
" print \"\".join(str3[i])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Whatever we wear\n",
"we become beautiful\n",
"moon viewing!\n",
"\n",
"WHATEVER WE WEAR\u0000\n",
"WE BECOME BEAUTIFUL\u0000\n",
"MOON VIEWING!\u0000\n"
]
}
],
"prompt_number": 20
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 18.4, Page No 303"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# There is no proper concept of Enum(Enumaration) in python so i have implemented in this manner\n",
"def enum(*sequential, **named):\n",
" enums = dict(zip(sequential, range(len(sequential))), **named)\n",
" return type('Enum', (), enums)\n",
"\n",
"con=enum(MIN_NUM=0,MAX_NUM=100)\n",
"\n",
"def fRecur(n):\n",
" if(n==con.MIN_NUM):\n",
" return 0\n",
" return (fRecur(n-1)+n)\n",
"\n",
"sum1=sum2=0\n",
"for i in range(1,(con.MAX_NUM)+1):\n",
" sum1+=i\n",
" sum2=fRecur(con.MAX_NUM)\n",
"\n",
"print \"The value of sum1 is %d.\" % sum1\n",
"print \"The value returned by fRecur() is %d.\" % sum2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The value of sum1 is 5050.\n",
"The value returned by fRecur() is 5050.\n"
]
}
],
"prompt_number": 34
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 18.5, Page No 306"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#The output is correct as it takes the CMD LINES from respective terminal.\n",
"import sys\n",
"print \"The value received by argc is \" ,len(sys.argv),\".\\n\"\n",
"print \"There are \",len(sys.argv), \"command-line arguments passed to main().\\n\"\n",
"print \"The first command-line argument is: \",sys.argv[0],\"\\n\"\n",
"print \"The rest of the command-line arguments are:\\n\"\n",
"for i in range(1,len(sys.argv)):\n",
" print sys.argv[i]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The value received by argc is 8 .\n",
"\n",
"There are 8 command-line arguments passed to main().\n",
"\n",
"The first command-line argument is: -c \n",
"\n",
"The rest of the command-line arguments are:\n",
"\n",
"-f\n",
"C:\\Users\\Vaibhav\\.ipython\\profile_default\\security\\kernel-683917c3-368a-425a-93a3-9782f6d369b5.json\n",
"--IPKernelApp.parent_appname='ipython-notebook'\n",
"--profile-dir\n",
"C:\\Users\\Vaibhav\\.ipython\\profile_default\n",
"--interrupt=852\n",
"--parent=1008\n"
]
}
],
"prompt_number": 1
}
],
"metadata": {}
}
]
}
|