-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathexample-hashmap.py
52 lines (36 loc) · 983 Bytes
/
example-hashmap.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
#hashtable
class hashmap:
def __init__(self):
self.size = 20
self.ht = [[] for _ in range(0, self.size)]
def set(self,key,data):
hashed_key = hash(key) % self.size
buckets = self.ht[hashed_key]
exist = False
for i , kv in enumerate(buckets):
k,v = kv
if key == k:
exist = True
break
if exist:
buckets[i] = ((key,data))
else:
buckets.append((key,data))
def get(self,key):
hashed_key = hash(key) % self.size
buckets = self.ht[hashed_key]
found = False
for i , kv in enumerate(buckets):
k,v = kv
if key == k:
found = True
break
if found:
return key
else:
print("not found")
h = hashmap()
h.set("big_house", "martell")
h.set("med_house", "tony")
h.set("small_house", "emma")
print(h.ht)