Skip to content

Instantly share code, notes, and snippets.

const isSwapOneTime = (input) => {
const diff = (a, b) => a.filter((i, idx) => i !== b[idx]);
const sort = (a) => [ ...a ].sort();
const sorted = sort(input);
return diff(input, sorted).length <= 2;
}
console.log(isSwapOneTime([1, 2, 4, 5, 3])); //false
console.log(isSwapOneTime([1, 3, 3, 4, 5])); //true
console.log(isSwapOneTime([1, 2, 3, 4, 5])); //true
function bingap(n) {
const binary = (n >>> 0).toString(2);
const f1 = new RegExp(/1?0+1?/, 'g');
const f2 = new RegExp(/1(0+)1/);
return binary.match(f1)
.filter(r1 => r1.match(f2))
.reduce((acc, n) => acc > n.length - 2 ? acc : n.length - 2, 0)
}
console.log(bingap(42));
@salamtamp
salamtamp / 0_reuse_code.js
Created March 4, 2016 05:43
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console