-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.py
297 lines (238 loc) · 6.78 KB
/
List.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
import time
from itertools import chain
from functools import reduce
from inspect import isfunction
class List(list):
def __init__(self, *args):
list.__init__(self)
for item in args:
self.append(item)
def filter(self, func, *args, **kwargs):
changedList = []
for element in self:
if func(element, *args, **kwargs):
changedList.append(element)
return List(*changedList)
def reduce(self, func):
return reduce(func, self)
def add(self, *args):
self.extend(args)
return self
def sum(self, sumFunc = None):
if isfunction(sumFunc):
return sum(sumFunc(item) for item in self)
else:
return sum(el for el in self)
@property
def length(self):
return len(self)
@property
def distinct(self):
from collections import OrderedDict
items = OrderedDict()
for item in self:
if item not in items:
items[item] = 0
return List(*items.keys())
def cat(self, num):
return self[0: num]
def forEach(self, func):
for item in self:
func(item)
@property
def first(self):
return self[0]
@property
def last(self):
return self[-1]
@property
def isEmpty(self):
if self:
return False
return True
def map(self, func, *args, **kwargs):
newList = []
for item in self:
newList.append(func(item, *args, **kwargs))
return List(*newList)
@property
def max(self):
maximum = None
for i in self:
if not maximum or maximum < i:
maximum = i
return maximum
def join(self, delimiter):
return f"{delimiter}".join(
map(str, self)
)
@property
def min(self):
lower = None
for i in self:
if not lower or lower > i:
lower = i
return lower
@property
def zipWithIndex(self):
self = List(*list(enumerate(self)))
return self
def slice(self, start, end):
return List(*self[start: end])
def sortIt(self, key=None, decendingOrder=False):
if not key:
fn = lambda x: x
else:
fn = key
self.sort(key=fn, reverse=decendingOrder)
return List(*self)
def remove(self, *items):
ls = []
for litem in self:
if litem not in items:
ls.append(litem)
return List(*ls)
def removeItemAtIndex(self, index):
if index == 0:
return List(*self[index + 1:])
return List(*self[0: index] + self[index + 1:])
def toDict(self, listOfKeys = None):
if listOfKeys:
return dict(zip(listOfKeys, self))
return dict(zip(range(self.length), self))
if __name__ == '__main__':
def filterLowerThen10(_):
if _ > 10:
return True
def multiplyEachBy10(_):
return _ * 10
def someMappingFuncWithWhichAcceptParameters(_, multiplier=5):
if _ * multiplier > 5:
return True
return False
def someMappingFuncWithArgs(_, multiplier):
if _ * multiplier > 5:
return True
return False
def ifIsGreaterthan(_, n):
return _ > n
# should accept additional function parameters. args
assert (
List(1, 2, 3, 4, 5)
.map(someMappingFuncWithArgs, 2)
) == [False, False, True, True, True]
# should accept additional function parameters. kwargs
assert (
List(1, 2, 3, 4, 5)
.map(someMappingFuncWithWhichAcceptParameters, multiplier=2)
) == [False, False, True, True, True]
assert(
List(10, 20, 30, 40, 50, 60)
.filter(ifIsGreaterthan, 25)
) == [30, 40, 50, 60]
# Should be equal to 610
assert(
List(10, 2, 13, 87, 22, 48)
.sortIt()
.add(10, 8, 33)
.distinct
.filter(filterLowerThen10)
.map(multiplyEachBy10)
.remove(330, 220, 870)
.removeItemAtIndex(4)
.reduce(lambda a, b: a + b)
) == 610
# Should be equal to [(0, 130), (1, 480)]
assert (
List(10, 2, 13, 87, 22, 48)
.sortIt()
.add(10, 8, 33)
.distinct
.filter(filterLowerThen10)
.map(multiplyEachBy10)
.remove(330, 220, 870)
.removeItemAtIndex(4)
.zipWithIndex
) == [(0, 130), (1, 480)]
# should sort and distinct values.
assert (
List(10, 2, 13, 87, 22, 48)
.sortIt()
.add(10, 8, 33)
.distinct
) == [2, 10, 13, 22, 48, 87, 8, 33]
# should distinct values and equal to
assert (
List(10, 2, 13, 87, 22, 48)
.add(10, 8, 33)
.distinct
) == [10, 2, 13, 87, 22, 48, 8, 33]
# should distinct values and equal to
assert (
List(10, 2, 13, 87, 22, 48)
.add(10, 8, 33)
.distinct
.removeItemAtIndex(3)
) == [10, 2, 13, 22, 48, 8, 33]
# should be sort with decending order and distinct values
assert (
List(10, 2, 13, 87, 22, 48)
.add(10, 8, 33)
.sortIt(decendingOrder=True)
.distinct
) == [87, 48, 33, 22, 13, 10, 8, 2]
# length should be 8
assert (
List(10, 2, 13, 87, 22, 48)
.add(10, 8, 33)
.sortIt(decendingOrder=True)
.distinct
.length
) == 8
# length should be 9
assert (
List(10, 2, 13, 87, 22, 48)
.add(10, 8, 33)
.sortIt(decendingOrder=True)
.length
) == 9
# should be join according to the specified parameter.
assert (
List(10, 2, 13, 87, 22, 48)
.add(10, 8, 33)
.sortIt(decendingOrder=True)
.join("*")
) == "87*48*33*22*13*10*10*8*2"
# max value.
assert (
List(10, 2, 13, 87, 22, 48)
.add(10, 8, 33)
.max
) == 87
# min value.
assert (
List(10, 2, 13, 87, 22, 48)
.add(10, 8, 33)
.min
) == 2
# is empty should return false
assert (
List(10, 2, 13, 87, 22, 48)
.add(10, 8, 33)
.isEmpty
) is False
# first item
assert (
List(1, 2, 3)
.first
) == 1
# last item
assert (
List(1, 2, 3)
.last
) == 3
# is empty should return false
assert (
List()
.isEmpty
) is True