-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable.py
57 lines (43 loc) · 1.35 KB
/
hashtable.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
"""
LookUp by key -----> O(1)
Insertion/Deletion -----> O(1)
Classes to implement HashTable in different languages:
Python :: dictionary
Java :: HashMap
Java :: Linked HashMap
C++ :: std::map
"""
class HashTable():
"""
In class hashTable, an array of size '100' is created which is initialised using list_comprehension; val in each
element is None
"""
def __init__(self):
self.MAX = 100 # size of array = 100
self.arr = [None for i in range(self.MAX)] # list-comprehension
def get_hash(self, key):
h = 0
for char in key:
h += ord(char) # returns ASCII val of char
return h%self.MAX # hash: Sum(ASCII_Values(key)) % size(array)
def add(self, key, val):
h = self.get_hash(key) # retrieving hash function
self.arr[h] = val
def get(self, key):
h = self.get_hash(key)
return self.arr[h]
def delete(self, key):
h = self.get_hash(key)
self.arr[h] = None
t = HashTable() # object of class HashTable
q = t.add('Google', 120)
r = t.get('Google')
# s = t.arr # to get full array
# m = t.delete('Google') # delete element by key
# n = t.arr # to get full array after deleting
print(r)
# todo Collision Handling
# Collision Handling: When one or more elements are assigned the same hash
s = t.add('Google', 121)
z = t.get('Google')
print(z)