-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable.py
More file actions
47 lines (39 loc) · 1.06 KB
/
hashtable.py
File metadata and controls
47 lines (39 loc) · 1.06 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
def calc_hash(data):
"""Полинимиальный хеш"""
k = 3571
s = 0
i = 1
data += 84832941
while data > 0:
s += data % 2 * k ** i
i += 1
data //= 2
return s % 2 ** 8
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
def add(self, element):
if not self.search(element):
node = [element, None]
if self.head is None:
self.head = node
else:
self.tail[1] = node
self.tail = node
def search(self, element):
curr = self.head
while curr is not None:
if curr[0] == element:
return True
curr = curr[1]
return False
class HashTabl:
def __init__(self):
self.table = [LinkedList() for _ in range(256)]
def add(self, element):
hsh = calc_hash(element)
self.table[hsh].add(element)
def search(self, element):
hsh = calc_hash(element)
return self.table[hsh].search(element)