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 BYTE = 8; | |
const BITS = 64; | |
const SEGMENTS = 8; | |
const SEGMENT = BITS / SEGMENTS; | |
const METHODS_MAP = { | |
8: ['getUint8', 'setUint8', 'getInt8', 'setInt8'], | |
16: ['getUint16', 'setUint16', 'getInt16', 'setInt16'], | |
32: ['getUint32', 'setUint32', 'getInt32', 'setInt32'], | |
}; |
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 solveFor = (target, input, solveFn) => { | |
let solved = false; | |
const sort = arr => arr.sort((a, b) => a-b); | |
const nums = input.map(n => +n); | |
const ops = [...'+*']; | |
const fns = { | |
'+': (a, b) => a + b, | |
'*': (a, b) => a * b, |
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
// https://ekw.ms.gov.pl/ | |
class MortgageRegister { | |
static check(str) { | |
const m = str.match(/([A-Z0-9]{4}\/[0-9]{8})\/([0-9])/); | |
if (m) { | |
return this.checksum(m[1]) === +m[2]; | |
} | |
return false; |
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
// alphametic puzzle solver (brute force) | |
const solve = (a, b, c, s) => { | |
const uniq = [...new Set([...(a+b+c+s)])]; | |
const res = []; | |
const rec = nums => { | |
if (nums.length >= uniq.length) { | |
const an = +[...a].map(l => nums[uniq.indexOf(l)]).join``; | |
const bn = +[...b].map(l => nums[uniq.indexOf(l)]).join``; | |
const cn = +[...c].map(l => nums[uniq.indexOf(l)]).join``; |
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
// http://instacode.dev/#Y29uc3QgcGVyZlRlc3QgPSAobmFtZSwgY2FsbGJhY2spID0+IHsKCWNvbnN0IGQwID0gK25ldyBEYXRlKCk7CiAgY2FsbGJhY2soKTsKICBjb25zdCBkMSA9ICtuZXcgRGF0ZSgpOwogIGNvbnNvbGUubG9nKGBUZXN0ICR7bmFtZX06ICR7ZDEtZDB9bXNgKTsKfTsKCgpjb25zdCBSRVBTID0gMTA7CmNvbnN0IFNJWkUgPSAxZTU7Cgpjb25zdCBhcnIgPSBbXTsKZm9yIChsZXQgaSA9IDA7IGkgPCBTSVpFOyArK2kpIHsKICBhcnJbaV0gPSB7aSwgcm5kOiBNYXRoLnJhbmRvbSgpfTsKfQpjb25zdCBzdHIgPSBKU09OLnN0cmluZ2lmeShhcnIpOwoKcGVyZlRlc3QoJ3N0cmluZ2lmeScsICgpID0+IHsKICBmb3IgKGxldCBpID0gMDsgaSA8IFJFUFM7ICsraSkgewogIAlKU09OLnN0cmluZ2lmeShhcnIpOwogIH0KfSk7CgpwZXJmVGVzdCgncGFyc2UnLCAoKSA9PiB7CiAgZm9yIChsZXQgaSA9IDA7IGkgPCBSRVBTOyArK2kpIHsKICAJSlNPTi5wYXJzZShzdHIpOwogIH0KfSk7 | |
const perfTest = (name, callback) => { | |
const d0 = +new Date(); | |
callback(); | |
const d1 = +new Date(); | |
console.log(`Test ${name}: ${d1-d0}ms`); | |
}; | |
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 genPermutations = size => { | |
const perms = []; | |
const gen = (res, used, shift) => { | |
if (used === size) { | |
perms.push(res); | |
} | |
for (let i = 0; i < (size-used); ++i) { | |
gen([...res, shift[i]], used + 1, [...shift.slice(0, i), ...shift.slice(i + 1)]); | |
} | |
}; |
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 matchIpv4 = str => { | |
const re = /(^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)/g; | |
return str.match(re) | []; | |
}; | |
const matchIpv6 = str => { | |
const re = /(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{ |
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 arrayToBst = arr => { | |
const [LEFT, VALUE, RIGHT] = [0, 1, 2]; | |
const node = [null, null, null] | |
const bst = node.slice(); | |
arr.map(el => { | |
let tmp = bst; | |
while (tmp[VALUE] !== null) { | |
const branch = el < tmp[VALUE] ? LEFT : RIGHT; | |
if (tmp[branch] === null) { |
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 sieve = []; | |
const MAX = 10008; | |
for (let i = 2; i < MAX; ++i) { | |
for (let j = i * 2; j < MAX; j += i) { | |
sieve[j] = 1; | |
} | |
} |
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
function countPairs(arr) { | |
// console.log('regular:'); | |
let cnt = 0; | |
for (let i = 0; i < arr.length; ++i) { | |
for (let j = i + 1; j < arr.length; ++j) { | |
const and = arr[i] & arr[j]; | |
const log = Math.log(and) / Math.log(2); | |