-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice.py
444 lines (308 loc) · 10 KB
/
practice.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
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
"""List Practice
Edit the functions until all of the doctests pass when
you run this file.
"""
def print_list(items):
"""Print each item in the input list.
For example::
>>> print_list([1, 2, 6, 3, 9])
1
2
6
3
9
"""
for item in items:
print item
def long_words(words):
"""Return words in input list that longer than 4 characters.
For example::
>>> long_words(["hello", "a", "b", "hi", "bacon", "bacon"])
['hello', 'bacon', 'bacon']
(If there are duplicates, show both --- notice "bacon" appears
twice in output)
If no words are longer than 4 characters, return an empty list::
>>> long_words(["all", "are", "tiny"])
[]
"""
# longer_words = []
# for word in words:
# if len(word) > 4:
# longer_words.append(word)
# Can use list comprehension here
longer_words = [word for word in words if len(word) > 4]
return longer_words
def n_long_words(words, n):
"""Return words in list longer than `n` characters.
For example::
>>> n_long_words(
... ["hello", "hey", "spam", "spam", "bacon", "bacon"],
... 3
... )
['hello', 'spam', 'spam', 'bacon', 'bacon']
>>> n_long_words(["I", "like", "apples", "bananas", "you"], 5)
['apples', 'bananas']
"""
# n_longer_words = []
# for word in words:
# if len(word) > int(n):
# n_longer_words.append(word)
# Can use list comprehension here
n_longer_words = [word for word in words if len(word) > int(n)]
return n_longer_words
def smallest_int(numbers):
"""Find the smallest integer in a list of integers and return it.
**DO NOT USE** the built-in function `min()`!
For example::
>>> smallest_int([-5, 2, -5, 7])
-5
>>> smallest_int([3, 7, 2, 8, 4])
2
If the input list is empty, return `None`::
>>> smallest_int([]) is None
True
"""
if not numbers:
return None
else:
smallest = numbers[0]
for number in numbers:
if number <= smallest:
smallest = number
else:
continue
return smallest
def largest_int(numbers):
"""Find the largest integer in a list of integers and return it.
**DO NOT USE** the built-in function `max()`!
For example::
>>> largest_int([-5, 2, -5, 7])
7
>>> largest_int([3, 7, 2, 8, 4])
8
If the input list is empty, return None::
>>> largest_int([]) is None
True
"""
if not numbers:
return None
else:
largest = 0
for number in numbers:
if number > largest:
largest = number
return largest
def halvesies(numbers):
"""Return list of numbers from input list, each divided by two.
For example::
>>> halvesies([2, 6, -2])
[1.0, 3.0, -1.0]
If any of the numbers are odd, make sure you don't round off
the half::
>>> halvesies([1, 5])
[0.5, 2.5]
"""
# half_numbers = []
# for number in numbers:
# half_number = float(number / 2.0)
# half_numbers.append(half_number)
half_numbers = [float(number / 2.0) for number in numbers]
return half_numbers
def word_lengths(words):
"""Return the length of words in the input list.
For example::
>>> word_lengths(["hello", "hey", "hello", "spam"])
[5, 3, 5, 4]
"""
# word_lengths = []
# for word in words:
# word_length = len(word)
# word_lengths.append(word_length)
word_lengths = [len(word) for word in words]
return word_lengths
def sum_numbers(numbers):
"""Return the sum of all of the numbers in the list.
Python has a built-in function, `sum()`, which already does
this --- but for this exercise, you should not use it.
For example::
>>> sum_numbers([1, 2, 3, 10])
16
Any empty list should return the sum of zero::
>>> sum_numbers([])
0
"""
total = 0
for number in numbers:
total = total + number
return total
def mult_numbers(numbers):
"""Return product (result of multiplication) of numbers in list.
For example::
>>> mult_numbers([1, 2, 3])
6
Obviously, if there is a zero in input, the product is zero::
>>> mult_numbers([10, 20, 0, 50])
0
As explained at http://en.wikipedia.org/wiki/Empty_product,
if the list is empty, the product should be 1::
>>> mult_numbers([])
1
"""
mult_numbers = 1
for number in numbers:
mult_numbers = mult_numbers * number
return mult_numbers
def join_strings(words):
"""Return a string of all input strings joined together.
Python has a built-in method, `list.join()` --- but for
this exercise, **you should not use it**.
For example::
>>> join_strings(["spam", "spam", "bacon", "balloonicorn"])
'spamspambaconballoonicorn'
For an empty list, you should return an empty string::
>>> join_strings([])
''
"""
joined_string = ''
for word in words:
joined_string = joined_string + word
return joined_string
def average(numbers):
"""Return the average (mean) of the list of numbers given.
For example::
>>> average([2, 4])
3.0
This should handle cases where the result isn't an integer::
>>> average([2, 12, 3])
5.666666666666667
There is no defined answer if the list given is empty;
it's fine if this raises an error when given an empty list.
(Think of the best way to handle an empty input list, though,
a feel free to provide a good solution here.)
"""
total = 0
count = 0
for number in numbers:
total = float(total + number)
count += 1
average = float(total / count)
return average
def join_strings_with_comma(words):
"""Return ['list', 'of', 'words'] like "list, of, words".
For example::
>>> join_strings_with_comma(
... ["Labrador", "Poodle", "French Bulldog"]
... )
'Labrador, Poodle, French Bulldog'
If there's only one thing in the list, it should return just that
thing, of course::
>>> join_strings_with_comma(["Pretzel"])
'Pretzel'
"""
joined_string = ''
for word in words:
if words.index(word) == 0:
joined_string = word
else:
joined_string = joined_string + ', ' + word
return joined_string
def reverse_list(items):
"""Return the input list, reversed.
**Do not use** the python function `reversed()` or the method
`list.reverse()`.
For example::
>>> reverse_list([1, 2, 3])
[3, 2, 1]
>>> reverse_list(["cookies", "love", "I"])
['I', 'love', 'cookies']
You should do this without changing the original list::
>>> orig = ["apple", "berry", "cherry"]
>>> reverse_list(orig)
['cherry', 'berry', 'apple']
>>> orig
['apple', 'berry', 'cherry']
"""
reverse_list = items[::-1]
return reverse_list
def reverse_list_in_place(items):
"""Reverse the input list `in place`.
Reverse the input list given, but do it "in place" --- that is,
do not create a new list and return it, but modify the original
list.
**Do not use** the python function `reversed()` or the method
`list.reverse()`.
For example::
>>> orig = [1, 2, 3]
>>> reverse_list_in_place(orig)
>>> orig
[3, 2, 1]
>>> orig = ["cookies", "love", "I"]
>>> reverse_list_in_place(orig)
>>> orig
['I', 'love', 'cookies']
"""
i = len(items)
for item in items:
moved_item = items[0]
items[i - 1:i] = items[i-1], moved_item
del items[0]
i = i - 1
def duplicates(items):
"""Return list of words from input list which were duplicates.
Return a list of words which are duplicated in the input list.
The returned list should be in ascending order.
For example::
>>> duplicates(
... ["apple", "banana", "banana", "cherry", "apple"]
... )
['apple', 'banana']
>>> duplicates([1, 2, 2, 4, 4, 4, 7])
[2, 4]
You should do this without changing the original list::
>>> orig = ["apple", "apple", "berry"]
>>> duplicates(orig)
['apple']
>>> orig
['apple', 'apple', 'berry']
"""
duplicates = []
for i, item in enumerate(items):
compared_item = item
for j, item in enumerate(items):
if item == compared_item and i != j and item not in duplicates:
duplicates.append(item)
return duplicates
def find_letter_indices(words, letter):
"""Return list of indices where letter appears in each word.
Given a list of words and a letter, return a list of integers
that correspond to the index of the first occurrence of the letter
in that word.
**DO NOT** use the `list.index()` method.
For example::
>>> find_letter_indices(['odd', 'dog', 'who'], 'o')
[0, 1, 2]
("o" is at index 0 in "odd", is at index 1 in "dog", and at
index 2 in "who")
If the letter doesn't occur in one of the words, use `None` for
that word in the output list. For example::
>>> find_letter_indices(['odd', 'dog', 'who', 'jumps'], 'o')
[0, 1, 2, None]
("o" does not appear in "jumps", so the result for that input is
`None`.)
"""
letter_indices = []
for word in words:
if letter in word:
letter_indices.append(words.index(word))
else:
letter_indices.append(None)
return letter_indices
#####################################################################
# END OF PRACTICE: You can ignore everything below.
if __name__ == "__main__":
import doctest
print
result = doctest.testmod()
if not result.failed:
print "ALL TESTS PASSED. GOOD WORK!"
print