-
-
Save mhewedy/11380342 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// direct translation for https://gist.github.com/sauron/9773864 | |
public class Example { | |
public static void main(String[] args) { | |
System.out.println(hash("leepadg")); | |
System.out.println(unhash(680131659347L)); | |
} | |
static String letters = "acdegilmnoprstuw"; | |
static long hash(String s) { | |
long h = 7; | |
for (int i = 0; i < s.length(); i++) { | |
int var = letters.indexOf(s.charAt(i)); | |
h = (h * 37 + var); | |
} | |
return h; | |
} | |
static String unhash(long h) { | |
StringBuffer sb = new StringBuffer(); | |
while (h > 7) { | |
Object[] obj = getNumberAndChar(h); | |
h = (long) obj[0]; | |
sb.append((char) obj[1]); | |
} | |
return sb.reverse().toString(); | |
} | |
static Object[] getNumberAndChar(long h) { | |
for (int i = 0; i < letters.length(); i++) { | |
if ((h - i) % 37 == 0) { | |
return new Object[] { h / 37, letters.charAt(i) }; | |
} | |
} | |
return new Object[] { 0, '\0' }; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! 😉