Skip to content

Instantly share code, notes, and snippets.

View Williammer's full-sized avatar

William He Williammer

View GitHub Profile
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:
@Williammer
Williammer / bitwise.basic.js
Last active December 26, 2019 06:58
Some basic functions for the bitwise operations
// 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
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];
@Williammer
Williammer / algorithm.backpack.js
Last active September 14, 2018 08:27
backpack solution recur/dynamic/greedy with javaScript.
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);
@Williammer
Williammer / algorithm.DS.queue-arr.js
Last active July 8, 2018 09:20
Normal data structures implemented in languages like javascript etc. plz note that object based data structure perform faster than the array based data structure.
var queue = [];
queue.push(2); // [2]
queue.push(5); // [2, 5]
var i = queue.shift(); // [5] // i = 2
@Williammer
Williammer / jsAlgorithm.swap.js
Last active July 8, 2018 09:10
alternative ways to swap. - several ways to swap without temp variable.
/* 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;
}
@Williammer
Williammer / jsPattern.PubSubImpl.js
Last active July 8, 2018 09:09
jsPattern.PubSubImpl - one compact library-agnostic implementation by addyosmani. Purely for learning purpose.
/*!
* Pub/Sub implementation
* http://addyosmani.com/
* Licensed under the GPL
* http://jsfiddle.net/LxPrq/
*/
;(function ( window, doc, undef ) {
@Williammer
Williammer / jsBasic.bindRight.js
Last active June 18, 2018 10:03
jsBasic.bindRight.js - implementation for the bind function that accept arguments from right most to the left.
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;
}
@Williammer
Williammer / jsPatterns.thunkify.js
Last active November 21, 2016 03:32
Thunk pattern - which seems to be one example of Partial application, is subroutine that helps.
function thunkify(fn) {
return function() {
var args = [].slice.call( arguments ),
ctx = this;
return function(cb) {
var called;
args.push(function(){
if (called) return;
@Williammer
Williammer / Algorithm.DS.binarySearchTree.Impl.cpp
Last active June 25, 2016 08:45
Binary Search Tree Impl and Binary Search related algorithm. PreOrder: Traverse from Root to left sub-tree, then right sub-tree. InOrder: from smallest to largest item,which means it started from left-most leaf. PostOrder: leafs->left subRoot-> rightLeafs-> right subRoot --> Root ??Order: Traverse from Root to each sub-root(left then right).
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
}