-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_table.py
99 lines (81 loc) · 3.64 KB
/
hash_table.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
# Author: Matthew Shelbourn | Student ID: 001059665 | mshelbo@wgu.edu | December, 2020
# hash_table.py contains the Class constructors and associated methods for the HashEntry and HashTable classes
# Class constructor for the Hash Table to be used for all packages
# The HashTable class includes the following:
# Methods for: defining the empty table, defining the hash key, adding an item to the table,
# retrieving an item from the table, updating an item in the table, and deleting an item from the table
# The hash table add_item function uses chaining for collision handling
class HashTable:
# Creates the empty hash table with empty arrays (buckets) in each row of table
# The number of rows can be controlled by the MAX variable
# Space-time complexity O(N)
def __init__(self):
self.MAX = 10
self.table = [[] for i in range(self.MAX)]
# Defines the hashing function (private function that is only used by other functions within class)
# Space-time complexity O(1)
def _get_hash(self, key):
return int(key) % len(self.table)
# <----- BEGIN CRUD FUNCTIONALITY ----->
# Function for adding a key/value pair to the hash table
# Space-time complexity O(N)
def create(self, key, val):
hashed_key = self._get_hash(key)
found = False
# Determine if key already exists & update value if it does
# If not found, then a new key/value pair is appended to the list for that hashed key
for i, el in enumerate(self.table[hashed_key]):
if len(el) == 2 and el[0] == key:
self.table[hashed_key][i] = (key, val)
found = True
print('A package with ID ' + str(key) + ' already exists in the table.')
print('The entry for this package has been updated with the new value.')
break
if not found:
self.table[hashed_key].append((key, val))
return
# Function for retrieving entries from the table
# Space-time complexity O(N)
def read(self, key):
hashed_key = self._get_hash(key)
found = False
for el in self.table[hashed_key]:
if el[0] == key:
found = True
entry = el
return el[1]
if not found:
print('Unable to locate a package with the ID:', key)
print('Please double check the ID and try again.')
return None
# Function for updating hash table entries
# Space-time complexity O(N)
def update(self, key, val):
hashed_key = self._get_hash(key)
found = False
for i, el in enumerate(self.table[hashed_key]):
if len(el) == 2 and el[0] == key:
self.table[hashed_key][i] = (key, val)
found = True
break
if not found:
print('Unable to update the package with ID:', key)
print('No package found with ID:', key)
print('Please double check the ID and try again.')
return None
# Function for deleting hash table entries
# Space-time complexity O(N)
def delete(self, key):
hashed_key = self._get_hash(key)
found = False
for i, el in enumerate(self.table[hashed_key]):
if len(el) == 2 and el[0] == key:
found = True
del self.table[hashed_key][i]
break
if not found:
print('Unable to delete the package with ID:', key)
print('No package found with ID:', key)
print('Please double check the ID and try again.')
return None
# <----- END CRUD FUNCTIONALITY ----->