forked from ninas/umonya_notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercises.html
531 lines (378 loc) · 20.9 KB
/
exercises.html
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
521
522
523
524
525
526
527
528
529
530
531
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Introductory Programming in Python: Exercises</title>
<link rel='stylesheet' type='text/css' href='style.css' />
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<script src="animation.js" type="text/javascript">
</script>
</head>
<body onload="animate_loop()">
<div class="page">
<h1>Introductory Programming in Python<br />
Exercises</h1>
<div class="centered">
[<a href="index.html">Course Outline</a>]<br />
[<a href="solutions">Solutions</a>]
</div>
<h3>Lesson 1</h3> <!-- Basic Concepts -->
<ol>
<li>What is a program?</li>
<li>What is the difference between an expression and a
statement?</li>
</ol>
<h3>Lesson 2</h3> <!-- Invocation -->
<p>Consider the following lines code...</p>
<pre class='listing'>a = 9
b = 3
a/b</pre>
<ol start='3'>
<li>What will the output be if these lines are entered into the
python interactive interpreter?</li>
<li>What will the output be if I run these lines from a
file/script?</li>
<li>What changes need to be made to produce output when I run
these lines from a file/script?</li>
</ol>
<h3>Lesson 3</h3> <!-- Basic Output -->
<p>Consider the following code...</p>
<pre class='listing'>print "MUCH madness is divinest sense,"
print "To a discerning eye;"
print "Much sense the starkest madness."
print "’T is the majority", "In this, as all, prevails."
print "Assent, and you are sane;"
print "Demur,—you ’re straightway dangerous",
print
print "And handled with a chain."</pre>
<ol start='6'>
<li>What does the print statement do generally?</li>
<li>What special rules does the print statement adhere to regarding
trailing spaces and newlines?</li>
<li>What output, exactly, does the above code produce. Indicate
space with underscores</li>
<li>Write a program that outputs favourite piece of poetry or other prose.</li>
<li>Write a program that outputs your name, age, and height in
metres in the following format. Make sure age is an integer, and
height is a float, and not simply part of your string.<br />
<code class='value'>My name is James, I am 28 years old and 1.78 metres
tall</code></li>
<li>Explain three possible ways to print a string containing an
apostrophe, for example the string<br /> <code class='value'>The cat's
mat</code>.</li>
</ol>
<h3>Lesson 4</h3> <!-- Program state -->
<ol start='12'>
<li>What does the assignment statement do?</li>
<li>What are the five basic variable types in python?</li>
<li>If the integer variable <em>i</em> has the value 7, then what is
the value of the expression <code class='value'>7/4</code>. Why?</li>
<li>How does one calculate the remainder (modulo) of an integer when
divided by another number?</li>
<li>If <em>s</em> is a sting variable with the value "Harry's Hippie
Hoedown", then what is the value of<br /><code class='value'>s + ": tickets only
$5"</code></li>
<li>If <em>s</em> is a sting variable with the value "Harry's Hippie
Hoedown", then what is the value of<br /><code class='value'>s + ": tickets only
$" + "5"*3</code></li>
<li>What is the value of <code class='value'>"ABBA was a Swedish band popular during the
80's"[0:4]</code>?</li>
<li>What is the value of <code class='value'>"ABBA was a Swedish band popular during the
80's"[-15:-7]</code>?</li>
<li>If the string variable <em>s</em> has the value "ABBA was a
Swedish band popular during the 80's", then what is the value of <br
/><code class='value'>"BAAB"+s[4:11]+"Danish"+s[19:25]+"un"+s[25:04]+"90's"</code></li>
</ol>
<h3>Lesson 5</h3> <!-- basic input -->
<ol start='21'>
<li>If there is a function named <em>my_function</em>, how do I call
it?</li>
<li>Are functions expressions or statements? What about when they
are called?</li>
<li>Write a program that asks the user to enter two numbers, and
prints the sum of those two numbers.</li>
<li>Write a program that asks the user to enter two numbers, and
prints the difference of those two numbers.</li>
<li>Write a program that asks the user to enter two numbers, and
prints the product of those two numbers. Are you sensing a pattern
here?</li>
<li>Write a program that asks the user for some text, and a number.
The program prints out the text a number of times equal to the
number entered, without line breaks or spaces in between each
repetition.</li>
</ol>
<h3>Lesson 6</h3> <!-- conditionals -->
<ol start='27'>
<li>Write a program that asks the user to enter two numbers. If the
second number is not zero, print the quotient of the two numbers,
otherwise print a message to the effect of not being able to divide
by zero.</li>
<li>Write a program that asks the user to enter 5 numbers, and
prints the smallest number entered.</li>
<li>Write a program that asks the user to enter 5 numbers, and
prints the largest number entered.</li>
<li>Write a program that asks the user to enter 5 numbers, and
prints their average.</li>
<li>Write a program that asks the user to enter a number from 1 to
12 and prints out the name of the corresponding month.</li>
<li>Write a program that asks the user to enter a four digit year,
and prints out whether that year is a leap year or not.</li>
<li>Write a program that asks the user to enter 3 names, then
outputs them sorted alphabetically.</li>
<li>Write a program that asks the user to enter a number, then
prints out all the numbers from 1 to 10 by which the entered number
is divisible.</li>
</ol>
<h3>Lesson 7</h3> <!-- conditional loops -->
<ol start='35'>
<li>Write a program that asks the user to enter a sequence of
numbers, ending with a blank line. Print out the smallest of those
numbers.</li>
<li>Write a program that asks the user to enter a sequence of
numbers, ending with a blank line. Print out the average of those
numbers.</li>
<li>Modify your solution to exercise 36 so that instead of printing
the average and then terminating when a blank line is entered, it
prints the average and then asks if the user wants to repeat the
process. If the user answers 'y' the process is repeated, otherwise
the program terminates.</li>
</ol>
<h3>Lesson 8</h3> <!-- sequential loops -->
<ol start='38'>
<li>Write a program that asks the user for the height of a triangle.
If a blank line is entered, the program finishes, otherwise it
prints out a right handed triangle, with the right angle on the
bottom right, made of asterisks ('*') of a height equal to the
number entered. Example input/output follows ...
<pre class='listing'>
Enter triangle height: 3
*
**
***
Enter triangle height: 5
*
**
***
****
*****
Enter triangle height:
</pre>
</li>
<li>Same as above, except now with the right angle in the top right! Example ...
<pre class='listing'>
Enter triangle height: 3
***
**
*
Enter triangle height: 5
*****
****
***
**
*
Enter triangle height:
</pre>
</li>
</ol>
<h3>Lesson 9</h3> <!-- lists -->
<ol start='40'>
<li>Write a program that asks the user to enter a sequence of up to
5 x:y coordinates with both x and y in the range 0 to 4, ending
their sequence entry by providing a blank line for the x
coordinate. Then display a five by five grid of '#' characters,
with the points in the grid entered by the user left blank. Assume
x increases from left to right, and y increases from top to bottom.
Example input/output is given ...
<pre class='listing'>Coordinates range from 0 to 4!
Please enter pair of coordinates (x:y), leave x blank to terminate sequence.
X> 3
Y> 3
Please enter pair of coordinates (x:y), leave x blank to terminate sequence.
X> 4
Y> 1
Please enter pair of coordinates (x:y), leave x blank to terminate sequence.
X> 1
Y> 4
Please enter pair of coordinates (x:y), leave x blank to terminate sequence.
X>
#####
####
#####
### #
# ###
</pre>
</li>
</ol>
<h3>Lesson 10</h3> <!-- tuples -->
<ol start='41'>
<li>
Write a function that returns the sum of two vectors, where the
sum of two vectors is a vector of the same dimension (i.e. has
the same number of elements) as both the original vectors (i.e.
original vectors must be of equal dimension, otherwise return
None), and has each element equal to the sum of its respective
elements from the original two vectors.<br />
<br />
<code>[x, y, z] + [a, b, c] = [x+a, y+b, z+c]</code><br />
<br />
Then ask the user to enter the direction and speed (in the form
of a 2 dimensional vector) of the wind, and again of an object
travelling in a two dimensional space. Your program should
output the vector representing the resultant direction vector of
the object.<br />
<br />
<a href="http://www.physicsclassroom.com/Class/vectors/U3L3a.html">Some High School Physics Background</a><br />
<a href="http://kipper.crk.umn.edu/physics/1012/xtr/vectors.html">More Complex, but More Complete</a>
</li>
</ol>
<h3>Lesson 11</h3> <!-- dictionaries -->
<h3>Lesson 12</h3> <!-- strings in depth -->
<h3>Lesson 13</h3> <!-- functions -->
<h3>Lesson 14</h3> <!-- variable scope -->
<h3>Lesson 15</h3> <!-- standard modules (math, sys, os) -->
<h3>Lesson 15</h3> <!-- standard modules (math, sys, os) -->
<h3>Lesson 16</h3> <!-- command line arguments -->
<h3>Lesson 17</h3> <!-- random numbers -->
<h3>Lesson 18</h3> <!-- files -->
<h3>Lesson 19</h3> <!-- regular expressions -->
<h3>Lesson 20</h3> <!-- parsing -->
<h3>Lesson 21</h3> <!-- os functionality -->
<h3>Lesson 22</h3> <!-- dates and times -->
<h3>Lesson 23</h3> <!-- writing error messages -->
<h3>Lesson 24</h3> <!-- understanding error messages -->
<h3>Lesson 25</h3> <!-- exceptions -->
<h3>Lesson 26</h3> <!-- debugging -->
<h3>Lesson 27</h3> <!-- recursion -->
<h3>Lesson 28</h3> <!-- classes -->
<h3>Lesson 29</h3> <!-- queues -->
<h3>Lesson 30</h3> <!-- stacks -->
<h3>Lesson 31</h3> <!-- trees -->
<h3>Lesson 32</h3> <!-- database theory -->
<h3>Lesson 33</h3> <!-- relational databases and SQL -->
<h3>Lesson 34</h3> <!-- using databases in python -->
<h3>Lesson 35</h3> <!-- parallel processing -->
<h3>Lesson 36</h3> <!-- event oreinted programming -->
<h3>Lesson 37</h3> <!-- gui programming -->
<h3>Lesson 38</h3> <!-- web programming/services -->
<ol start="42">
<li>A small arts and crafts store owner in the middle of the Karoo
has recently upgraded to a computerised point of sale system, and
wants to do the same for his guest book. Customers have previously
left their names a small paragraph of comment in the book. The owner
would like his customers to be able to walk up to a computer near
the exit, type in their names, and enter a brief comment. He's only
interested in a customer's most recent comments, and doesn't want
store old comments. So repeat customer's must be able to update
their previous comments. When a repeat customer types in their name,
their previous comment is displayed back to them, and they are
afforded the opportunity to enter a new comment. Should they enter a
blank line instead of a comment, their previous comment is
preserved. Also, if instead of a customer name the special command
'quit' is entered, the program exits. Similarly the command
'showcomments' causes all customers' names to be displayed, followed
by their comments slightly indented. Customer's must be able to
enter their names in any case.</li>
<li>Extend your solution to the previous problem, by allowing
customers to enter multi-line comments, and to terminate their
comments by entering a blank line. If the comment is entirely blank,
i.e. the first line is blank, then it does not overwrite the former
comment if any. Also, ensure that when the comments are outputted
back, either because of the 'showcomments' command, or a repeat
customer entering their name, that the line width of the outputted
comments does not exceed 60 characters, nor break a word in two,
i.e. lines are only broken on white space.</li>
</ol>
<h3>Lesson 12</h3>
<ol start="43">
<li>Write a program to display the contents of a file as is, i.e.
the whitespace and line breaks outputted must be identical to that
in the file. Ask the user which file to display.</li>
<li>Write a program that asks the user for the name of a file, asks
for the name of another file, then outputs the contents of the
first file into the second file, essentially copying it.</li>
<li>Convert the previous program into a function, and allow the
user to continue entering filenames for source and destination
files, until they enter a blank line for the source file.</li>
<li>Instead of copying a file as is, write a new function that
coverts all letters in the source file to uppercase letters when
written to the destination file.</li>
<li>Write a program that reads in a FastA file of the user's
choice, and outputs a list of the 'titles' contained therein. You
can obtain a sample FastA file <a
href='data/brca.fasta'>here</a>.</li>
<li>Write a program that reads in a FastA file of the user's
choice, and outputs a numbered list of the titles contained
therein. Ask the user for a number from the menu, make sure that
the number is in range, otherwise ask again. Display the title and
sequence string of the sequence chosen by the user, then display
the menu again. The user may quit the program by entering 'q' at
the menu prompt.</li>
<li>Write a program that reads in a grid of biomass densities (specified as floats) from a file designated by the user. The grid can be of any dimensions, as long as it is square, and is specified in the file by a number of <strong>
tab separated</strong> floating point numbers, where tabs indicate column separation, and lines indicate row separation. Ask the user to input a number of hours. Each hour the biomass density in each cell of the grid increases by 5%. Biomass
densities may not be greater than 1, and excess biomass density is spread evenly amongst the four grid cells to the north, east, south, and west. If those cells are already at density 1 themselves, no growth takes place. The grid does not wrap
around. After the number of hours specified have been calculated, output the grid as a collection of lines of floats indicating the new biomass levels per cell. Sample output might be
<pre>
0 1 2 3 4 5 6 7 8 9
0 1.0 1.0 0.5 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1 1.0 0.8 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
2 0.5 0.0 0.0 0.0 0.0 0.5 0.0 0.0 0.0 0.0
3 0.0 0.0 0.0 0.0 0.8 1.0 0.8 0.0 0.0 0.0
4 0.0 0.0 0.0 0.8 1.0 1.0 1.0 0.5 0.0 0.0
5 0.0 0.0 0.0 0.0 0.8 1.0 0.8 0.0 0.0 0.0
6 0.0 0.0 0.0 0.0 0.0 0.5 0.0 0.0 0.0 0.0
7 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
8 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
9 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
</pre>
</li>
</ol>
<p class="header"><a name="week4" id="week4">Week 4</a></p>
<ol start="30">
<li>Write a program that reads a file specified as the first argument on the command line, and outputs it to screen.</li>
<li>Write a program that reads a file specified as the last argument on the command line. If any optional command line arguments are encountered prior to the filename, i.e. more than one command line argument is given, those
arguments specify things to do to the output. If the argument '-b' is encountered, blank lines are not outputted. If the argument '-C' is encountered, the first non white space letter after a period is Capitalised. Any error conditions should be
sent to stderr.</li>
<li>Write a program that accepts as its first command line argument a width in columns, and as its second command line argument a filename. The contents of that file are outputted to screen using lines no longer than width, and not
breaking any words in two.</li>
<li>Write a program to output the CG/AT ratio of all sequences in a FastA file specified on the command line, such that their titles are outputted followed by their GC/AT ratio, one sequence per line. The CG/AT ratio of a sequence
is defined as the sum of occurences of 'C' and 'G' divided by the sum of occurences of 'A' and 'T' within a that sequence.</li>
<li>Write a program that accepts a single FastA file from the command line and for each sequence in the file, outputs that sequence's title, followed by a sequence of indented lines, one for each coding region found in the sequence,
containing the contents of the coding region. Coding regions subsequent to the first should be in the same reading frame. Coding regions start at start codons ('TAA', 'TAG', 'TGA'), and stop at stop codons ('TTG', 'CTG', 'ATG'). Any match of start
and stop codons defines a coding region.</li>
<li>Describe in a natural langauge what the regular expression <b>Jack Sprat could eat no (fat)|(hat)</b> matches.</li>
<li>Describe in a natural language what the regular expression <b>His wife|girlfriend could eat no lean|bean</b> matches.</li>
<li>Write down all the possible matches for the regular expression <b>All in all Jack's ((wife's)|(girlfriend's) )?dietician was pretty (un)?happy about the situation</b>.</li>
<li>Write a regular expression that identifies an integer.</li>
<li>Write a regular expression that identifies a float.</li>
<li>Write a regular expression that identifies a coding region.</li>
<li>Write a regular expression that identifies a line containing at least one word float pair, where the word and the float are separated by a comma and any amount of white space before or after the comma, and pairs are separated
from one another in the same way. An example line would be "Height, 0.37 ,\tweight,13.2 ,width, 0.54"</li>
</ol>
<p class="header"><a name="week5" id="week5">Week 5</a></p>
<ol start="42">
<li>Write a program that counts the number of a given character (from the command line) in a given string (also from the command line) recursively.</li>
<li>Write a program that outputs the reverse of a given string (from the command line), by using a recursive reverse function.</li>
<li>Write a non recursive function to sort a list of ten integers generated randomly. Do not use the list.sort() method.</li>
<li>Write a recursive sort function. <em>Hint:</em> Write a function that inserts an integer into the correct place in an already sorted list, using recursion. Use this in your sort function.</li>
<li>Write a program that lists all the permutations of a string given on the command line.</li>
<li>A transport company needs to buy five new vehicles for its operation. As they are a start up company, they are very concerned about cash, and would like to spend as little as possible to get the most out of their vehicles. They
want to get as much carrying capacity for as little money as possible, and they cannot afford more than R1 million anyway. Vehicles have the properties: cost, and carrying capacity. Assuming we are supplied the list of vehicle models available from
the command line, each model as an argument specified as '<name>:<cost>:<capacity>', write a program that outputs the optimal combination of five vehicles that give the highest capacity/cost ratio.</li>
</ol>
<p class="header"><a name="week6" id="week6">Week 6</a></p>
<ol start="48">
<li>Write a vector class for vectors of any size. The class must support vector addition (__sub__), vector subtraction (__sub__), scalar multiplication (__mul__ by integer/float), vector multiplication (__mul__ by vector), unary
negation (__neg__), magnitude/distance (__abs__), scaled magnitide, and dot product operations.</li>
<li>Write a matrix class for matrices of any size. The class must support matrix addition (__add__), matrix subtraction (__sub__), scalar multiplication (__mul__ by integer/float), matrix multiplication (__mul__ by matrix/vector),
unary negation (__neg__), and transposition operations.</li>
</ol>
<div class="centered">
[<a href="index.html">Course Outline</a>]<br />
[<a href="solutions">Solutions</a>]
</div>
</div>
<div class="pagefooter">
Copyright © James Dominy 2007-2008; Released under the <a href="http://www.gnu.org/copyleft/fdl.html">GNU Free Documentation License</a><br />
<a href="intropython.tar.gz">Download the tarball</a>
</div>
</body>
</html>