-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_maps.py
67 lines (47 loc) · 1.38 KB
/
hash_maps.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
# This is a class used to link data for mod-mail
class HashTable:
# Original: https://www.geeksforgeeks.org/hash-map-in-python/
def __init__(self, size):
"""Constructs a hash table, this is much like a java hash map."""
self.size = size
self.hash_table = self.create_buckets()
def create_buckets(self):
return [[] for _ in range(self.size)]
def set_val(self, key, val):
hashed_key = hash(key) % self.size
bucket = self.hash_table[hashed_key]
found_key = False
for index, record in enumerate(bucket):
record_key = record
if record_key == key:
found_key = True
break
if found_key:
bucket[index] = (key, val)
else:
bucket.append((key, val))
def get_val(self, key):
hashed_key = hash(key) % self.size
bucket = self.hash_table[hashed_key]
found_key = False
for record in enumerate(bucket):
record_key = record
if record_key == key:
found_key = True
break
if found_key:
return record_key
else:
return 'No record found'
def delete_val(self, key):
hashed_key = hash(key) % self.size
bucket = self.hash_table[hashed_key]
found_key = False
for index, record in enumerate(bucket):
record_key = record
if record_key == key:
found_key = True
break
if found_key:
bucket.pop(index)
return