-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_table.py
More file actions
109 lines (95 loc) · 3.59 KB
/
Copy pathhash_table.py
File metadata and controls
109 lines (95 loc) · 3.59 KB
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
class HashTable:
"""
The HashTable class
"""
def __init__(self) -> None:
self.collection = {}
def hash(self, string: str) -> int:
# This function calculates the hash value of a given string
# by summing the ASCII values of its characters.
hash_list = [ord(char) for char in string]
return sum(hash_list)
def add(self, key: str, value: str) -> None:
# This function adds a key-value pair to the hash table.
hash_val = self.hash(key)
if hash_val in self.collection:
self.collection[hash_val][key] = value
return
self.collection[hash_val] = {key: value}
def remove(self, key: str) -> None:
# This function removes a key-value pair from the hash table.
hash_val = self.hash(key)
if hash_val in self.collection and key in self.collection[hash_val]:
del self.collection[hash_val][key]
def lookup(self, key: str):
# This function looks up a value in the hash table by its key.
hash_val = self.hash(key)
if hash_val in self.collection and key in self.collection[hash_val]:
return self.collection[hash_val][key]
return None
def __str__(self):
# This function returns a string representation of the hash table.
return str(self.collection)
def end_program_statement():
# This function is called when the user wants to exit the program.
print("\nExiting the program.")
exit()
def menu():
# This function is called when the user wants to see the menu again.
print(
'\nFind hash value of a string ⇾ 1\n'+
'Add key-value pair to collection ⇾ 2\n'+
'Remove key-value pair ⇾ 3\n'+
'Lookup value in collection by key ⇾ 4\n'
'View collection ⇾ 5\n'+
'Type "q", "quit", "exit" or Ctrl+C to exit the program.'
)
def menu_reminder():
print('Type 6 to see the menu again.')
# The program starts here.
print('<<< HASH TABLE >>>\n')
print(
'This is a simple implementation of a hash table in Python.\n'+
'The hash table uses a simple hash function that sums the ASCII values of the characters in the key.\n'+
'The hash table uses chaining to handle collisions.\n'+
'The hash table supports adding, removing, and looking up key-value pairs.\n')
hash_table = HashTable()
menu()
while True:
try:
inp = input("\nEnter your choice (1-6): ").strip()
except KeyboardInterrupt:
end_program_statement()
if inp.lower() in ['q', 'quit', 'exit']:
end_program_statement()
if inp == '1':
string = input("Enter a string to find its hash value: ").strip()
print(f"The hash value of '{string}' is: {hash_table.hash(string)}")
menu_reminder()
elif inp == '2':
key = input("Enter a key: ").strip()
value = input("Enter a value: ").strip()
hash_table.add(key, value)
print(f"Added key-value pair: {key}: {value}")
menu_reminder()
elif inp == '3':
key = input("Enter a key to remove: ").strip()
hash_table.remove(key)
print(f"Removed key: {key}")
menu_reminder()
elif inp == '4':
key = input("Enter a key to lookup: ").strip()
value = hash_table.lookup(key)
if value is not None:
print(f"The value for key '{key}' is: {value}")
else:
print(f"Key '{key}' not found.")
menu_reminder()
elif inp == '5':
print(f"Collection: {hash_table}")
menu_reminder()
elif inp == '6':
menu()
else:
print("Invalid choice. Please try again.")
menu_reminder()