-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist_operations.py
More file actions
271 lines (199 loc) · 7.6 KB
/
list_operations.py
File metadata and controls
271 lines (199 loc) · 7.6 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
"""
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
operations. The docstring of each function describes what it should do. Consult
test_list_operations.py for concrete examples of the expected function behavior.
"""
def head(input_list):
"""Return the first element of the input list."""
return input_list[0]
def tail(input_list):
"""Return all elements of the input list except the first."""
return input_list[1:]
def last(input_list):
"""Return the last element of the input list."""
return input_list[-1]
def init(input_list):
"""Return all elements of the input list except the last."""
return input_list[:-1]
def first_three(input_list):
"""Return the first three elements of the input list."""
return input_list[0:3]
def last_five(input_list):
"""Return the last five elements of the input list."""
return input_list[-5:]
def middle(input_list):
"""Return all elements of the input list except the first two and the last
two.
"""
return input_list[2:-2]
def inner_four(input_list):
"""Return the third, fourth, fifth, and sixth elements of the input list."""
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.
"""
return input_list[-6:-2:1]
def replace_head(input_list):
"""Replace the head of the input list with the value 42."""
value = 42
input_list[0] = value
return input_list[0]
def replace_third_and_last(input_list):
"""Replace the third and last elements of the input list with the value 37."""
value = 37
input_list[2] = value
input_list[-1] = value
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.
"""
value = [42,37]
input_list[2:-2]= value
return input_list
def delete_third_and_seventh(input_list):
"""Remove the third and seventh elements of the input list."""
del input_list[6]
del input_list[2]
return input_list
def delete_middle(input_list):
"""Remove all elements from the input list except for the first two and the
last two.
"""
del input_list[2:-2]
return input_list
"""
Part 2: Derived operations on lists
-----------------------------------
In this section you will implement your own versions of the standard list methods.
You should use only the primitive operations from Part 1 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):
"""custom_len(input_list) imitates len(input_list)"""
count = 0
if input_list != []:
for element in input_list:
#print count
count += 1
return count
# For the next four functions, get clever using slice operations described in the first half
def custom_append(input_list, value):
"""custom_append(input_list, value) imitates input_list.append(value)"""
input_list[custom_len(input_list):] = [value]
return input_list
def custom_extend(input_list, values):
"""custom_extend(input_list, values) imitates input_list.extend(values)"""
input_list[custom_len(input_list):] = values
return input_list
def custom_insert(input_list, index, value):
"""custom_insert(input_list, index, value) imitates
input_list.insert(index, value)
"""
""" one option. not so efficient
length_list = custom_len(input_list)
if length_list == index: # if list is empty or at the end
custom_append(input_list,value)
elif index == 0: # if index is at the beginning
add_value = [value]
input_list[::] = add_value + input_list
else: # if index is the middle of the list
first_half_list = input_list[:index]
second_half_list = input_list[index:]
add_list= [value]
input_list[::] = first_half_list + add_list + second_half_list
return input_list"""
input_list[index:index] = [value]
return input_list
def custom_remove(input_list, value):
"""custom_remove(input_list, value) imitates input_list.remove(value)"""
element_index = 0
if input_list != []:
for element in input_list:
if input_list[element_index] == value:
del input_list[element_index]
break
else:
element_index += 1
return input_list
def custom_pop(input_list):
"""custom_pop(input_list) imitates input_list.pop()"""
if input_list != []:
save_me = input_list[-1]
del input_list[-1]
return save_me
def custom_index(input_list, value):
"""custom_index(input_list, value) imitates input_list.index(value)"""
element_index = 0
if input_list != []:
for el in input_list:
if input_list[element_index] == value:
found_it = element_index
break
elif (custom_len(input_list) - 1) == element_index:
found_it = "This was not found"
else:
element_index += 1
return found_it
def custom_count(input_list, value):
"""custom_count(input_list, value) imitates input_list.count(value)"""
element_index = 0
counter = 0
for element in input_list:
if input_list[element_index] == value:
counter += 1
element_index += 1
else:
element_index += 1
return counter
def custom_reverse(input_list):
"""custom_reverse(input_list) imitates input_list.reverse()"""
"""new_list = []
while input_list != []:
a = custom_pop(input_list)
custom_append(new_list, a)
input_list[::] = new_list
return input_list"""
input_list[::] = input_list[::-1]
return input_list
def custom_contains(input_list, value):
"""custom_contains(input_list, value) imitates (value in input_list)"""
true_or_false = False
for el in input_list:
if el == value:
true_or_false = True
break
return true_or_false
def custom_equality(some_list, another_list):
"""custom_equality(some_list, another_list) imitates
(some_list == another_list)
"""
compare_list = True
index_counter = 0
if custom_len(some_list) != custom_len(another_list):
compare_list = False
else:
while compare_list and (index_counter < custom_len(some_list)):
if some_list[index_counter] == another_list[index_counter]:
index_counter += 1
else:
compare_list = False
return compare_list