Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
C
Java
class SimpleHashSet: 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, value): # Simple hash function: sum of character codes modulo the number of buckets return sum(ord(char) for char in value) % self.size def add(self, value): # Add a value if it's not already present index = self.hash_function(value) bucket = self.buckets[index] if value not in bucket: bucket.append(value) def contains(self, value): # Check if a value exists in the set index = self.hash_function(value) bucket = self.buckets[index] return value in bucket def remove(self, value): # Remove a value index = self.hash_function(value) bucket = self.buckets[index] if value in bucket: bucket.remove(value) def print_set(self): # Print all elements in the hash set print("Hash Set Contents:") for index, bucket in enumerate(self.buckets): print(f"Bucket {index}: {bucket}") # Creating the Hash Set from the simulation hash_set = SimpleHashSet(size=10) hash_set.add("Charlotte") hash_set.add("Thomas") hash_set.add("Jens") hash_set.add("Peter") hash_set.add("Lisa") hash_set.add("Adele") hash_set.add("Michaela") hash_set.add("Bob") hash_set.print_set() print("\n'Peter' is in the set:",hash_set.contains('Peter')) print("Removing 'Peter'") hash_set.remove('Peter') print("'Peter' is in the set:",hash_set.contains('Peter')) print("'Adele' has hash code:",hash_set.hash_function('Adele')) #Python
#include
#include
#include
#define BUCKET_COUNT 10 #define MAX_STRING_LENGTH 256 // Define a maximum string length typedef struct Node { char value[MAX_STRING_LENGTH]; // Use a fixed-size array for values struct Node* next; } Node; typedef struct { Node* buckets[BUCKET_COUNT]; } SimpleHashSet; unsigned int hashFunction(const char* value) { unsigned int hash = 0; while (*value) { hash = (hash + *value++) % BUCKET_COUNT; } return hash; } void initHashSet(SimpleHashSet* set) { for (int i = 0; i < BUCKET_COUNT; i++) { set->buckets[i] = NULL; } } int contains(SimpleHashSet *set, const char *value) { unsigned int index = hashFunction(value); Node *node = set->buckets[index]; while (node != NULL) { if (strcmp(node->value, value) == 0) { return 1; // Found } node = node->next; } return 0; // Not found } void removeValue(SimpleHashSet *set, const char *value) { unsigned int index = hashFunction(value); Node **node = &(set->buckets[index]); while (*node != NULL) { if (strcmp((*node)->value, value) == 0) { Node *toDelete = *node; *node = (*node)->next; free(toDelete); // Just free the node, not toDelete->value return; // Value removed } node = &((*node)->next); } } void add(SimpleHashSet* set, const char* value) { unsigned int index = hashFunction(value); // Check if the value already exists to avoid duplicates Node* current = set->buckets[index]; while (current != NULL) { if (strcmp(current->value, value) == 0) { // Value already exists, do not add again return; } current = current->next; } // Add new value Node* newNode = (Node*)malloc(sizeof(Node)); if (!newNode) return; // Handle malloc failure strncpy(newNode->value, value, MAX_STRING_LENGTH); newNode->value[MAX_STRING_LENGTH - 1] = '\0'; // Ensure null-termination newNode->next = set->buckets[index]; set->buckets[index] = newNode; } void printHashSet(SimpleHashSet *set) { printf("Hash Set Contents:\n"); for (int i = 0; i < BUCKET_COUNT; i++) { Node *node = set->buckets[i]; printf("Bucket %d: ", i); while (node) { printf("%s ", node->value); node = node->next; } printf("\n"); } } int main() { SimpleHashSet hashSet; initHashSet(&hashSet); add(&hashSet, "Charlotte"); add(&hashSet, "Thomas"); add(&hashSet, "Jens"); add(&hashSet, "Peter"); add(&hashSet, "Lisa"); add(&hashSet, "Adele"); add(&hashSet, "Michaela"); add(&hashSet, "Bob"); printHashSet(&hashSet); printf("\n'Peter' is in the set: %s\n", contains(&hashSet, "Peter") ? "true" : "false"); printf("Removing 'Peter'\n"); removeValue(&hashSet, "Peter"); printf("'Peter' is in the set: %s\n", contains(&hashSet, "Peter") ? "true" : "false"); printf("'Adele' has hash code: %u\n", hashFunction("Adele")); // Free memory, omitted for brevity return 0; } // C
import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { SimpleHashSet hashSet = new SimpleHashSet(10); hashSet.add("Charlotte"); hashSet.add("Thomas"); hashSet.add("Jens"); hashSet.add("Peter"); hashSet.add("Lisa"); hashSet.add("Adele"); hashSet.add("Michaela"); hashSet.add("Bob"); hashSet.printSet(); System.out.println("\n'Peter' is in the set: " + hashSet.contains("Peter")); System.out.println("Removing 'Peter'"); hashSet.remove("Peter"); System.out.println("'Peter' is in the set: " + hashSet.contains("Peter")); System.out.println("'Adele' has hash code: " + hashSet.hashFunction("Adele")); } static class SimpleHashSet { private final int size; private final List
> buckets; public SimpleHashSet(int size) { this.size = size; this.buckets = new ArrayList<>(size); for (int i = 0; i < size; i++) { buckets.add(new ArrayList<>()); // Use ArrayList for the buckets } } private int hashFunction(String value) { return value.chars().reduce(0, Integer::sum) % size; } public void add(String value) { int index = hashFunction(value); List
bucket = buckets.get(index); if (!bucket.contains(value)) { bucket.add(value); } } public boolean contains(String value) { int index = hashFunction(value); List
bucket = buckets.get(index); return bucket.contains(value); } public void remove(String value) { int index = hashFunction(value); List
bucket = buckets.get(index); bucket.remove(value); } public void printSet() { System.out.println("Hash Set Contents:"); for (int index = 0; index < buckets.size(); index++) { List
bucket = buckets.get(index); System.out.println("Bucket " + index + ": " + bucket); } } } } //Java
Python result:
C result:
Java result:
Hash Set Contents:
Bucket 0: ['Thomas', 'Jens']
Bucket 1: []
Bucket 2: ['Peter']
Bucket 3: ['Lisa']
Bucket 4: ['Charlotte']
Bucket 5: ['Adele', 'Bob']
Bucket 6: []
Bucket 7: []
Bucket 8: ['Michaela']
Bucket 9: []
'Peter' is in the set: True
Removing 'Peter'
'Peter' is in the set: False
'Adele' has hash code: 5
Hash Set Contents:
Bucket 0: Jens Thomas
Bucket 1:
Bucket 2: Peter
Bucket 3: Lisa
Bucket 4: Charlotte
Bucket 5: Bob Adele
Bucket 6:
Bucket 7:
Bucket 8: Michaela
Bucket 9:
'Peter' is in the set: true
Removing 'Peter'
'Peter' is in the set: false
'Adele' has hash code: 5
Hash Set Contents:
Bucket 0: [Thomas, Jens]
Bucket 1: []
Bucket 2: [Peter]
Bucket 3: [Lisa]
Bucket 4: [Charlotte]
Bucket 5: [Adele, Bob]
Bucket 6: []
Bucket 7: []
Bucket 8: [Michaela]
Bucket 9: []
'Peter' is in the set: true
Removing 'Peter'
'Peter' is in the set: false
'Adele' has hash code: 5