-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable.py
More file actions
114 lines (89 loc) · 3.16 KB
/
Copy pathhashtable.py
File metadata and controls
114 lines (89 loc) · 3.16 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
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
107
108
109
110
111
112
113
114
from data_structures.linked_list import LinkedList
class Hashtable:
"""
This hash table is for storing and retrieving key-value pairs. It uses an array of linked lists or none (buckets) to store the pairs (drops).
The number of buckets in the hash table is set to 1024.
"""
def __init__(self, size=1024):
"""
initializes the hash table
"""
self._size = size
self._buckets = [None] * size
def set(self, key, value):
"""
inserts or updates a key-value pair in the hash table
"""
index = self._hash(key)
bucket = self._buckets[index]
if bucket is None:
bucket = LinkedList()
self._buckets[index] = bucket
current = bucket.head
while current:
candidate_drop = current.value
# checking to see if the specific key is already there in one of the buckets
if candidate_drop[0] == key:
# if so, update the value instead of adding a new drop at the head node.
candidate_drop[1] = value
return
current = current.next
# if the specific key isn't found, add the drop aka key-value pair
drop = [key, value]
bucket.insert(drop)
def get(self, key):
"""
gets the value of a key from the hash table, or None if the key isn't there
"""
index = self._hash(key)
bucket = self._buckets[index]
if bucket is None:
return None
current = bucket.head
while current:
drop = current.value
if drop[0] == key:
return drop[1]
############### check this line
current = current.next
return None
def keys(self):
"""
rturns a list of all the keys stored in the hash table
"""
key_list = []
for bucket in self._buckets: # list of LinkedLists (or None)
if bucket: # LinkedList
current = bucket.head # Node
while current:
drop = current.value # List of 2 items [key, value]
key_list.append(drop[0])
current = current.next
return key_list
def has(self, key):
"""
checks if a given key is in the hash table and returns boolean true or false
"""
for bucket in self._buckets:
if bucket:
current = bucket.head
while current:
drop = current.value
if drop[0] == key:
return True
current = current.next
return False
def _hash(self, key):
"""
private method that computes the hash value of a given key by:
Add all the ASCII values together.
Multiply it by a prime number such as 599.
Use modulo to get the remainder of the result, when divided by the total size of the array.
"""
index = 0
key_str = str(key)
for char in key_str:
index += ord(char)
index *= 599
index = index % self._size
return index