Skip to content

Instantly share code, notes, and snippets.

Avatar

William He Williammer

View GitHub Profile
View math.sqrt.bsearch.py
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 Dec 26, 2019
Some basic functions for the bitwise operations
View bitwise.basic.js
// 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
View node-rename-dir-files.js
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 Jun 18, 2018
jsBasic.bindRight.js - implementation for the bind function that accept arguments from right most to the left.
View jsBasic.bindRight.js
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 Sep 14, 2018
backpack solution recur/dynamic/greedy with javaScript.
View algorithm.backpack.js
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
jsPatterns.recur.trampoline.js
View jsPatterns.recur.trampoline.html
<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
HashTable in js, with simple Hash and the better Horner's hash method.
View algorithm.DS.HashTable.js
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) {
View algorithm.DS.doubly-linklist.js
@Williammer
Williammer / algorithm.DS.stack-arr-noPushPop-impl.js
Last active May 15, 2016
Stack data structure in javaScript.
View algorithm.DS.stack-arr-noPushPop-impl.js
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
Flip the strings down.
View str_flipDown.js
(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) {