Skip to content

Instantly share code, notes, and snippets.

@daniel-sc
Last active February 24, 2023 09:16
Show Gist options
  • Save daniel-sc/cde195a0a18c583aa5018de28b93642a to your computer and use it in GitHub Desktop.
Save daniel-sc/cde195a0a18c583aa5018de28b93642a to your computer and use it in GitHub Desktop.
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