-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic.py
More file actions
241 lines (186 loc) · 5.23 KB
/
dynamic.py
File metadata and controls
241 lines (186 loc) · 5.23 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
MAXIMUM_ARRAY_LENGTH = 1024
def Main(operation, args):
if operation == 'DynamicListTest':
return DynamicListTest()
return False
def DynamicListTest():
dynamicList = DynamicList()
added = DynamicAppend(dynamicList, 1)
assert(added)
count = len(dynamicList["packed"][0]["array"])
assert(count == 1)
added = DynamicAppend(dynamicList, 2)
assert(added)
count = len(dynamicList["packed"][0]["array"])
assert(count == 2)
removed = DynamicRemove(dynamicList, 1)
assert(removed)
count = len(dynamicList["packed"][0]["array"])
assert(count == 1)
items = dynamicList["items"]
assert(items == 1)
removed = DynamicRemove(dynamicList, 2)
assert(removed)
count = len(dynamicList["packed"][0]["array"])
assert(count == 0)
items = dynamicList["items"]
assert(items == 0)
return True
#### Dynamic/PackedList ####
def DynamicList():
'''
Creates a new DynamicList
'''
dynamic = {
"packed": [],
"items": 0
}
return dynamic
def DynamicAppend(dynamic, itm):
'''
Appends an item to a packed list or creates a new one.
:param packed: The DynamicList
:param itm: The item to add to the DynamicList
'''
packedArr = dynamic["packed"]
maximum = MAXIMUM_ARRAY_LENGTH * 7 - 6
length = len(packedArr)
for i in range(length):
packed = packedArr[i]
if packed["items"] < maximum:
PackedAppend(packed, itm)
dynamic["packed"][i] = packed
dynamic["items"] += 1
return True
if length < MAXIMUM_ARRAY_LENGTH:
packed = PackedList()
PackedAppend(packed, itm)
packedArr.append(packed)
dynamic["items"] += 1
return True
return False
def DynamicRemove(dynamic, itm):
'''
Removes an item from the DynamicList.
:param packed: The DynamicList
:param itm: The item to remove from the DynamicList
'''
packedArr = dynamic["packed"]
length = len(packedArr)
for i in range(length):
packed = packedArr[i]
if PackedRemove(packed, itm):
dynamic["packed"][i] = packed
dynamic["items"] -= 1
return True
return False
def PackedList():
'''
Creates a new PackedList
'''
packed = {
"array": [],
"items": 0
}
return packed
def PackedAppend(packed, itm):
'''
Appends an item to the PackedList.array or wraps it in a new layer if full.
Increments the PackedList.items count.
:param packed: The PackedList
:param itm: The item to add to the PackedList
'''
array = packed["array"]
length = len(array)
if length == MAXIMUM_ARRAY_LENGTH:
tmp = [array]
tmp.append(itm)
array = tmp
else:
array.append(itm)
packed["array"] = array
packed["items"] += 1
def PackedRemove(packed, itm):
'''
Removes an item from the PackedList.array.
:param packed: The PackedList
:param itm: The item to remove from the PackedList
'''
length = len(packed["array"])
if length == 0:
return False
if not do_swap(packed, itm): # Item not found
return False
if length == 2: # Peel off layer
peel(packed)
else: # Remove last item
packed["array"] = remove_last(length, packed["array"])
packed["items"] -= 1
return True
def peel(packed):
'''
Peels a layer off of the PackedList
:param packed: The PackedList
'''
packed["array"] = packed["array"][0]
def remove_last(length, lst):
'''
Removes the last item from a list.
:param lst: The list to remove the item from
'''
nLst = []
if length > 1:
for i in range(length - 1):
nLst.append(lst[i])
return nLst
def do_swap(packed, itm):
'''
Swaps the last item in the PackedList.array with the item.
:param packed: The PackedList
:param itm: The item to swap
'''
array = packed["array"]
items = packed["items"]
length = len(array)
if length is 0:
return False
if length is 1:
return True
last = array[length - 1]
if last is itm:
return True
layers = get_layers(items)
if do_find(array, length, layers, itm, last):
array[length - 1] = itm
return True
return False
def do_find(array, length, layers, itm, last):
'''
Finds the item in the array and swaps it with the last item
:param array: The PackedList.array
:param length: The length of the PackedList.array
:param layers: The amount of layers in the PackedList.array
:param itm: The item to swap
:param last: The last item in the PackedList.array
'''
for i in range(length):
item = array[i]
if i == 0 and layers > 1:
if do_find(item, len(item), layers - 1, itm, last):
return True
elif item is itm:
array[i] = last
return True
return False
def get_layers(items):
'''
Calculated the amount of layers in the PackedList
:param items: The amount of items in the PackedList
'''
x = items
x -= MAXIMUM_ARRAY_LENGTH
layers = 1
while x > 0:
x -= (MAXIMUM_ARRAY_LENGTH - 1)
layers += 1
return layers