forked from bast/python-tdd-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercises.py
396 lines (297 loc) · 10.9 KB
/
exercises.py
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
def reverse_list(l):
"""
Reverses order of elements in list l.
"""
return None
def test_reverse_list():
assert reverse_list([1, 2, 3, 4, 5]) == [5, 4, 3, 2, 1]
# ------------------------------------------------------------------------------
def reverse_string(s):
"""
Reverses order of characters in string s.
"""
return None
def test_reverse_string():
assert reverse_string("foobar") == "raboof"
# ------------------------------------------------------------------------------
def is_english_vowel(c):
"""
Returns True if c is an english vowel
and False otherwise.
"""
return None
def test_is_english_vowel():
assert is_english_vowel('a')
assert is_english_vowel('e')
assert is_english_vowel('i')
assert is_english_vowel('o')
assert is_english_vowel('u')
assert is_english_vowel('y')
assert is_english_vowel('A')
assert is_english_vowel('E')
assert is_english_vowel('I')
assert is_english_vowel('O')
assert is_english_vowel('U')
assert is_english_vowel('Y')
assert not is_english_vowel('k')
assert not is_english_vowel('z')
assert not is_english_vowel('?')
# ------------------------------------------------------------------------------
def count_num_vowels(s):
"""
Returns the number of vowels in a string s.
"""
return None
def test_count_num_vowels():
sentence = "hey ho let's go"
assert count_num_vowels(sentence) == 5
sentence = "HEY ho let's GO"
assert count_num_vowels(sentence) == 5
paragraph = """She told me her name was Billie Jean,
as she caused a scene
Then every head turned with eyes
that dreamed of being the one
Who will dance on the floor in the round"""
assert count_num_vowels(paragraph) == 54
# ------------------------------------------------------------------------------
def histogram(l):
"""
Converts a list of integers into a simple string histogram.
"""
return None
def test_histogram():
assert histogram([2, 5, 1]) == '##\n#####\n#'
# ------------------------------------------------------------------------------
def get_word_lengths(s):
"""
Returns a list of integers representing
the word lengths in string s.
"""
return None
def test_get_word_lengths():
text = "Three tomatoes are walking down the street"
assert get_word_lengths(text) == [5, 8, 3, 7, 4, 3, 6]
# ------------------------------------------------------------------------------
def find_longest_word(s):
"""
Returns the longest word in string s.
In case there are several, return the first.
"""
return None
def test_find_longest_word():
text = "Three tomatoes are walking down the street"
assert find_longest_word(text) == "tomatoes"
text = "foo foo1 foo2 foo3"
assert find_longest_word(text) == "foo1"
# ------------------------------------------------------------------------------
def validate_dna(s):
"""
Return True if the DNA string only contains characters
a, c, t, or g (lower or uppercase). False otherwise.
"""
return None
def test_validate_dna():
assert validate_dna('CCGGAAGAGCTTACTTAGccggaagagcttacttag')
assert not validate_dna('xCCGGAAGAGCTTACTTAGccggaagagcttacttag')
assert not validate_dna('CCxGGAAGAGCTTACTTAGccggaagagcttacttag')
# ------------------------------------------------------------------------------
def base_pair(c):
"""
Return the corresponding character (lowercase)
of the base pair. If the base is not recognized,
return 'unknown'.
"""
return None
def test_base_pair():
assert base_pair('a') == 't'
assert base_pair('t') == 'a'
assert base_pair('c') == 'g'
assert base_pair('g') == 'c'
assert base_pair('A') == 't'
assert base_pair('T') == 'a'
assert base_pair('C') == 'g'
assert base_pair('G') == 'c'
assert base_pair('x') == 'unknown'
assert base_pair('foo') == 'unknown'
# ------------------------------------------------------------------------------
def transcribe_dna_to_rna(s):
"""
Return string s with each letter T replaced by U.
Result is always uppercase.
"""
return None
def test_transcribe_dna_to_rna():
dna = 'CCGGAAGAGCTTACTTAGccggaagagcttacttag'
assert transcribe_dna_to_rna(dna) == 'CCGGAAGAGCUUACUUAGCCGGAAGAGCUUACUUAG'
# ------------------------------------------------------------------------------
def get_complement(s):
"""
Return the DNA complement in uppercase
(A -> T, T-> A, C -> G, G-> C).
"""
return None
def test_get_complement():
assert get_complement('CCGGAAGAGCTTACTTAG') == 'GGCCTTCTCGAATGAATC'
assert get_complement('ccggaagagcttacttag') == 'GGCCTTCTCGAATGAATC'
# ------------------------------------------------------------------------------
def get_reverse_complement(s):
"""
Return the reverse complement of string s
(complement reversed in order).
"""
return None
def test_get_reverse_complement():
assert get_reverse_complement('CCGGAAGAGCTTACTTAG') == 'CTAAGTAAGCTCTTCCGG'
assert get_reverse_complement('ccggaagagcttacttag') == 'CTAAGTAAGCTCTTCCGG'
# ------------------------------------------------------------------------------
def remove_substring(substring, string):
"""
Returns string with all occurrences of substring removed.
"""
return None
def test_remove_substring():
assert remove_substring('GAA', 'CCGGAAGAGCTTACTTAG') == 'CCGGAGCTTACTTAG'
assert remove_substring('CCG', 'CCGGAAGAGCTTACTTAG') == 'GAAGAGCTTACTTAG'
assert remove_substring('TAG', 'CCGGAAGAGCTTACTTAG') == 'CCGGAAGAGCTTACT'
assert remove_substring('GAA', 'GAAGAAGAA') == ''
# ------------------------------------------------------------------------------
def get_position_indices(triplet, dna):
"""
Returns list of position indices for a specific triplet (3-mer)
in a DNA sequence. We start counting from 0
and jump by 3 characters from one position to the next.
"""
return None
def test_get_position_indices():
assert get_position_indices('GAA', 'CCGGAAGAGCTTACTTAG') == [1]
assert get_position_indices('GAA', 'CCGGAAGAGCTTACTTAGGAAGAA') == [1, 6, 7]
# ------------------------------------------------------------------------------
def get_3mer_usage_chart(s):
"""
This routine implements a 'sliding window'
and extracts all possible consecutive 3-mers.
It counts how often they appear and returns
a list of tuples with (name, occurrence).
The list is alphabetically sorted by the name
of the 3-mer.
"""
return None
def test_get_3mer_usage_chart():
s = 'CCGGAAGAGCTTACTTAGGAAGAA'
result = []
result.append(('AAG', 2))
result.append(('ACT', 1))
result.append(('AGA', 2))
result.append(('AGC', 1))
result.append(('AGG', 1))
result.append(('CCG', 1))
result.append(('CGG', 1))
result.append(('CTT', 2))
result.append(('GAA', 3))
result.append(('GAG', 1))
result.append(('GCT', 1))
result.append(('GGA', 2))
result.append(('TAC', 1))
result.append(('TAG', 1))
result.append(('TTA', 2))
assert get_3mer_usage_chart(s) == result
# ------------------------------------------------------------------------------
def read_column(file_name, column_number):
"""
Reads column column_number from file file_name
and returns the values as floats in a list.
"""
return None
def test_read_column():
import tempfile
import os
text = """1 0.1 0.001
2 0.2 0.002
3 0.3 0.003
4 0.4 0.004
5 0.5 0.005
6 0.6 0.006"""
# we save this text to a temporary file
file_name = tempfile.mkstemp()[1]
with open(file_name, 'w') as f:
f.write(text)
# and now we pass the file name to the function which will read the column
assert read_column(file_name, 2) == [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
# we remove the temporary file
os.unlink(file_name)
# ------------------------------------------------------------------------------
def character_statistics(file_name):
"""
Reads text from file file_name, then
lowercases the text, and then returns
a tuple (x, y), where x is the most abundant
and y is the least abundant but present character found.
Use the isalpha() method to figure out
whether the character is in the alphabet.
"""
return None
def test_character_statistics():
import tempfile
import os
text = """
To be, or not to be: that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,
And by opposing end them? To die: to sleep;
No more; and by a sleep to say we end
The heart-ache and the thousand natural shocks
That flesh is heir to, 'tis a consummation
Devoutly to be wish'd. To die, to sleep;
To sleep: perchance to dream: ay, there's the rub;
For in that sleep of death what dreams may come
When we have shuffled off this mortal coil,
Must give us pause: there's the respect
That makes calamity of so long life;
For who would bear the whips and scorns of time,
The oppressor's wrong, the proud man's contumely,
The pangs of despised love, the law's delay,
The insolence of office and the spurns
That patient merit of the unworthy takes,
When he himself might his quietus make
With a bare bodkin? who would fardels bear,
To grunt and sweat under a weary life,
But that the dread of something after death,
The undiscover'd country from whose bourn
No traveller returns, puzzzles the will
And makes us rather bear those ills we have
Than fly to others that we know not of?
Thus conscience does make cowards of us all;
And thus the native hue of resolution
Is sicklied o'er with the pale cast of thought,
And enterprises of great pith and moment
With this regard their currents turn awry,
And lose the name of action.--Soft you now!
The fair Ophelia! Nymph, in thy orisons
Be all my sins remember'd."""
# we save this text to a temporary file
file_name = tempfile.mkstemp()[1]
with open(file_name, 'w') as f:
f.write(text)
# and now we pass the file name to the function which will get the stats
(most_abundant, least_abundant) = character_statistics(file_name)
assert (most_abundant, least_abundant) == ('e', 'q')
# we remove the temporary file
os.unlink(file_name)
# ------------------------------------------------------------------------------
def pythagorean_triples(n):
"""
Returns list of all unique pythagorean triples
(a, b, c) where a < b < c <= n.
"""
l = []
# loop over all a < b < c <= n
for c in range(1, n + 1):
for b in range(1, c):
for a in range(1, b):
if a*a + b*b == c*c:
l.append((a, b, c))
return l
# ------------------------------------------------------------------------------
def test_pythagorean_triples():
pass # so far we do not test anything, check also test coverage