-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable.py
49 lines (41 loc) · 1.37 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
class HashTable:
def __init__(self, size):
self.lst = [None for i in range(0, size)]
self.size = size
def hashFunction(self, string):
hashValue = 0
for char in string:
hashValue += ord(char)
hashValue = (hashValue * ord(char)) % self.size
return hashValue
def insertEntry(self, string):
if string is not None:
hashedString = self.hashFunction(string)
self.lst[hashedString] = string
return True
return False
def printHashTable(self):
for i in range(0, len(self.lst)):
print(f"{i} - {self.lst[i]}")
def findEntry(self, string):
hashedString = self.hashFunction(string)
if self.lst[hashedString] is not None:
return True
else:
return False
def deleteEntry(self, string):
hashedString = self.hashFunction(string)
if self.lst[hashedString] is not None:
self.lst[hashedString] = None
return True
else:
return False
if __name__ == "__main__":
hashtable = HashTable(10)
hashtable.insertEntry("Leonardo")
hashtable.insertEntry("John")
hashtable.printHashTable()
print(hashtable.findEntry("Leonardo"))
print(hashtable.findEntry("Doe"))
print(hashtable.deleteEntry("John"))
hashtable.printHashTable()