Created
July 20, 2012 03:06
-
-
Save mishudark/3148422 to your computer and use it in GitHub Desktop.
Shortener
This file contains hidden or 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
| var letras = [0,1,2,3,4,5,6,7,8,9,'A','a','B','b','C','c','D','d','E','e','F','f','G','g','H','h','I','i','J','j','K','k','L','l','M','m','N','n','O','o','P','p','Q','q','R','r','S','s','T','t','U','u','V','v','W','w','X','x','Y','y','Z','z']; | |
| function encode_shortener(num){ | |
| var t = letras.length; | |
| var shorten = ''; | |
| while(num > 0){ | |
| var div = Math.floor(num / t); | |
| var res = num - div * t; | |
| shorten += letras[res]; | |
| num = div; | |
| } | |
| return shorten; | |
| } | |
| function decode_shortener(phrase){ | |
| var t = letras.length; | |
| var position = create_dic_letras(); | |
| var total = 0; | |
| $.each(phrase,function(i,l){ | |
| total += position[l] * Math.pow(t,i); | |
| }); | |
| return total; | |
| } | |
| function create_dic_letras(){ | |
| var letras_hash = {}; | |
| $.each(letras,function(i,l){ | |
| letras_hash[l] = i; | |
| }); | |
| return letras_hash; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment