-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparsearray.py
More file actions
30 lines (24 loc) · 945 Bytes
/
Copy pathsparsearray.py
File metadata and controls
30 lines (24 loc) · 945 Bytes
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
class SparseArray:
"""A Sparse Array."""
def __init__(me, arr, size=None):
"""Create dict of (index, value) for values that are not zero."""
me.size = size or len(arr)
me.data = {idx: val for idx, val in enumerate(arr) if val is not 0}
def set(me, i, val):
"""Set a value."""
if i > me.size:
raise IndexError
elif val != 0:
me.data[i] = val
elif val == 0 and i in me.data:
del me.data[i]
def get(me, i):
"""Get a value."""
if not 0 <= i < me.size:
raise IndexError
return me.data.get(i, 0) # returns 0 if key not in dict
arr = [0, 1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 7] + 200 * [0]
sparse = SparseArray(arr, len(arr))
sparse.set(214, 3) # Update value at index 214 to 3
print(f"internal data : {sparse.data}")
print(f"effective values : {[sparse.get(x) for x in range(sparse.size)]}")