-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_algo.py
73 lines (43 loc) · 1.47 KB
/
hash_algo.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
class AlgoHashTable:
def __init__ (self, size):
self.size = size
self.hash_table = self.create_buckets()
def create_buckets(self):
return [[] for _ in range(self.size)]
def __str__(self):
return "".join(str(item) for item in self.hash_table)
def set_val(self, key, value):
hashed_key = hash(key)%self.size
bucket = self.hash_table[hashed_key]
found_key = False
for index, record in enumerate(bucket):
record_key, record_value = record
if record_key == key:
found_key = True
break
if found_key:
bucket[index] = (key, value)
else:
bucket.append((key, value))
def get_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_value = record
if record_key == key:
found_key = True
break
if found_key:
return record_value
else:
return f"{key} not found"
hash_table = AlgoHashTable(200)
file = open("data.txt")
lines = file.readline().strip().split(':')
for line in file:
key, value = line.split(":")
hash_table.set_val(key, value)
print(hash_table)
print("-"*100)
print(hash_table.get_val("[email protected]"))