-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_table_oop.py
More file actions
29 lines (25 loc) · 806 Bytes
/
hash_table_oop.py
File metadata and controls
29 lines (25 loc) · 806 Bytes
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
class HashTable:
def __init__(self, max_size):
self.max_size = max_size
self.table = [None]*max_size
def hash(self, value):
return value % self.max_size
def insert(self, value):
count = 0
x = self.hash(value)
while self.table[x] != None:
if count == self.max_size:
return "cannot insert"
x = (x + 1) % self.max_size
count += 1
self.table[x] = value
def search(self, value):
x = self.hash(value)
initial = x
while self.table[x] != value:
if self.table[x] == None:
return "not found"
x = (x + 1) % self.max_size
if x == initial:
return "not found"
return self.table[x]