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 sqrt_binary(num): | |
if num > 1: | |
low = 1 | |
high = num | |
else : | |
low = num | |
high = 1 | |
mid = float(low) + float((high - low) / 2) | |
while abs(mid ** 2 - num) > 0.000001: | |
if mid ** 2 < num: |
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
// right shift of 0 makes the num unsigned: https://stackoverflow.com/questions/9939760/how-do-i-convert-an-integer-to-binary-in-javascript | |
const numToBinary(num) => (n >>> 0).toString(2); | |
const lowBit = (n) => n & -n; // returns the sub-number which is the last 1 in binary of the original number | |
// low Bit operation is used in "Binary indexed Trees" | |
// https://zh.wikipedia.org/zh-hans/%E6%A0%91%E7%8A%B6%E6%95%B0%E7%BB%84 |
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
const fs = require('fs'); | |
function addSchemaName(basePath) { | |
const result = fs.readdirSync(basePath); | |
// console.log(result); | |
result.forEach((path) => { | |
if (path.startsWith('_') || path.endsWith('Schema.js')) { return; } | |
if (path.endsWith('.js')) { | |
const prefix = path.split('.')[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
function max(a, b) { | |
return (a > b) ? a : b; | |
} | |
function knapsackRecur(capacity, size, value, n) { | |
if (n == 0 || capacity == 0) { | |
return 0; | |
} | |
if (size[n - 1] > capacity) { | |
return knapsack(capacity, size, value, n - 1); |
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 queue = []; | |
queue.push(2); // [2] | |
queue.push(5); // [2, 5] | |
var i = queue.shift(); // [5] // i = 2 |
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
/* swap without tmp variable, merely with +/-. | |
Notice: this is restricted to number swap. | |
*/ | |
function swap(a, b){ | |
a = a+b; | |
b = a-b; | |
a = a-b; | |
} |
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
/*! | |
* Pub/Sub implementation | |
* http://addyosmani.com/ | |
* Licensed under the GPL | |
* http://jsfiddle.net/LxPrq/ | |
*/ | |
;(function ( window, doc, undef ) { |
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.prototype.bindRight = function (context, ...firstArgs) { | |
return (...secArgs) => { | |
return this.apply(null, [...secArgs.reverse(), ...firstArgs.reverse()]); | |
}; | |
}; | |
function add(x, y, z) { | |
return 100 * x + 10 * y + z; | |
} |
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 thunkify(fn) { | |
return function() { | |
var args = [].slice.call( arguments ), | |
ctx = this; | |
return function(cb) { | |
var called; | |
args.push(function(){ | |
if (called) return; |
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
struct Node{ | |
int data; | |
Node *left, *right; | |
}; | |
void preOrderPrint(Node *root) | |
{ | |
print(root->name); //record root | |
if (root->left != NULL) preOrderPrint(root->left); //traverse left if exists | |
if (root->right != NULL) preOrderPrint(root->right);//traverse right if exists | |
} |
NewerOlder