-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_tables.py
50 lines (40 loc) · 956 Bytes
/
hash_tables.py
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
#These are basically dictonaries in Python
list_ = [3,2,9,11,7]
list_len = len(list_)
def hash_key(key, list_len):
return key % list_len
'''
newList = []
for i in range(list_len):
newList.insert(hash_key(list_[i], list_len), list_[i])
#print(f'Hash value for {list_[i]} is {hash_key(list_[i], list_len)}')
print(newList, ':new')
print(list_, ':odd')
'''
hashTable = [[],] * 10
def checkPrime(n):
if n == 1 or n == 0:
return 0
for i in range(2, n//2):
if n % i == 0:
return 0
return 1
def getPrime(n):
if n % 2 == 0:
n += 1
while not checkPrime(n):
n +=2
return n
def hashFunction(key):
capacity = getPrime(10)
return key % capacity
def insertData(key, data):
index = hashFunction(key)
hashTable[index] = [key, data]
def removeData(key):
index = hashFunction(key)
hashTable[index] = 0
insertData(1, 'Apple')
insertData(0, 'Acute bean')
insertData(5, ' ')
print(hashTable)