Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
C
Java
class SimpleHashMap: def __init__(self, size=100): self.size = size self.buckets = [[] for _ in range(size)] # A list of buckets, each is a list (to handle collisions) def hash_function(self, key): # Sum only the numerical values of the key, ignoring non-numeric characters numeric_sum = sum(int(char) for char in key if char.isdigit()) return numeric_sum % 10 # Perform modulo 10 on the sum def put(self, key, value): # Add or update a key-value pair index = self.hash_function(key) bucket = self.buckets[index] for i, (k, v) in enumerate(bucket): if k == key: bucket[i] = (key, value) # Update existing key return bucket.append((key, value)) # Add new key-value pair if not found def get(self, key): # Retrieve a value by key index = self.hash_function(key) bucket = self.buckets[index] for k, v in bucket: if k == key: return v return None # Key not found def remove(self, key): # Remove a key-value pair index = self.hash_function(key) bucket = self.buckets[index] for i, (k, v) in enumerate(bucket): if k == key: del bucket[i] # Remove the key-value pair return def print_map(self): # Print all key-value pairs in the hash map print("Hash Map Contents:") for index, bucket in enumerate(self.buckets): print(f"Bucket {index}: {bucket}") # Creating the Hash Map from the simulation hash_map = SimpleHashMap(size=10) # Adding some entries hash_map.put("123-4567", "Charlotte") hash_map.put("123-4568", "Thomas") hash_map.put("123-4569", "Jens") hash_map.put("123-4570", "Peter") hash_map.put("123-4571", "Lisa") hash_map.put("123-4672", "Adele") hash_map.put("123-4573", "Michaela") hash_map.put("123-6574", "Bob") hash_map.print_map() print("\nName associated with '123-4570':", hash_map.get("123-4570")) print("Updating the name for '123-4570' to 'James'") hash_map.put("123-4570","James") print("Name associated with '123-4570':", hash_map.get("123-4570")) #Python
#include
#include
#include
#define BUCKET_COUNT 10 #define MAX_KEY_LEN 16 #define MAX_VAL_LEN 32 typedef struct Entry { char key[MAX_KEY_LEN]; char value[MAX_VAL_LEN]; struct Entry* next; } Entry; typedef struct { Entry* buckets[BUCKET_COUNT]; } SimpleHashMap; unsigned int hashFunction(const char* key) { unsigned int sum = 0; for (int i = 0; key[i] != '\0'; i++) { if (key[i] >= '0' && key[i] <= '9') { sum += key[i] - '0'; } } return sum % BUCKET_COUNT; } void initHashMap(SimpleHashMap* map) { for (int i = 0; i < BUCKET_COUNT; i++) { map->buckets[i] = NULL; } } void put(SimpleHashMap* map, const char* key, const char* value) { unsigned int index = hashFunction(key); Entry* current = map->buckets[index]; // Update value if key already exists while (current != NULL) { if (strcmp(current->key, key) == 0) { strncpy(current->value, value, MAX_VAL_LEN - 1); current->value[MAX_VAL_LEN - 1] = '\0'; return; } current = current->next; } // Add new key-value pair Entry* newEntry = (Entry*)malloc(sizeof(Entry)); strncpy(newEntry->key, key, MAX_KEY_LEN - 1); newEntry->key[MAX_KEY_LEN - 1] = '\0'; strncpy(newEntry->value, value, MAX_VAL_LEN - 1); newEntry->value[MAX_VAL_LEN - 1] = '\0'; newEntry->next = map->buckets[index]; map->buckets[index] = newEntry; } const char* get(SimpleHashMap* map, const char* key) { unsigned int index = hashFunction(key); Entry* current = map->buckets[index]; while (current != NULL) { if (strcmp(current->key, key) == 0) { return current->value; } current = current->next; } return NULL; // Key not found } void removeEntry(SimpleHashMap* map, const char* key) { unsigned int index = hashFunction(key); Entry* current = map->buckets[index]; Entry* prev = NULL; while (current != NULL) { if (strcmp(current->key, key) == 0) { if (prev != NULL) { prev->next = current->next; } else { map->buckets[index] = current->next; } free(current); return; } prev = current; current = current->next; } } void freeHashMap(SimpleHashMap* map) { for (int i = 0; i < BUCKET_COUNT; i++) { Entry* current = map->buckets[i]; while (current != NULL) { Entry* temp = current; current = current->next; free(temp); } } } void printHashMap(SimpleHashMap* map) { printf("Hash Map Contents:\n"); for (int i = 0; i < BUCKET_COUNT; i++) { Entry* current = map->buckets[i]; printf("Bucket %d: ", i); while (current != NULL) { printf("(%s: %s) ", current->key, current->value); current = current->next; } printf("\n"); } } int main() { SimpleHashMap map; initHashMap(&map); put(&map, "123-4567", "Charlotte"); put(&map, "123-4568", "Thomas"); put(&map, "123-4569", "Jens"); put(&map, "123-4570", "Peter"); put(&map, "123-4571", "Lisa"); put(&map, "123-4672", "Adele"); put(&map, "123-4573", "Michaela"); put(&map, "123-6574", "Bob"); printHashMap(&map); // Demonstrate get and put printf("\nName associated with '123-4570': %s\n", get(&map, "123-4570")); printf("Updating the name for '123-4570' to 'James'."); put(&map, "123-4570", "James"); printf("\nName associated with '123-4570': %s\n", get(&map, "123-4570")); freeHashMap(&map); return 0; } // C
import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { SimpleHashMap hashMap = new SimpleHashMap(10); // Adding some entries hashMap.put("123-4567", "Charlotte"); hashMap.put("123-4568", "Thomas"); hashMap.put("123-4569", "Jens"); hashMap.put("123-4570", "Peter"); hashMap.put("123-4571", "Lisa"); hashMap.put("123-4672", "Adele"); hashMap.put("123-4573", "Michaela"); hashMap.put("123-6574", "Bob"); hashMap.printMap(); // Demonstrating retrieval and update System.out.println("\nName associated with '123-4570': " + hashMap.get("123-4570")); System.out.println("Updating the name for '123-4570' to 'James'"); hashMap.put("123-4570", "James"); System.out.println("Name associated with '123-4570': " + hashMap.get("123-4570")); } } class SimpleHashMap { private final List
> buckets; public SimpleHashMap(int size) { buckets = new ArrayList<>(size); for (int i = 0; i < size; i++) { buckets.add(new ArrayList<>()); } } private int hashFunction(String key) { int numericSum = key.chars() .filter(Character::isDigit) .map(Character::getNumericValue) .sum(); return numericSum % buckets.size(); } public void put(String key, String value) { int index = hashFunction(key); List
bucket = buckets.get(index); for (Pair pair : bucket) { if (pair.key.equals(key)) { pair.value = value; return; } } bucket.add(new Pair(key, value)); } public String get(String key) { int index = hashFunction(key); List
bucket = buckets.get(index); for (Pair pair : bucket) { if (pair.key.equals(key)) { return pair.value; } } return null; // Key not found } public void remove(String key) { int index = hashFunction(key); List
bucket = buckets.get(index); bucket.removeIf(pair -> pair.key.equals(key)); } public void printMap() { System.out.println("Hash Map Contents:"); for (int i = 0; i < buckets.size(); i++) { List
bucket = buckets.get(i); System.out.print("Bucket " + i + ": "); bucket.forEach(pair -> System.out.print("[" + pair.key + ": " + pair.value + "] ")); System.out.println(); } } } class Pair { String key; String value; public Pair(String key, String value) { this.key = key; this.value = value; } } //Java
Python result:
C result:
Java result:
Hash Map Contents:
Bucket 0: [('123-4569', 'Jens')]
Bucket 1: []
Bucket 2: [('123-4570', 'Peter')]
Bucket 3: [('123-4571', 'Lisa')]
Bucket 4: []
Bucket 5: [('123-4672', 'Adele'), ('123-4573', 'Michaela')]
Bucket 6: []
Bucket 7: []
Bucket 8: [('123-4567', 'Charlotte'), ('123-6574', 'Bob')]
Bucket 9: [('123-4568', 'Thomas')]
Name associated with '123-4570': Peter
Updating the name for '123-4570' to 'James'
Name associated with '123-4570': James
Hash Map Contents:
Bucket 0: (123-4569: Jens)
Bucket 1:
Bucket 2: (123-4570: Peter)
Bucket 3: (123-4571: Lisa)
Bucket 4:
Bucket 5: (123-4573: Michaela) (123-4672: Adele)
Bucket 6:
Bucket 7:
Bucket 8: (123-6574: Bob) (123-4567: Charlotte)
Bucket 9: (123-4568: Thomas)
Name associated with '123-4570': Peter
Updating the name for '123-4570' to 'James'.
Name associated with '123-4570': James
Hash Map Contents:
Bucket 0: [123-4569: Jens]
Bucket 1:
Bucket 2: [123-4570: Peter]
Bucket 3: [123-4571: Lisa]
Bucket 4:
Bucket 5: [123-4672: Adele] [123-4573: Michaela]
Bucket 6:
Bucket 7:
Bucket 8: [123-4567: Charlotte] [123-6574: Bob]
Bucket 9: [123-4568: Thomas]
Name associated with '123-4570': Peter
Updating the name for '123-4570' to 'James'
Name associated with '123-4570': James