7 kyu

BAD Hash - String to Int

108 of 259Bigg-T

Description:

Implement a function which takes a string, and returns its hash value.

Algorithm steps:

  • a := sum of the ascii values of the input characters
  • b := sum of every difference between the consecutive characters of the input (second char minus first char, third minus second, ...)
  • c := (a OR b) AND ((NOT a) shift left by 2 bits)
  • d := c XOR (32 * (total_number_of_spaces + 1))
  • return d

Note: OR, AND, NOT, XOR are bitwise operations.


Examples

input = "a"
a = 97
b = 0
result = 64

input = "ca"
a = 196
b = -2
result = -820

Give an example why this hashing algorithm is bad?

Fundamentals

Stats:

CreatedJul 18, 2017
PublishedJul 18, 2017
Warriors Trained643
Total Skips39
Total Code Submissions1203
Total Times Completed259
C Completions108
NASM Completions9
Python Completions156
Total Stars11
% of votes with a positive feedback rating80% of 93
Total "Very Satisfied" Votes60
Total "Somewhat Satisfied" Votes28
Total "Not Satisfied" Votes5
Total Rank Assessments11
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • Bigg-T Avatar
  • FArekkusu Avatar
  • monadius Avatar
  • uniapi Avatar
  • user8436785 Avatar
Ad