-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhashingStrategies.py
196 lines (159 loc) · 5.5 KB
/
hashingStrategies.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import os
class HashLP:
def randomPrime(self, upper):
'''
Generates a random prime number for hash_2 function.
'''
import random
prime = []
for num in range(upper + 1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
prime.append(num)
return random.choice(prime)
def __init__(self, hashsize=10) -> None:
'''
Initialises hashtable size and prime number.
'''
self._hashsize = hashsize
self._hashtable = [-1] * self._hashsize
self._size = 0
self._q = self.randomPrime(self._hashsize)
print(f'[ NOTE: Prime Number chosen: {self._q} ]')
def isfull(self):
'''
Returns True if Hash table is full, otherwise False.
'''
return self._size == self._hashsize
def isempty(self):
'''
Returns True if Hash table is empty, otherwise False.
'''
return self._size == 0
def hash_1(self, key):
'''
Returns Hash value using simple Mod operator.
'''
return key % self._hashsize
def hash_2(self, key):
'''
Returns Hash value using prime number.
'''
return self._q - (key % self._q)
def findnext(self, index_h1, index_h2, method='linear', factor=1):
'''
This function is used to calculate the next index position based on
following parameters if the collision occurs.
{parameters} : 'index_h1' -- Hash value by hash_1 function
'index_h2' -- Hash value by hash_2 function
'linear' -- Linear Probing
'quad' -- Quadratic Probing
'double' -- Double Hashing
{returns} : Returns index when collison occurs.
'''
if method == 'linear':
return (index_h1 + factor) % self._hashsize
elif method == 'quad':
return (index_h1 + factor ** 2) % self._hashsize
elif method == 'double':
return (index_h1 + (factor * index_h2)) % self._hashsize
def insert(self, element, method='linear'):
'''
Inserts element in hashtable using the passed Method.
'''
if self.isfull():
print('Hash Table is Full !')
return False
position = index_h1 = self.hash_1(element)
index_h2 = self.hash_2(element)
n = 0
while self._hashtable[position] != -1:
n += 1
position = self.findnext(
index_h1, index_h2, method=method, factor=n)
self._hashtable[position] = element
self._size += 1
return True
def search(self, key, method='linear'):
'''
Returns index position if the element is found in the table, else False.
'''
if self.isempty():
print('Hashtable is empty')
else:
position = index_h1 = self.hash_1(key)
index_h2 = self.hash_2(key)
n = 0
while True:
if self._hashtable[position] == key:
return position
else:
n += 1
position = self.findnext(
index_h1, index_h2, method=method, factor=n)
if position == index_h1 - 1:
break
return
def display(self):
'''
Utility funtion to display Hashtable.
'''
if self.isempty():
print('Hashtable is empty')
else:
for i, element in enumerate(self._hashtable):
print(f'[{i}] -- {element}')
###############################################################################
def method():
'''
Prints different methods for selecting a Hashing strategy.
'''
methods = ['Linear Probing', 'Quadratic Probing', 'Double Hashing']
method_arg = ['linear', 'quad', 'double']
print("Chose a Method for collision case:")
for i, method in enumerate(methods):
print(f'{i + 1}. {method}')
method = int(input("Enter choice: "))
method = method_arg[method - 1]
return method
def options(method):
'''
Prints Menu for operations
'''
options_list = ['Insert Item', 'Search', 'Display Hashtable', 'Exit']
print(f"MENU for Hash Table using '{method}' strategy.")
for i, option in enumerate(options_list):
print(f'{i + 1}. {option}')
choice = int(input("Enter choice: "))
return method, choice
def switch_case(method, choice):
'''
Switch Case for operations
'''
if choice == 1:
elem = int(input("Enter item: "))
print("Added item.") if H.insert(
elem, method=method) else print('Item cannot be added.')
elif choice == 2:
elem = int(input("Enter item to search: "))
result = H.search(elem, method=method)
print(f'Found element at {result}.') if result != None else print(
'Item does not exist.')
elif choice == 3:
print('HASHTABLE')
H.display()
elif choice == 4:
import sys
sys.exit()
###############################################################################
if __name__ == '__main__':
n = int(input('Enter hash table size: '))
H = HashLP(hashsize=n)
method = method()
os.system('cls')
while True:
method, choice = options(method)
switch_case(method, choice)