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
| // big int | |
| const BigInt = function BigInt(n = '0') { | |
| const str = (n.join && n.join`` || n + '').replace(/^0+/, '') || '0'; | |
| const sign = str[0] === '-' && +n !== 0; | |
| const arr = [...str.replace(/^\-/, '')].map(n => +n); | |
| const calcDbl = bi => { | |
| return [1, 2, 3].reduce(([dbl, acc]) => { | |
| acc = acc.add(acc); |
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
| // array intersection that correctly handles also duplicates | |
| const intersection = (a1, a2) => { | |
| const cnt = new Map(); | |
| a2.map(el => cnt.set(el, cnt.has(el) ? cnt.get(el) + 1 : 1)); | |
| return a1.filter(el => cnt.has(el)).filter(el => { | |
| const left = cnt.get(el); | |
| if (0 < left) { | |
| cnt.set(el, left - 1); | |
| return true; |
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 groupBy(arr, fn) { | |
| fn = fn || ((o) => o); | |
| let g = []; | |
| let v = []; | |
| for (let i = 0; i < arr.length; ++i) { | |
| let val = fn(arr[i]); | |
| let idx = g.indexOf(val); | |
| if (idx === -1) { | |
| g.push(val); |
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 findIndexes = (arr, fn) => { | |
| const res = []; | |
| arr.map((el, i, arr) => { | |
| if (fn(el, i, arr)) res.push(i); | |
| }); | |
| return res; | |
| }; | |
| // set prototype |
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
| // code that generates BF (hardcoded) solution using js | |
| // https://www.codewars.com/kata/bf-n-th-fibonacci-number/train/bf | |
| const MAX = 100; | |
| const flag = '+>'; | |
| const inp = ','; | |
| const nestedLoop = n => { | |
| if (n > MAX) return ''; | |
| return `-[ |
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 fib = n => { | |
| const sq5 = Math.sqrt(5); | |
| return (sq5 * (1+sq5)**n - sq5 * (1-sq5)**n) * 2**-n * 0.2; | |
| }; | |
| const fib2n = f => { | |
| const sq5 = Math.sqrt(5); | |
| const phi = (1 + sq5) / 2; | |
| return Math.floor(Math.log(f * sq5 + 0.5) / Math.log(phi)); | |
| }; |
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 bijectiveBinary = { | |
| convertToInt: s => parseInt('1'+s.replace(/./g,d=>(+d)-1), 2) - 1, | |
| convertFromInt: i => (i+1).toString(2).substr(1).replace(/./g,d=>(+d)+1) | |
| }; | |
| // codewars: | |
| // https://www.codewars.com/kata/58f5e53e663082f9aa000060/solutions/javascript |
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 = (max = 1e5) => { | |
| const maxi = Math.sqrt(max); | |
| const notPrime = new Int8Array(max); | |
| notPrime[0] = notPrime[1] = 1; | |
| for (let i = 2; i < maxi; ++i) { | |
| if (notPrime[i] === 0) { | |
| for (let j = 2*i; j < max; j += i) { | |
| notPrime[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
| // input: array of bits | |
| // [1,0,1,0,1,1] | |
| function convertBit(p, v, i) { | |
| return p[i] = i && p[i-1] ? 1 - v : v, p; | |
| } | |
| function bin2gray(bits) { | |
| return bits.reduceRight(convertBit, bits); | |
| } |
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 convertToRoman = num => { | |
| const roman = [...'IVXLCDM']; | |
| const patt = [ | |
| [], | |
| [0], | |
| [0, 0], | |
| [0, 0, 0], | |
| [0, 1], | |
| [1], | |
| [1, 0], |