You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: notes/L-Files.ipynb
+29-76Lines changed: 29 additions & 76 deletions
Original file line number
Diff line number
Diff line change
@@ -64,7 +64,7 @@
64
64
"# Create a file numbers.txt and write the numbers from 0 to 24 there\n",
65
65
"f = open(\"numbers.txt\", \"w\")\n",
66
66
"for num in range(25):\n",
67
-
" f.write(str(num)+'\\n')\n",
67
+
" f.write(str(num) + \"\\n\")\n",
68
68
"f.close()"
69
69
]
70
70
},
@@ -158,7 +158,7 @@
158
158
"metadata": {},
159
159
"outputs": [],
160
160
"source": [
161
-
"lines = content.split(\"\\n\") # we get back a list of strings\n",
161
+
"lines = content.split(\"\\n\") # we get back a list of strings\n",
162
162
"print(lines)"
163
163
]
164
164
},
@@ -169,9 +169,9 @@
169
169
"outputs": [],
170
170
"source": [
171
171
"# here we convert the strings into integers, using a list comprehension\n",
172
-
"# we have the conditional to avoid trying to parse the string '' that\n",
172
+
"# we have the conditional to avoid trying to parse the string '' that\n",
173
173
"# is at the end of the list\n",
174
-
"numbers = [int(line) for line in lines if len(line)>0]\n",
174
+
"numbers = [int(line) for line in lines if len(line) > 0]\n",
175
175
"print(numbers)"
176
176
]
177
177
},
@@ -192,7 +192,7 @@
192
192
"source": [
193
193
"#### Exercise 1\n",
194
194
"\n",
195
-
"* Write a function that reads a file and returns its content as a list of strings (one string per line). Read the file with filename `data/restaurant-names.txt`. If you stored your notebook under `Student_Notebooks` the full filename is `/home/ubuntu/jupyter/NYU_Notes/2-Introduction_to_Python/data/restaurant-names.txt`"
195
+
"* Write a function that reads a file and returns its content as a list of strings (one string per line). Read the file with filename `../data/restaurant-names.txt`."
196
196
]
197
197
},
198
198
{
@@ -208,7 +208,7 @@
208
208
"source": [
209
209
"#### Exercise 2\n",
210
210
"\n",
211
-
"* Write a function that reads the n-th column of a CSV file and returns its contents. (Reuse the function that you wrote above.) Then reads the file `data/baseball.csv` and return the content of the 5th column (`team`)."
211
+
"* Write a function that reads the n-th column of a CSV file and returns its contents. (Reuse the function that you wrote above.) Then reads the file `../data/baseball.csv` and return the content of the 5th column (`team`)."
212
212
]
213
213
},
214
214
{
@@ -224,35 +224,12 @@
224
224
"source": [
225
225
"#### Exercise 3 \n",
226
226
"\n",
227
-
"The command below will create a file called `phonetest.txt`. Write code that:\n",
228
-
"* Reads the file `phonetest.txt`\n",
227
+
"Write code that:\n",
228
+
"* Reads the file `../data/phonetest.txt`\n",
229
229
"* Write a function that takes as input a string, and removes any non-digit characters\n",
230
230
"* Print out the \"clean\" string, without any non-digit characters"
231
231
]
232
232
},
233
-
{
234
-
"cell_type": "code",
235
-
"execution_count": null,
236
-
"metadata": {},
237
-
"outputs": [],
238
-
"source": [
239
-
"%%file phonetest.txt\n",
240
-
"679-397-5255\n",
241
-
"2126660921\n",
242
-
"212-998-0902\n",
243
-
"888-888-2222\n",
244
-
"800-555-1211\n",
245
-
"800 555 1212\n",
246
-
"800.555.1213\n",
247
-
"(800) 555-1214\n",
248
-
"1-800-555-1215\n",
249
-
"1(800)555-1216\n",
250
-
"800-555-1212-1234\n",
251
-
"800-555-1212x1234\n",
252
-
"800-555-1212 ext. 1234\n",
253
-
"work 1-(800) 555.1212 #1234"
254
-
]
255
-
},
256
233
{
257
234
"cell_type": "code",
258
235
"execution_count": null,
@@ -276,49 +253,33 @@
276
253
"outputs": [],
277
254
"source": []
278
255
},
279
-
{
280
-
"cell_type": "code",
281
-
"execution_count": null,
282
-
"metadata": {},
283
-
"outputs": [],
284
-
"source": []
285
-
},
286
-
{
287
-
"cell_type": "code",
288
-
"execution_count": null,
289
-
"metadata": {},
290
-
"outputs": [],
291
-
"source": []
292
-
},
293
-
{
294
-
"cell_type": "code",
295
-
"execution_count": null,
296
-
"metadata": {},
297
-
"outputs": [],
298
-
"source": []
299
-
},
300
256
{
301
257
"cell_type": "markdown",
302
-
"metadata": {},
258
+
"metadata": {
259
+
"solution2": "hidden",
260
+
"solution2_first": true
261
+
},
303
262
"source": [
304
-
"#### Solution for exercise 4 (with a lot of comments)"
263
+
"#### Solution for exercise 3 (with a lot of comments)"
305
264
]
306
265
},
307
266
{
308
267
"cell_type": "code",
309
268
"execution_count": null,
310
-
"metadata": {},
269
+
"metadata": {
270
+
"solution2": "hidden"
271
+
},
311
272
"outputs": [],
312
273
"source": [
313
274
"# this function takes as input a phone (string variable)\n",
314
275
"# and prints only its digits\n",
315
276
"def clean(phone):\n",
316
-
" # We initialize the result variable to be empty.\n",
317
-
" # We will append to this variable the digit characters\n",
277
+
" # We initialize the result variable to be empty.\n",
278
+
" # We will append to this variable the digit characters\n",
318
279
" result = \"\"\n",
319
280
" # This is a set of digits (as **strings**) that will\n",
0 commit comments