-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflatassociativearray.py
106 lines (86 loc) · 2.58 KB
/
flatassociativearray.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
class FlatAssociativeArray:
def __init__(self, array):
"""Inits from array to histogram"""
self.data_list = []
self.keys = []
if array:
for item in array:
if not self.search(item):
self.insert(item)
else:
self.update(item)
self.keys = self.getKeys()
def insert(self, data):
"""Appends data to end of list,
updates keys with data
Params: data - key of thing to add
str -> ()
*Should only be called if data not in data_list
"""
item = (data, 1)
self.data_list.append(item)
self.keys = self.getKeys()
def update(self, data):
"""Increases count of data
Params: data - key of elem to update
str -> ()
* if data in keys, increases count
"""
index = self.getIndex(data)
self.data_list[index] = (self.getKey(index), self.getValue(index) + 1)
def remove(self, data):
"""Removes given tuple
Params: data - elem to remove
(str, int) -> ()
"""
key = data[0]
index = self.getIndex(key)
self.data_list.remove(index)
def search(self, key):
"""Returns (key, val) item given key
Params: key - key of data to retrive
str -> (str, int)
"""
index = self.getIndex(key)
if index:
return self.data_list[index]
return None
def getIndex(self, key):
"""Returns index of key | O(n)
Params - key - key of data to retrive
str -> int
"""
keys = self.keys
for i in range(len(keys)):
if key == keys[i]:
return i
def getKey(self, index):
"""Returns key at index
int -> str
"""
return self.data_list[index][0]
def getValue(self, key):
"""Returns value of key (FREQUENCY)
str -> int
"""
index = self.getIndex(key)
if index:
return self.data_list[index][1]
def getKeys(self):
"""Returns all keys in data_list
() -> array
"""
keys = []
for item in self.data_list:
keys.append(item[0])
return keys
def __iter__(self):
"""Required for iterable"""
for item in self.data_list:
yield item
def __getitem__(self, index):
"""Required for iterable"""
return self.search(index)
def __name__(self):
"""Required for timeit benchmark"""
return "FlatAssociativeArray"