-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem_1430_easy.py
47 lines (36 loc) · 1.28 KB
/
Problem_1430_easy.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
"""
This problem was asked by Facebook.
You have a large array with most of the elements as zero.
Use a more space-efficient data structure, SparseArray, that implements the same interface:
init(arr, size): initialize with the original large array and size.
set(i, val): updates index at i with val.
get(i): gets the value at index i
"""
class SparseArray:
def __init__(self, arr, size) -> None:
self.sparse_array = {}
self.size = size
if self.size != len(arr):
raise Exception("There is no match in sizes")
for index, value in enumerate(arr):
if value != 0:
self.sparse_array[index] = value
def set(self, i, val) -> None:
if 0 <= i < self.size:
self.sparse_array[i] = val
else:
raise Exception("index out of bound")
def get(self, i) -> int:
if self.size > i >= 0:
return self.sparse_array.get(i, 0)
else:
raise Exception("index out of bound")
def get_data(self) -> dict:
return self.sparse_array
x_array = [0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 77, 0]
x = SparseArray(x_array, 12)
print("Data: ", x.get_data())
print("Data at index 2: ", x.get(2))
print("Data at index 10: ", x.get(10))
x.set(2, 21)
print("Data: ", x.get_data())