Last active
February 24, 2023 09:16
-
-
Save daniel-sc/cde195a0a18c583aa5018de28b93642a to your computer and use it in GitHub Desktop.
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
const minCharCode = Math.min(...'aAzZ019-_.:,'.toUpperCase().split('').map(c => c.charCodeAt(0))); | |
const maxCharCode = Math.max(...'aAzZ019-_.:,'.toUpperCase().split('').map(c => c.charCodeAt(0))); | |
const alphabetSize = maxCharCode - minCharCode + 2; | |
console.log(minCharCode, maxCharCode, alphabetSize, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER); | |
function toNumber(char) { | |
return char.toUpperCase().charCodeAt(0) - minCharCode + 1; | |
} | |
/** | |
* @param s string | |
*/ | |
function embed(s) { | |
let sum = 0; | |
for (let i = 0; i < s.length; i++) { | |
sum += toNumber(s.charAt(i)) / Math.pow(alphabetSize, i+1); | |
} | |
return sum; | |
} | |
const arg = process.argv[2]; | |
console.log(`string: ${arg}`); | |
console.log(`embed: ${embed(arg)}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment