-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_operations.py
249 lines (148 loc) · 5.18 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
"""Functions that manipulate lists without using Python's built-in list methods.
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.
They include:
* 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])
Implement functions that each use just one of the above operations.
The docstring of each function describes what it should do.
DO NOT USE ANY OF THE BUILT IN LIST METHODS, OR len()!
"""
def head(input_list):
"""Return the first element of the input list.
For example:
>>> head(['Jan', 'Feb', 'Mar'])
'Jan'
"""
return input_list[0]
def tail(input_list):
"""Return all elements of the input list except the first.
For example:
>>> tail(['Jan', 'Feb', 'Mar'])
['Feb', 'Mar']
"""
return input_list[1:]
def last(input_list):
"""Return the last element of the input list.
For example:
>>> last(['Jan', 'Feb', 'Mar'])
'Mar'
"""
return input_list[-1]
def init(input_list):
"""Return all elements of the input list except the last.
For example:
>>> init(['Jan', 'Feb', 'Mar'])
['Jan', 'Feb']
"""
return input_list[:-1]
##############################################################################
# Do yourself a favor and get a short code review here.
def first_three(input_list):
"""Return the first three elements of the input list.
For example:
>>> first_three(['Jan', 'Feb', 'Mar', 'Apr', 'May'])
['Jan', 'Feb', 'Mar']
"""
return input_list[0:3]
def last_five(input_list):
"""Return the last five elements of the input list.
For example:
>>> last_five([0, 3, 6, 9, 12, 15, 18, 21, 24, 27])
[15, 18, 21, 24, 27]
"""
return input_list[-5:]
def middle(input_list):
"""Return all elements of input_list except the first two and the last two.
For example:
>>> middle([0, 3, 6, 9, 12, 15, 18, 21, 24, 27])
[6, 9, 12, 15, 18, 21]
"""
return input_list[2:-2]
def inner_four(input_list):
"""Return the third, fourth, fifth, and sixth elements of input_list.
For example:
>>> inner_four([0, 3, 6, 9, 12, 15, 18, 21, 24, 27])
[6, 9, 12, 15]
"""
return input_list[2:6]
def inner_four_end(input_list):
"""Return the elements that are 6th, 5th, 4th, and 3rd from the end of input_list.
This function should return those elements in a list, in the exact order
described above.
For example:
>>> inner_four_end([0, 3, 6, 9, 12, 15, 18, 21, 24, 27])
[12, 15, 18, 21]
"""
return input_list[-6:-2]
def replace_head(input_list):
"""Replace the head of input_list with the value 42 and return nothing.
For example:
>>> multiples = [0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
>>> replace_head(multiples)
>>> multiples == [42, 3, 6, 9, 12, 15, 18, 21, 24, 27]
True
"""
input_list[0] = 42
return
def replace_third_and_last(input_list):
"""Replace third and last elements of input_list with 37 and return nothing.
For example:
>>> multiples = [0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
>>> replace_third_and_last(multiples)
>>> multiples == [0, 3, 37, 9, 12, 15, 18, 21, 24, 37]
True
"""
input_list[2] = 37
input_list[-1] = 37
return
def replace_middle(input_list):
"""Replace all elements of a list but the first and last two with 42 and 37.
After the replacement, 42 and 37 should appear in that order in input_list.
Return nothing.
For example:
>>> multiples = [0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
>>> replace_middle(multiples)
>>> multiples == [0, 3, 42, 37, 24, 27]
True
"""
input_list[2:-2] = [42, 37]
return
def delete_third_and_seventh(input_list):
"""Remove third and seventh elements of input_list and return nothing.
For example:
>>> notes = ['Do', 'Re', 'Mi', 'Fa', 'So', 'La', 'Ti', 'Do']
>>> delete_third_and_seventh(notes)
>>> notes == ['Do', 'Re', 'Fa', 'So', 'La', 'Do']
True
"""
del input_list[2]
del input_list[5]
return
def delete_middle(input_list):
"""Remove all elements from input_list except the first two and last two.
Return nothing.
For example:
>>> notes = ['Do', 'Re', 'Mi', 'Fa', 'So', 'La', 'Ti', 'Do']
>>> delete_middle(notes)
>>> notes == ['Do', 'Re', 'Ti', 'Do']
True
"""
while len(input_list) > 4 :
input_list.pop(2)
return
##############################################################################
# END OF MAIN EXERCISE. Yay! You did it! You Rock!
#
# Please ask for a code review from an Education team member before proceeding.
##############################################################################
# This is the part were we actually run the doctests.
if __name__ == "__main__":
import doctest
result = doctest.testmod()
if result.failed == 0:
print "ALL TESTS PASSED"