Skip to content

Instantly share code, notes, and snippets.

View gkucmierz's full-sized avatar
💻

Grzegorz Kućmierz gkucmierz

💻
View GitHub Profile
@gkucmierz
gkucmierz / find_indexes.js
Last active November 14, 2019 20:31
findIndex, missing findIndexes, prototype, polyfill
const findIndexes = (arr, fn) => {
const res = [];
arr.map((el, i, arr) => {
if (fn(el, i, arr)) res.push(i);
});
return res;
};
// set prototype
@gkucmierz
gkucmierz / bf_gen_fibonacci.js
Created November 10, 2019 20:13
fibonacci bf generate code
// 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 `-[
@gkucmierz
gkucmierz / fibonacci.js
Last active December 9, 2019 00:42
calculating fibonacci numbers and reverse, fib, formula
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));
};
@gkucmierz
gkucmierz / bijective_numeration.js
Created October 28, 2019 22:25
Bijective Numeration
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
@gkucmierz
gkucmierz / sieve_of_eratosthenes.js
Last active September 22, 2019 06:09
Sieve of Eratosthenes
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;
}
}
@gkucmierz
gkucmierz / gray_code.js
Created September 19, 2019 05:14
bin to gray; gray to bin
// 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);
}
const convertToRoman = num => {
const roman = [...'IVXLCDM'];
const patt = [
[],
[0],
[0, 0],
[0, 0, 0],
[0, 1],
[1],
[1, 0],
@gkucmierz
gkucmierz / random_word.js
Created September 12, 2019 14:42
random word, password generator
const randomWord = (len = 16, up = true, low = true, digits = true, special = '') => {
const chars = [special];
up && chars.push('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
low && chars.push('abcdefghijklmnopqrstuvwxyz');
digits && chars.push('0123456789');
const str = chars.join``;
const word = [];
for (let i = 0; i < len; ++i) {
word[i] = str[Math.round(Math.random() * str.length) % str.length];
@gkucmierz
gkucmierz / lcm_gcd.js
Created September 12, 2019 14:06
lcm, gcd
// Smallest Common Multiple
// Least common multiple
// Greatest common divisor
function gcd(a, b) {
if (a < 0) a = -a;
if (b < 0) b = -b;
if (b > a) {
[a, b] = [b, a];
}
@gkucmierz
gkucmierz / kraken.com-remove_old_addresses.js
Last active September 9, 2019 13:31
kraken.com - remove old addresses
const actions = [
() => document.querySelector('button[name=delete]').click(),
() => document.querySelector('button.delete-address').click(),
() => document.querySelector('button.back').click()
];
let i = 0;
setInterval(() => actions[(i++)%actions.length](), 1e3);