This file contains 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
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++) { |
This file contains 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 flatten(arr) { | |
var res = []; | |
(function dfs(arr, i) { | |
if (i >= arr.length) { | |
return; | |
} | |
if (Array.isArray(arr[i])) { | |
return dfs(arr[i], 0); |
This file contains 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
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) { |
This file contains 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 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; |
This file contains 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 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; | |
} | |
} |
This file contains 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 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; | |
} |
This file contains 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
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 |
This file contains 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
var obj = {prop1: "v1", prop2: "v2", prop3: "v3"}; | |
var arr = []; | |
for (var i in obj) { | |
arr.push({ | |
name : i, | |
value : obj[i] | |
}) | |
} |
This file contains 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
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) |
This file contains 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
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] |