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 decodeMorse = morseCode => morseCode | |
| .trim() | |
| .split` ` | |
| .map(e => morseDictionary[e]) | |
| .join`` | |
| .replace(/\s+/g, ' '); | |
| const morseDictionary = { | |
| '...---...': 'SOS', | |
| '-.-.--': '!', |
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
| /** | |
| * [1,2,3].equals([1,2,3]); // true | |
| * [1,2,3].equals([1,2]); // false | |
| * [1,2,3].equals([1,2,4]); // false | |
| * [1,2,3].equals("123"); // false | |
| * Array.prototype.equals.call("123", "123"); // true | |
| * Array.prototype.equals.call("123", [1,2,3]); // false | |
| * [1,2,3].equals([1,2,{value: 3}], (x, y) => (x.value || x) === (y.value || y)); // true | |
| */ | |
| Array.prototype.equals = function (other, callback = (x, y) => (x === y)) { |
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 angle = (a, b, c ,d) => ~~((Math.atan2(d - b, c - a)) * 180 / Math.PI); |
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 quickSort = (arr, left, right) => { | |
| let len = arr.length, | |
| pivot, | |
| partitionIndex; | |
| if (left < right){ | |
| pivot = right; | |
| partitionIndex = partition(arr, pivot, left, right); | |
| //sort left and right |
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 hamming = n => { | |
| const seq = [1]; | |
| let i2 = 0, i3 = 0, i5 = 0; | |
| for (let i = 1; i < n; i++) { | |
| let x = Math.min(2 * seq[i2], 3 * seq[i3], 5 * seq[i5]); | |
| seq.push(x); | |
| if (2 * seq[i2] <= x) i2++; | |
| if (3 * seq[i3] <= x) i3++; | |
| if (5 * seq[i5] <= x) i5++; | |
| } |
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 => Math.round(Math.pow(((1 + Math.sqrt(5)) / 2), n - 1) / Math.sqrt(5)); |
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 numberFormat = n => String(n).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$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 makeNumber = (n, f) => f ? f(n) : n; | |
| const zero = f => makeNumber(0, f); | |
| const one = f => makeNumber(1, f); | |
| const two = f => makeNumber(2, f); | |
| const three = f => makeNumber(3, f); | |
| const four = f => makeNumber(4, f); | |
| const five = f => makeNumber(5, f); | |
| const six = f => makeNumber(6, f); | |
| const seven = f => makeNumber(7, f); | |
| const eight = f => makeNumber(8, 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 hexToBase64 = hex => Buffer.from(hex, 'hex').toString('base64'); | |
| const hex2bin = Object.assign( ["0000","0001","0010","0011","0100","0101","0110","0111","1000","1001"], { a:"1010", b:"1011", c:"1100", d:"1101", e:"1110", f:"1111" } ) , | |
| bin2base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" , | |
| hexToBase64 = s => s.replace( /./g, c => hex2bin[c] ).replace( /.{1,6}/g, s => bin2base64[Number.parseInt(s.padEnd(6,0),2)] ) + "=".repeat(s.length%3) ; |
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 chained = f => x => f.reduce((r, f) => f(r), x); | |
| // chained([a,b,c,d])(input) | |
| // yields the same result as: | |
| // d(c(b(a(input)))) |