Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create hashmap.py #141

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions hashmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
class HashTable:

# Create empty bucket list of given size
def __init__(self, size):
self.size = size
self.hash_table = self.create_buckets()

def create_buckets(self):
return [[] for _ in range(self.size)]

# Insert values into hash map
def set_val(self, key, val):

# Get the index from the key
# using hash function
hashed_key = hash(key) % self.size

# Get the bucket corresponding to index
bucket = self.hash_table[hashed_key]

found_key = False
for index, record in enumerate(bucket):
record_key, record_val = record

# check if the bucket has same key as
# the key to be inserted
if record_key == key:
found_key = True
break

# If the bucket has same key as the key to be inserted,
# Update the key value
# Otherwise append the new key-value pair to the bucket
if found_key:
bucket[index] = (key, val)
else:
bucket.append((key, val))

# Return searched value with specific key
def get_val(self, key):

# Get the index from the key using
# hash function
hashed_key = hash(key) % self.size

# Get the bucket corresponding to index
bucket = self.hash_table[hashed_key]

found_key = False
for index, record in enumerate(bucket):
record_key, record_val = record

# check if the bucket has same key as
# the key being searched
if record_key == key:
found_key = True
break

# If the bucket has same key as the key being searched,
# Return the value found
# Otherwise indicate there was no record found
if found_key:
return record_val
else:
return "No record found"

# Remove a value with specific key
def delete_val(self, key):

# Get the index from the key using
# hash function
hashed_key = hash(key) % self.size

# Get the bucket corresponding to index
bucket = self.hash_table[hashed_key]

found_key = False
for index, record in enumerate(bucket):
record_key, record_val = record

# check if the bucket has same key as
# the key to be deleted
if record_key == key:
found_key = True
break
if found_key:
bucket.pop(index)
return

# To print the items of hash map
def __str__(self):
return "".join(str(item) for item in self.hash_table)


hash_table = HashTable(50)

# insert some values
hash_table.set_val('[email protected]', 'some value')
print(hash_table)
print()

hash_table.set_val('[email protected]', 'some other value')
print(hash_table)
print()

# search/access a record with key
print(hash_table.get_val('[email protected]'))
print()

# delete or remove a value
hash_table.delete_val('[email protected]')
print(hash_table)