-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_operations.py
245 lines (160 loc) · 5.17 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
# 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.
#
# 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.
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.
# 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.
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 the 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 the 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 sixth, fifth, fourth, and third elements from the end of the
list, in that order.
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 the 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
def replace_third_and_last(input_list):
"""
Replace the third and last elements of the input list with the value 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], input_list[-1] = 37, 37
# input_list[2] = input_list[-1] = 37
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. 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]
def delete_third_and_seventh(input_list):
"""
Remove the third and seventh elements of the 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]
def delete_middle(input_list):
"""
Remove all elements from the input list except for the first two and the
last two. Return nothing.
For example:
>>> notes = ['Do', 'Re', 'Mi', 'Fa', 'So', 'La', 'Ti', 'Do']
>>> delete_middle(notes)
>>> notes == ['Do', 'Re', 'Ti', 'Do']
True
"""
del input_list[2:-2]
##############################################################################
# END OF MAIN EXERCISE. Yay! You did it! You Rock!
#
# Please ask for a code review from an instructor/TA before proceeding.
if __name__ == "__main__":
import doctest
result = doctest.testmod()
if result.failed == 0:
print "ALL TESTS PASSED"