Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
C
Java
def hash_function(value): sum_of_chars = 0 for char in value: sum_of_chars += ord(char) return sum_of_chars % 10 print("'Bob' has hash code:",hash_function('Bob')) #Python
#include
#include
int hashFunction(const char* value) { int sum = 0; for (int i = 0; i < strlen(value); i++) { sum += (int) value[i]; } return sum % 10; } int main() { char value[] = "Bob"; printf("'Bob' has hash code: %d\n", hashFunction(value)); return 0; } // C
public class Main { public static void main(String[] args) { String value = "Bob"; System.out.println("'" + value + "' has hash code: " + hashFunction(value)); } public static int hashFunction(String value) { int sum = 0; for (int i = 0; i < value.length(); i++) { sum += value.charAt(i); } return sum % 10; } } //Java
Python result:
C result:
Java result:
'Bob' has hash code: 5
'Bob' has hash code: 5
'Bob' has hash code: 5