Skip to content

Instantly share code, notes, and snippets.

View lqt0223's full-sized avatar

lqt0223 lqt0223

  • Shanghai, China
View GitHub Profile
@lqt0223
lqt0223 / promise.js
Last active May 30, 2017 12:11
Promise implementation
// Promise Implemenetation
// SOME CONSTANT
var https = require('https')
var URL1 = "https://jsonplaceholder.typicode.com/posts?userId="
var URL2 = "https://jsonplaceholder.typicode.com/posts?id="
// Phase 1: make function in 'then' deferred, and callback with resolved data later
@lqt0223
lqt0223 / sync.html
Last active May 29, 2017 10:13
The difference between 'sync' event and 'sync' attribute in Backbone
<html lang="en">
<head>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<script src="https://cdn.bootcss.com/underscore.js/1.8.3/underscore.js"></script>
<script src="https://cdn.bootcss.com/backbone.js/1.3.3/backbone.js"></script>
</head>
<body>
<script>
// The difference between 'sync' event and 'sync' attribute in Backbone
@lqt0223
lqt0223 / playground.html
Last active May 29, 2017 10:10
Backbone playground
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<script src="https://cdn.bootcss.com/underscore.js/1.8.3/underscore.js"></script>
<script src="https://cdn.bootcss.com/backbone.js/1.3.3/backbone.js"></script>
</head>
<body>
@lqt0223
lqt0223 / filter_object.js
Created April 27, 2017 02:48
22 Filter an object and return key-value pairs that pass the filter
function filterObject(obj, fn){
return Object.keys(obj).filter((key) => {
var e = obj[key];
return fn(e);
}).map((e) => {
return {
key: e,
value: obj[e]
};
});
@lqt0223
lqt0223 / lcs.js
Created April 11, 2017 15:14
21 Longest common subsequence in JavaScript
var str1 = "XMJYAUZ";
var str2 = "MZJAWXU";
var result = lcs(str1, str2);
console.log(result);
// result is { maxLength: 4, sequence: 'MJAU' }
function lcs(str1, str2){
var maxLength = 0, maxCol = 0, maxRow = 0;
var sequence = "";
@lqt0223
lqt0223 / knapsack.js
Created April 11, 2017 11:52
20 0-1 Knapsack problem in JavaScript
/* 0-1 knapsack problem
For an overall introduction to knapsack problem, see https://en.wikipedia.org/wiki/Knapsack_problem
Function name: knapsack
Param:
items: an array of {w: v:} (where 'w' stands for weight, and 'v' stands for value)
capacity: a positive integer number
Will return max sum value that can reach, and the chosen subset to add up to the value.
@lqt0223
lqt0223 / bitwise.js
Created April 9, 2017 12:02
19 Bitwise operation and utilities
// bitwise operation and utilities
// Left shift by 1, will multiply the number by 2
console.log(2 << 1); // return 4
// Right shift by 1, will divide the number by 2.
// In JavaScript, the decimal part will be floored
console.log(2 >> 1); // return 1
console.log(3 >> 1); // return 1
@lqt0223
lqt0223 / graph.js
Last active April 8, 2017 16:06
18 Weighted & Directional Graph and Dijkstra's Algorithm in JavaScript
// Weighted directional graph implementation and Dijkstra's algorithm
class Graph {
constructor() {
this.vertices = [];
this.edges = [];
}
// add a sequence of vertices to the graph
@lqt0223
lqt0223 / deepcopy.js
Last active April 7, 2017 03:49
17 Deep copy in JavaScript
// You are welcome to have a test on this function to check if it supports deep copy for very complex nested objects.
function deepCopy(o){
if(typeof o === "object" && o !== null){
var _o = new o.constructor();
for(var k in o){
_o[k] = deepCopy(o[k]);
}
return _o;
}else{
return o;
@lqt0223
lqt0223 / p&c.js
Last active December 21, 2017 02:46
16 Permutation and Combination in JavaScript
/*
Permutation in mathmatics stands for all possible order for a set.
Combination in mathmatics stands for all possible ways of selecting items from a set.
Find more at
https://en.wikipedia.org/wiki/Permutation
https://en.wikipedia.org/wiki/Combination
*/