Skip to content

Instantly share code, notes, and snippets.

@abozhilov
abozhilov / bridges.js
Created December 15, 2015 18:51
Bridges
var north = [2, 5, 3, 3, 3, 1, 8, 2, 6, 7, 6];
var south = [1, 2, 5, 3, 4, 1, 7, 8, 2, 5, 6];
var nLen = north.length;
var sLen = south.length;
var matrix = [];
for (var i = 0; i <= nLen; i++) {
matrix[i] = [];
for (var j = 0; j <= sLen; j++) {
@abozhilov
abozhilov / flatten.js
Last active December 6, 2015 20:56
Tail call flatten
function flatten(arr) {
var res = [];
(function dfs(arr, i) {
if (i >= arr.length) {
return;
}
if (Array.isArray(arr[i])) {
return dfs(arr[i], 0);
@abozhilov
abozhilov / .js
Created October 19, 2015 12:27
js zip
var arr1 = [1, 2, 3, 4],
arr2 = [5, 6, 7, 8, 9];
function* values(arr) {
for (var i = 0, len = arr.length; i < len; i++) {
yield arr[i];
}
}
function* zip(...iters) {
@abozhilov
abozhilov / gist:cb4710ddc1c1f4d1a28d
Created October 9, 2014 10:42
Array.from simple polyfill
function arrayFrom(arrayLike, mapFn, thisArg) {
var arr = [];
if (typeof mapFn != 'function') {
arr.push.apply(arr, arrayLike);
return arr;
}
for (var i = 0, len = arrayLike.length; i < len; i++) {
arr[i] = mapFn.call(thisArg, arrayLike[i], i);
}
return arr;
@abozhilov
abozhilov / gist:6209657
Created August 12, 2013 10:05
Array pull inplace
function pull(arr) {
var map = {},
len = arr.length,
args = arguments;
for (var i = 1, argsLen = args.length; i < argsLen; i++) {
for (var j = len; j--;) {
if (arr[j] === args[i]) {
map[j] = true;
}
}
function pull(arr) {
var map = {},
res = [],
len = arr.length,
args = arguments;
for (var i = 1, argsLen = args.length; i < argsLen; i++) {
for (var j = len; j--;) {
if (arr[j] === args[i]) {
map[j] = true;
}
def merge(list1, list2):
res = []
while len(list1) and len(list2):
if list1[-1] > list2[-1]:
res.append(list1.pop())
else:
res.append(list2.pop())
res.reverse()
return list1 + list2 + res
var obj = {prop1: "v1", prop2: "v2", prop3: "v3"};
var arr = [];
for (var i in obj) {
arr.push({
name : i,
value : obj[i]
})
}
@abozhilov
abozhilov / gist:5249833
Last active June 10, 2020 23:36
2^1000 mod 100?
2^1 = 2 (mod 100)
-------------------
2^2 = 4 (mod 100)
2^3 = 8 (mod 100)
2^4 = 16 (mod 100)
2^5 = 32 (mod 100)
2^6 = 64 (mod 100)
2^7 = 28 (mod 100)
2^8 = 56 (mod 100)
2^9 = 12 (mod 100)
var hanoi = function (n) {
/**
* Index of the cols is the current position of the disc.
* The value is the transition column.
* When the N minus disc number is even number, use the first row of the table.
* Otherwise when the number is odd use the second.
*/
var STATE_TABLE = [
[2, 0, 1],
[1, 2, 0]