-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_operations.py
333 lines (247 loc) · 8.56 KB
/
list_operations.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
# Part 1: Fundamental operations on lists
# ---------------------------------------
#
# The fundamental operations on lists in Python are those that are part of the
# language syntax and/or cannot be implemented in terms of other list operations:
#
# * List literals ([], ['hello'], [3, 1, 4, 1, 5, 9], etc.)
# * List indexing (some_list[index])
# * List indexing assignment (some_list[index] = value)
# * List slicing (some_list[start:end])
# * List slicing assignment (some_list[start:end] = another_list)
# * List index deletion (del some_list[index])
# * List slicing deletion (del some_list[start:end])
#
# In this section you will implement functions that each use just one of the above
# operations. The docstring of each function describes what it should do. Consult
# test_list_operations.py for concrete examples of the expected function behavior.
#
# DO NOT USE ANY OF THE BUILT IN LIST METHODS, OR len(l)
def head(input_list):
"""
Return the first element of the input list.
[ A, B, C, D, E, F ] --> A
"""
return input_list[0]
def tail(input_list):
"""
Return all elements of the input list except the first.
[ A, B, C, D ] --> [ B, C, D ]
"""
return input_list[1:]
def last(input_list):
"""
Return the last element of the input list.
[ A, B, C, D ] --> D
"""
return input_list[-1]
def init(input_list):
"""
Return all elements of the input list except the last.
[ A, B, C, D ] --> [ A, B, C ]
"""
return input_list[:-1]
##############################################################################
# Do yourself a favor and get a short code review here.
# You can also get reviewed by a neighbor who has been reviewed.
def first_three(input_list):
"""
Return the first three elements of the input list.
[ A, B, C, D, E, F ] --> [ A, B, C ]
"""
return input_list[0:3]
def last_five(input_list):
"""
Return the last five elements of the input list.
[ A, B, C, D, E, F ] --> [ B, C, D, E, F ]
"""
return input_list[-5:]
def middle(input_list):
"""
Return all elements of the input list except the first two and the last two.
[ A, B, C, D, E, F ] --> [ C, D ]
"""
return input_list[2:-2]
def inner_four(input_list):
"""
Return the third, fourth, fifth, and sixth elements of the input list.
[ A, B, C, D, E, F, G ] --> [ C, D, E, F ]
"""
return input_list[2:6]
def inner_four_end(input_list):
"""
Return the sixth, fifth, fourth, and third elements from the end of the
list, in that order.
[ A, B, C, D, E, F, G, H, I, J, K, L] --> [ G, H, I, J ]
"""
return input_list[-6:-2]
def replace_head(input_list):
"""
Replace the head of the input list with the value 42.
[ A, B, C, D ] --> [ 42, B, C, D]
"""
input_list[0] = 42
return input_list
def replace_third_and_last(input_list):
"""
Replace the third and last elements of the input list with the value 37.
[ A, B, C, D, E, F ] --> [ A, B, 37, D, E, 37 ]
"""
input_list[2] = input_list[-1] = 37
return input_list
def replace_middle(input_list):
"""
Replace all elements of the input list with the the values 42 and 37, in
that order, except for the first two and last two elements.
[ A, B, C, D, E, F, G, H, I ] --> [ A, B, 42, 37, H, I ]
"""
input_list[2:-2] = [42, 37]
return input_list
def delete_third_and_seventh(input_list):
"""
Remove the third and seventh elements of the input list.
[ A, B, C, D, E, F, G, H ] --> [ A, B, D, E, F, H ]
"""
del input_list[2]
del input_list[5]
return input_list
def delete_middle(input_list):
"""
Remove all elements from the input list except for the first two and the
last two.
[ A, B, C, D, E, F, G, H ] --> [ A, B, G, H ]
"""
del input_list[2:-2]
return input_list
##############################################################################
# END OF MAIN EXERCISE
#
# Please ask for a code review from an instructor/TA before proceeding.
# Further Study / Extra Credit
# ----------------------------
#
# In this section you will implement your own versions of the standard list methods.
# You should use only the primitive operations from Part 1 and 2 in your implementations.
# For loops are also allowed, such as the following:
# for element in some_list:
# # Do something with element
#
# Each custom method imitates a built-in list method, as described by the docstring
# for each function. Play with the built-in methods in the Python REPL to get a feel
# for how they work before trying to write your custom version. You may also look at
# the test_list_operations.py file for concrete examples of expected behavior.
def custom_len(input_list):
"""
like len(input_list), should return the number of items in the list
"""
count = 0
for i in input_list:
count = count + 1
return count
# For the next four exercises, you'll need to be clever and think about ways
# to use "list slice assignment" to solve these problems.
#
# NOTE: these are especially contrived--for example, you wouldn't really want
# to typically append things to a list like this (you'd want to to use the
# list.append() method), but we want you to practice list slicing assignment
# in different ways so it sticks in your brain.
def custom_append(input_list, value):
"""
like input_list.append(value), should add the value to the end of the list
and return nothing
"""
list_length = custom_len(input_list)
input_list[list_length:list_length] = [value]
return
def custom_extend(input_list, second_list):
"""a
like input_list.extend(second_list), should append every item in the second
list to the end of the first list and return nothing
"""
list_length = custom_len(input_list)
input_list[list_length:list_length] = second_list
return
def custom_insert(input_list, index, value):
"""
like input_list.insert(index, value), should insert (not replace) the value
at the specified index of the input list and return nothing
"""
input_list[index:index] = [value]
return
def custom_remove(input_list, value):
"""
like input_list.remove(value), should remove the first item of the
value specified and return nothing
"""
counter = 0
for i in input_list:
if i == value:
input_list[counter:counter+1] = []
break
counter += 1
def custom_pop(input_list):
"""
like input_list.pop(), should remove the last item in the list and
return it
"""
last_value = input_list[-1]
input_list[-1:] = []
return last_value
def custom_index(input_list, value):
"""
like input_list.index(value), should return the index of the first item
which matches the specified value
"""
count = 0
for i in input_list:
if i == value:
return count
break
count = count + 1
def custom_count(input_list, value):
"""
like input_list.count(value), should return the number of times the specified
value appears in the list.
"""
count = 0
for i in input_list:
if i == value:
count = count + 1
return count
def custom_reverse(input_list):
"""
like input_list.reverse(), should reverse the elements of the original list
and return nothing (we call this reversing "in place")
"""
swap_number = custom_len(input_list) / 2
for i in range(swap_number):
current_n = input_list[i]
current_neg_n = input_list[(i + 1) * -1]
input_list[i] = current_neg_n
input_list[(i+1)*-1] = current_n
def custom_contains(input_list, value):
"""
like (value in input_list), should return True if the list contains the
specified value and False if it does not. Remember, do not use the `in`
statement -- find another way to solve it!
"""
for i in input_list:
if i == value:
return True
return False
def custom_equality(some_list, another_list):
"""
like (some_list == another_list), should return True if both lists contain
the same values in the same indexes
"""
if custom_len(some_list) != custom_len(another_list):
return False
else:
for i in range(len(some_list)):
if some_list[i] != another_list[i]:
return False
return True
##############################################################################
# END OF EXTRA CREDIT
#
# Please ask for a code review. Also, give your partner a high-five!