Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
C
Java
my_hash_set = [None,'Jones',None,'Lisa',None,'Bob',None,'Siri','Pete',None] def hash_function(value): sum_of_chars = 0 for char in value: sum_of_chars += ord(char) return sum_of_chars % 10 def contains(name): index = hash_function(name) return my_hash_set[index] == name print("'Pete' is in the Hash Set:",contains('Pete')) #Python
#include
#include
char* myHashSet[10] = {NULL, "Jones", NULL, "Lisa", NULL, "Bob", NULL, "Siri", "Pete", NULL}; int hashFunction(const char* value) { int sumOfChars = 0; while (*value) { sumOfChars += (int)*value; value++; } return sumOfChars % 10; } int contains(const char* name) { int index = hashFunction(name); if (myHashSet[index] != NULL && strcmp(myHashSet[index], name) == 0) { return 1; // True } return 0; // False } int main() { printf("'Pete' is in the Hash Set: %s\n", contains("Pete") ? "true" : "false"); return 0; } // C
public class Main { static final String[] myHashSet = {null, "Jones", null, "Lisa", null, "Bob", null, "Siri", "Pete", null}; public static void main(String[] args) { System.out.println("'Pete' is in the Hash Set: " + contains("Pete")); } public static int hashFunction(String value) { int sumOfChars = 0; for (char c : value.toCharArray()) { sumOfChars += c; } return sumOfChars % 10; } public static boolean contains(String name) { int index = hashFunction(name); return myHashSet[index] != null && myHashSet[index].equals(name); } } //Java
Python result:
C result:
Java result:
'Pete' is in the Hash Set: True
'Pete' is in the Hash Set: true
'Pete' is in the Hash Set: true