forked from csev/pythonauto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercises.php
More file actions
521 lines (483 loc) · 19.4 KB
/
exercises.php
File metadata and controls
521 lines (483 loc) · 19.4 KB
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
<?php
$EXERCISES =
Array(
"hello" => Array (
"qtext" => "Write a program that uses a <b>print</b> statement to say 'hello world'
as shown below.",
"desired" => "hello world",
"code" => 'prinq "hello world"',
"checks" => Array(
"print" => "You must use a print statement within the loop."
)),
"loop" => Array (
"qtext" => "Write a program that uses a <b>for</b> loop and the built-in function
<b>range</b> to write out three numbers as shown below.",
"desired" => "0
1
2",
"code" => 'print range(3)',
"checks" => Array(
"for" => "You must produce the numbers using a for loop.",
"print" => "You must use a print statement within the loop.",
"range" => "You should use the range function to generate the list of numbers on the for statement.",
":" => "Your for statement should end with a colon (:) and the following line should be indented"
)),
"2.2" => Array (
"qtext" => "<b>2.2</b> Write a program that uses <b>raw_input</b>
to prompt a user for their name and then
welcomes them. Note that <b>raw_input</b> will pop a dialog box.",
"desired" => "Hello Sarah",
"code" => '# The code below almost works
name = raw_input("Enter your name")
print "Howdy"',
"checks" => Array(
"raw_input" => "You must prompt for the user's name using the raw_input() function.",
"!Sarah" => "You must actually prompt for the user's name",
"print" => "You must use the print statement to print the line of output."
)),
"2.3" => Array(
"qtext" => "<b>2.3</b> Write a program to prompt the user for hours and rate per hour using raw_input
to compute gross pay. Use 35 hours and a rate of 2.75 per hour to test the
program (the pay should be 96.25). You should use <b>raw_input</b> to
read a string and <b>float()</b> to convert the string to a number.
Do not worry about error checking or bad user data.",
"desired" => "Pay: 96.25",
"code" => '# This first line is provided for you
hrs = raw_input("Enter Hours:")',
"checks" => Array(
"raw_input" => "You must prompt the pay and rate using the raw_input() function.",
"print" => "You must use the print statement to print the output.",
"float" => "You should use the built-in float() function to convert from a string to a float.",
"!96.25" => "You must actually calculate the pay.")),
"3.1" => Array(
"qtext" => "<b>3.1</b> Write a program to prompt the user for hours and rate per hour using raw_input
to compute gross pay. Award time-and-a-half for the hourly rate for all hours
worked above 40 hours. Use 45 hours and a rate of 10.00 per hour to test the
program (the pay should be 475). You should use <b>raw_input</b> to
read a string and <b>float()</b> to convert the string to a number.
Do not worry about error checking the user input - assume the user types numbers properly.
",
"desired" => "Pay: 475",
"code" => 'hrs = raw_input("Enter Hours:")
h = float(hrs)',
"checks" => Array(
"raw_input" => "You must prompt the pay and rate using the raw_input() function.",
"print" => "You must use the print statement to print the output.",
"if" => "You should use an if statement to decide to to the overtime computation or not.",
"float" => "You should use the built-in float() function to convert from a string to a float.",
"!475" => "You must actually calculate the pay.")),
"3.3" => Array(
"qtext" => "<b>3.3</b> Write a program to prompt the user for a score using <b>raw_input</b>.
Print out a letter grade based on the following table:<br/>
Score Grade<br/>
>= 0.9 A<br/>
>= 0.8 B<br/>
>= 0.7 C<br/>
>= 0.6 D<br/>
< 0.6 F<br/>
If the user enters a value out of range, print a suitable error message and exit.
For the test, enter a score of 0.85.
",
"desired" => "B",
"code" => '',
"checks" => Array(
"raw_input" => "You must prompt for the score using the raw_input() function.",
"float" => "You should use the built-in float() function to convert from a string to a float.",
"print" => "You must use the print statement to print the output.",
"if" => "You should use an if statement to check the value of the score.",
"elif" => "You should use an elif statement to check the value of the score.")
),
"4.6" => Array(
"qtext" => "<b>4.6</b> Write a program to prompt the user for hours and rate per hour using raw_input
to compute gross pay. Award time-and-a-half for the hourly rate for all hours
worked above 40 hours.
Put the logic to do the computation of time-and-a-half in a function called <b>computepay()</b>
and use the function to do the computation. The function should return a value.
Use 45 hours and a rate of 10.00 per hour to test the
program (the pay should be 475.0). You should use <b>raw_input</b> to
read a string and <b>float()</b> to convert the string to a number.
Do not worry about error checking the user input unless you want to -
you can assume the user types numbers properly.
",
"desired" => "Pay: 475",
"code" => 'def computepay(h,r):
return 42.37
hrs = raw_input("Enter Hours:")
p = computepay(10,20)
print "Pay",p',
"checks" => Array(
"raw_input" => "You must prompt the pay and rate using the raw_input() function.",
"print" => "You must use the print statement to print the output.",
"if" => "You should use an if statement to decide to to the overtime computation or not.",
"float" => "You should use the built-in float() function to convert from a string to a float.",
"def" => "You must use a function called computepay to do the computation.",
"return" => "You must use a return statement to pass the computed pay back to the main code.",
"computepay" => "You must use a function called computepay to do the computation.",
"!475" => "You must actually calculate the pay.")
),
"5.2" => Array(
"qtext" => "<b>5.2</b> Write a program that repeatedly prompts a user for integer numbers
until the user enters 'done'. Once 'done' is entered, print out the largest and smallest
of the numbers. If the user enters anything other than a valid number catch it
with a try/except and put out an appropriate message and ignore the number.
Enter the numbers from the book for problem 5.1 and Match the sample output below.
",
"desired" => "Invalid input
Maximum is 7
Minimum is 4",
"code" => 'largest = None
smallest = None
while True:
num = raw_input("Enter a number: ")
if num == "done" : break
print num
print "Maximum", largest',
"checks" => Array(
"raw_input" => "You must prompt for the numbers using the raw_input() function.",
"print" => "You must use the print statement to print the output.",
"while" => "You should use a while statement to read the numbers.",
"int" => "You should use the int() function to convert from a string to an integer.",
"try" => "You should handle bad numbers using a try/except structure.",
"except" => "You should handle bad numbers using a try/except structure.")
),
"6.5" => Array(
"qtext" => "<b>6.5</b> Write code using find() and string slicing (see section 6.10) to extract
the number at the end of the line below. Convert the extracted value to a floating point
number and print it out.",
"desired" => "0.8475",
"code" => 'text = "X-DSPAM-Confidence: 0.8475";',
"checks" => Array(
"find" => "You should use the find function to get the position of the colon in the string.",
":" => "You should use string slicing [n:m] to extract data from the string.",
"float" => "You should use the float() function to convert from a string to an integer.",
'!"0.8475"' => "You must actually pull the data from the string.")
),
"7.1" => Array(
"qtext" => "<b>7.1</b> Write a program that prompts for a file name, then opens that file
and reads through the file, and print the contents of the file in upper case. Use
the file <b>words.txt</b> to produce the output below.".
'<p>
You can download the sample data at
<a href="http://www.pythonlearn.com/code/words.txt" target="_blank">
http://www.pythonlearn.com/code/words.txt</a>',
"desired" => "WRITING PROGRAMS OR PROGRAMMING IS A VERY CREATIVE
AND REWARDING ACTIVITY YOU CAN WRITE PROGRAMS FOR
MANY REASONS RANGING FROM MAKING YOUR LIVING TO SOLVING
A DIFFICULT DATA ANALYSIS PROBLEM TO HAVING FUN TO HELPING
SOMEONE ELSE SOLVE A PROBLEM THIS BOOK ASSUMES THAT
{\EM EVERYONE} NEEDS TO KNOW HOW TO PROGRAM AND THAT ONCE
YOU KNOW HOW TO PROGRAM, YOU WILL FIGURE OUT WHAT YOU WANT
TO DO WITH YOUR NEWFOUND SKILLS
WE ARE SURROUNDED IN OUR DAILY LIVES WITH COMPUTERS RANGING
FROM LAPTOPS TO CELL PHONES WE CAN THINK OF THESE COMPUTERS
AS OUR PERSONAL ASSISTANTS WHO CAN TAKE CARE OF MANY THINGS
ON OUR BEHALF THE HARDWARE IN OUR CURRENT-DAY COMPUTERS
IS ESSENTIALLY BUILT TO CONTINUOUSLY AS US THE QUESTION
WHAT WOULD YOU LIKE ME TO DO NEXT
OUR COMPUTERS ARE FAST AND HAVE VASTS AMOUNTS OF MEMORY AND
COULD BE VERY HELPFUL TO US IF WE ONLY KNEW THE LANGUAGE TO
SPEAK TO EXPLAIN TO THE COMPUTER WHAT WE WOULD LIKE IT TO
DO NEXT IF WE KNEW THIS LANGUAGE WE COULD TELL THE
COMPUTER TO DO TASKS ON OUR BEHALF THAT WERE REPTITIVE
INTERESTINGLY, THE KINDS OF THINGS COMPUTERS CAN DO BEST
ARE OFTEN THE KINDS OF THINGS THAT WE HUMANS FIND BORING
AND MIND-NUMBING",
"code" => '# Use words.txt as the file name
fname = raw_input("Enter file name: ")
fh = open(fname)
',
"xcode" => '# Use words.txt as the file name
fname = raw_input("Enter file name: ")
fh = open(fname)
text = fh.read().strip()
print text.upper()
',
"checks" => Array(
"raw_input" => "You must prompt for the file name using the raw_input() function.",
"open" => "You need to use open() to open the file.",
"print" => "You must use the print statement to print the lines.",
"strip" => "You should use strip() or rstrip() to avoid double newlines. You may need to scroll down to see a mis-match of the output.",
"upper" => "You should use the upper() function to convert the lines to upper case.")
),
"7.2" => Array(
"qtext" => '<b>7.2</b> Write a program that prompts for a file name, then opens that file
and reads through the file, looking for lines of the form:
<pre>
X-DSPAM-Confidence: 0.8475
</pre>
Count these lines and extract the floating point values from each
of the lines and coput the average of those values and produce an output
as shown below.
<p>
You can download the sample data at
<a href="http://www.pythonlearn.com/code/mbox-short.txt" target="_blank">
http://www.pythonlearn.com/code/mbox-short.txt</a> when you are testing
below enter <b>mbox-short.txt</b> as the file name.',
"desired" => "Average spam confidence: 0.7507185185185187",
"code" => '# Use the file name mbox-short.txt as the file name
fname = raw_input("Enter file name: ")
fh = open(fname)
for line in fh:
if not line.startswith("X-DSPAM-Confidence:") : continue
print line
print "Done"
',
"xcode" => '# Use the file name mbox-short.txt as the file name
fname = raw_input("Enter file name: ")
fh = open(fname)
tot = 0.0
count = 0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:") : continue
words = line.split()
tot = tot + float(words[1])
count = count + 1
print "Average spam confidence:", tot/count
',
"checks" => Array(
"raw_input" => "You must prompt for the file name using the raw_input() function.",
"open" => "You need to use open() to open the file.",
"float" => "You should use the float() function to convert from a string to an integer.",
'!18518' => "You must actually pull the data from the strings and convert it.",
"/" => "Average is usually a total / count.")
),
"8.4" => Array(
"qtext" => '<b>8.4</b> Open the file <b>romeo.txt</b> and read it line by
line. For each line, split the line into a list of words using the <b>split()</b>
function. The program should build a list of words. For each word on each line
check to see if the word is already in the list and if not append it to the list.
When the program completes, sort and print the resulting words in alphabetical order.
<p>
You can download the sample data at
<a href="http://www.pythonlearn.com/code/romeo.txt" target="_blank">
http://www.pythonlearn.com/code/romeo.txt</a>',
"desired" => "['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']",
"code" => 'fname = raw_input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
print line.rstrip()
',
"xcode" => 'fname = raw_input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
words = line.split()
for word in words:
if word in lst: continue
lst.append(word)
lst.sort()
print lst
',
"checks" => Array(
"split" => "You should use split() to break each line into words.",
"append" => "You should use append() to add the word to the list if it is not there.",
"raw_input" => "You should prompt for the file name using the raw_input() function.",
"open" => "You need to use open() to open the file.",
"sort" => "You need to use sort() to sort the list before you print it out.",
"for" => "You need two for loops. One for the lines and one for the words on each line.")
),
"8.5" => Array(
"qtext" => "<b>8.5</b> Open the file <b>mbox-short.txt</b> and read it line by
line. When you find a line that starts with 'From ' like the following line:
<pre>
From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
</pre>
You will parse the From line using split() and print out the second word in the line
(i.e. the entire address of the person who sent the message). Then print out
a count at the end.
<p>
<b>Hint:</b> make sure not to include the lines that start with 'From:'.".
'<p>
You can download the sample data at
<a href="http://www.pythonlearn.com/code/mbox-short.txt" target="_blank">
http://www.pythonlearn.com/code/mbox-short.txt</a>',
"desired" => "stephen.marquard@uct.ac.za
louis@media.berkeley.edu
zqian@umich.edu
rjlowe@iupui.edu
zqian@umich.edu
rjlowe@iupui.edu
cwen@iupui.edu
cwen@iupui.edu
gsilver@umich.edu
gsilver@umich.edu
zqian@umich.edu
gsilver@umich.edu
wagnermr@iupui.edu
zqian@umich.edu
antranig@caret.cam.ac.uk
gopal.ramasammycook@gmail.com
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
stephen.marquard@uct.ac.za
louis@media.berkeley.edu
louis@media.berkeley.edu
ray@media.berkeley.edu
cwen@iupui.edu
cwen@iupui.edu
cwen@iupui.edu
There were 27 lines in the file with From as the first word",
"code" => 'fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
print "There were", count, "lines in the file with From as the first word"
',
"xcode" => 'fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
for line in fh:
wds = line.split()
if len(wds) < 2 : continue
if wds[0] != "From" : continue
print wds[1]
count = count + 1
print "There were", count, "lines in the file with From as the first word"
',
"checks" => Array(
"for" => "You need a for loop to read the lines in the file.",
"split" => "You should use split() to break each line into words.",
"if" => "You need to use one or more if statements to skip the lines that do not start with 'From '.",
"raw_input" => "You should prompt for the file name using the raw_input() function.",
"open" => "You need to use open() to open the file.")
),
"9.4" => Array(
"qtext" => "<b>9.4</b> Write a program to read through the <b>mbox-short.txt</b> and figure
out who has the most commits. The program looks for 'From ' lines and takes the second
word of those lines as the person who sent the mail. The program creates a Python
dictionary that maps the senders mail address to a count of the number of times
they appear in the file. After the dictionary is produced, the program reads through
the dictionary using a maximum loop to find the most prolific committer.",
"desired" => "cwen@iupui.edu 5",
"code" => 'name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
',
"xcode" => 'name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
counts = dict()
for line in handle:
wds = line.split()
if len(wds) < 2 : continue
if wds[0] != "From" : continue
email = wds[1]
counts[email] = counts.get(email,0) + 1
bigcount = None
bigname = None
for name,count in counts.items():
if bigname is None or count > bigcount:
bigname = name
bigcount = count
print bigname, bigcount
',
"checks" => Array(
"for" => "You need a for loop to read the lines in the file.",
"split" => "You should use split() to break each line into words.",
"if" => "You need to use one or more if statements to skip the lines that do not start with 'From '.",
"raw_input" => "You should prompt for the file name using the raw_input() function.",
"open" => "You need to use open() to open the file.")
),
"10.2" => Array(
"qtext" => "<b>10.2</b> Write a program to read through the <b>mbox-short.txt</b> and figure
out the distribution by hour of the day for each of the messages. You can pull the hour
out from the 'From ' line by finding the time and then splitting the string a second time
using a colon.
<pre>
From stephen.marquard@uct.ac.za Sat Jan 5 <b>09</b>:14:16 2008
</pre>
Once you have accumulated the counts for each hour, print out the counts, sorted by hour
as shown below. Note that the autograder does not have support for the sorted() function.",
"desired" => "04 3
06 1
07 1
09 2
10 3
11 6
14 1
15 2
16 4
17 2
18 1
19 1",
"code" => 'name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
',
"xcode" => 'name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
counts = dict()
for line in handle:
wds = line.split()
if len(wds) < 5 : continue
if wds[0] != "From" : continue
when = wds[5]
tics = when.split(":")
if len(tics) != 3 : continue
hour = tics[0]
counts[hour] = counts.get(hour,0) + 1
lst = counts.items()
lst.sort()
for key, val in lst :
print key, val
',
"checks" => Array(
"for" => "You need a for loop to read the lines in the file.",
"sort" => "You need to use list sort() method to sort the list of times.")
),
"11.1" => Array (
"qtext" => '<b>11.1</b> Sadly, the autograder does not support the regular expression library.
So please write a program that computes the
<b>Answer to the Ultimate Question of Life, the Universe, and Everything</b>
[<a href="http://www.youtube.com/watch?v=aboZctrHfK8" target="_blank">more detail</a>].
Sample output is below.',
"desired" => "42",
"code" => '',
"checks" => Array(
"print" => "By now you should know that a print statement would be helpful here.",
"*" => "I think that multiplication is involved..."
)),
"11.9" => Array(
"qtext" => "<b>11.9</b> Write a program to prompt the user for a regular expression
and read through the <b>mbox-short.txt</b> and count the number of lines that match
the regular expression using re.search().",
"desired" => "04 3
19 1",
"code" => 'import re
string = raw_input("Enter a regular expression:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open("mbox-short.txt")
count = 0
for line in handle:
if re.search(string) : count = count + 1
print "mbox-short.txt had ", count, "lines that matched", string
',
"xcode" => 'name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
counts = dict()
for line in handle:
wds = line.split()
if len(wds) < 5 : continue
if wds[0] != "From" : continue
when = wds[5]
tics = when.split(":")
if len(tics) != 3 : continue
hour = tics[0]
counts[hour] = counts.get(hour,0) + 1
lst = counts.items()
lst.sort()
for key, val in lst :
print key, val
',
"checks" => Array(
"for" => "You need a for loop to read the lines in the file.",
"sort" => "You need to use list sort() method to sort the list of times.")
)
);
?>