-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathhash-table.py
More file actions
41 lines (31 loc) · 713 Bytes
/
hash-table.py
File metadata and controls
41 lines (31 loc) · 713 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
30
31
32
33
34
35
36
37
38
39
40
41
# Python implementation of a basic hash table.
hashTable = [[],] * 10
def primeCheck(n):
if n == 1 or n == 0:
return 0
for i in range(2, n//2):
if n % 1 == 0:
return 0
return 1
def primeGet(n):
if n % 2 == 0:
n += 1
while not primeCheck(n):
n += 2
return n
def hashFn(key):
capacity = primeGet(10)
return key % capacity
def insertData(key, data):
index = hashFn(key)
hashTable[index] = [key, data]
def removeData(key):
index = hashFn(key)
hashTable[index] = 0
insertData(123, "test1")
insertData(432, "test2")
insertData(213, "test3")
insertData(654, "test4")
print(hashTable)
removeData(123)
print(hashTable)