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 / 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 / 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 / jsPatterns.recur.trampoline.html
Last active May 31, 2016 15:37
jsPatterns.recur.trampoline.js
<h1 class="index_title">
About Me
</h1>
<br/>
<p class="selfy">
Hi there, my name is William He or Weinan He in Chinese. I'm currently living in Hong Kong and working at <a target="_blank" href="https://www.accedo.tv/">Accedo</a> as the Software Developer.
<br/>
<br/> My current job is to develop cool Web TV applications, which is one type of web application, with the amazing javaScript. I love to explore and learn more about the web technology world and develop some useful applications in my spare time!
<br/>
@Williammer
Williammer / algorithm.DS.HashTable.js
Last active May 22, 2016 13:53
HashTable in js, with simple Hash and the better Horner's hash method.
function HashTable() {
this.table = new Array(137);
this.simpleHash = simpleHash;
this.betterHash = betterHash;
this.showDistro = showDistro;
this.put = put;
this.get = get;
}
/*function put(data) {
@Williammer
Williammer / algorithm.DS.doubly-linklist.js
Last active May 15, 2016 14:31
LinkList implementations.
@Williammer
Williammer / algorithm.DS.stack-arr-noPushPop-impl.js
Last active May 15, 2016 06:16
Stack data structure in javaScript.
function Stack() {
// this._size = 0;
// this._storage = {};
this._storage = []; // array could be more performant due to array browser optimize
}
Stack.prototype.push = function (data) {
// var size = ++this._size;
var len = this._storage.length;
this._storage[len] = data;
@Williammer
Williammer / str_flipDown.js
Last active May 9, 2016 15:42
Flip the strings down.
(function () {
function flip() {
var result = flipString(document.f.original.value.toLowerCase());
document.f.flipped.value = result;
}
function flipString(aString) {
var last = aString.length - 1;
var result = new Array(aString.length)
for (var i = last; i >= 0; --i) {