Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Creating Hash Table with String Keys | Advanced Data Structures
Algorithms and Data Structures Overview

book
Challenge: Creating Hash Table with String Keys

Opgave

Swipe to start coding

In the previous chapter, we created a hash table with a function that can work with numerical keys. Now you will implement a hash function that will work with keys of type string.

This particular hash function must calculate the hash value by summing the ASCII values of the characters in the key string. Doing so transforms variable-length input (keys of different lengths) into a fixed-size hash value. This hash value is then modulo with the size of the hash table to determine the index where the key-value pair should be stored.

Your task is to implement this hash function by filling the gaps in the ._hash_function() method of the HashTable class.

Løsning

import numpy as np

class HashTable:
def __init__(self, size):
self.size = size
self.table = {i: np.array([]) for i in range(size)}
def _hash_function(self, key):
# An interesting hash function: sum of ASCII values of characters in the key
hash_value = sum(ord(char) for char in key)
return hash_value % self.size
def insert(self, key, value):
index = self._hash_function(key)
if len(self.table[index]) == 0:
self.table[index] = np.array([(key, value)])
else:
self.table[index] = np.append(self.table[index], np.array([(key, value)]), axis=0)
def search(self, key):
index = self._hash_function(key)
for pair in self.table[index]:
if pair is not None and pair[0] == key:
return pair[1]
return None
def delete(self, key):
index = self._hash_function(key)
for i, pair in enumerate(self.table[index]):
if pair is not None and pair[0] == key:
self.table[index] = np.delete(self.table[index], i, axis=0)
return True
return False

# Example usage:
hash_table = HashTable(size=10)

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 5
import numpy as np

class HashTable:
def __init__(self, size):
self.size = size
self.table = {i: np.array([]) for i in range(size)}
def _hash_function(self, key):
# An interesting hash function: sum of ASCII values of characters in the key
hash_value = ___(___(char) for char in key)
return hash_value ___ self.size
def insert(self, key, value):
index = self._hash_function(key)
if len(self.table[index]) == 0:
self.table[index] = np.array([(key, value)])
else:
self.table[index] = np.append(self.table[index], np.array([(key, value)]), axis=0)
def search(self, key):
index = self._hash_function(key)
for pair in self.table[index]:
if pair is not None and pair[0] == key:
return pair[1]
return None
def delete(self, key):
index = self._hash_function(key)
for i, pair in enumerate(self.table[index]):
if pair is not None and pair[0] == key:
self.table[index] = np.delete(self.table[index], i, axis=0)
return True
return False

# Example usage:
hash_table = HashTable(size=10)

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt