This Code Golf was inspired by the recent Daily WTF article You Can't Handle the True!, which features a string comparison written as:
String yes = "YES";
if ((delay.hashCode()) == yes.hashCode())
Imagine the trouble it would have caused for Steve's team if Java's String.hashCode
method just happened to be implemented in a way that "YES".hashCode() == "NO".hashCode()
. So, the challenge I propose here is:
Write, in as few characters as possible, a hash function (I'll call it
h
) with a string parameter and integer return value, such thath("YES")
is equal toh("NO")
.
Of course, this would be trivial to do with a function like def h(s): return 0
, which makes a hash collision for every string. To make this challenge more interesting, you must abide by the following additional rule:
Of the other 18 277 possible strings consisting of three or fewer uppercase ASCII letters (
^[A-Z]{0,3}$
), there must be no hash collisions.
Clarification (pointed out by Heiko Oberdiek): The input string may contain characters other than A-Z
, and your code must be able to hash arbitrary strings. (You may, however, assume that the input is a character string rather than a null pointer or an object of some other data type.) However, it does not matter what the return value is for strings that do not match ^[A-Z]{0,3}$
, as long as it's an integer.
Furthermore, to obfuscate the intent of this function:
Your code must not include any of the letters 'Y', 'E', 'S', 'N', or 'O' (in either upper- or lowercase) within character or string literals.
Of course, this restriction doesn't apply to language keywords, so else
, return
, etc. are fine.
YESNO
to check for this specific exception. \$\endgroup\$