-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable.py
More file actions
74 lines (55 loc) · 1.57 KB
/
hashtable.py
File metadata and controls
74 lines (55 loc) · 1.57 KB
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
from linked_list.linked_list import LinkedList
class HashTable:
def __init__(self, size=1024):
self.size = size
self.bucket = [None] * self.size
def hash(self, key):
"""
Argument: Key
Return: Index for key
"""
hash_index = 0
check = str(key)
for char in check:
hash_index += ord(char)
hash_index *= 599
hash_index %= self.size
return hash_index
def add(self, key, value):
"""
Argument: key, value
Returns: Nothing
"""
hash_index = self.hash(key)
if not self.bucket[hash_index]:
self.bucket[hash_index] = LinkedList()
bucket = self.bucket[hash_index]
bucket.insert([key, value])
def get(self, key):
"""
Argument: Key
Return: value associated with key
"""
hash_index = self.hash(key)
if self.bucket[hash_index] is None:
return None
current = self.bucket[hash_index].head
while current:
if current.value[0] == key:
return current.value[1]
current = current.next
return None
def contains(self, key):
"""
Argument: Key
Return: boolean
"""
hash_index = self.hash(key)
if self.bucket[hash_index] is None:
return False
current = self.bucket[hash_index].head
while current:
if current.value[0] == key:
return True
current = current.next
return False