Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am lukealbao on github.
  • I am lukealbao (https://keybase.io/lukealbao) on keybase.
  • I have a public key ASCa2ZGlr6ZsHFfLwNxDhb4gC_4OhzbFTc5qP4MLhQA-Ggo

To claim this, I am signing this object:

@lukealbao
lukealbao / formatter.js
Created July 11, 2017 15:39
Format some percentages
// Given a function, integerFormat, that takes an array of numbers and returns a new
// array of numbers, like integerFormat([30.1, 60.3, 29.6]) => [30, 60, 30],
// you can create a parameterized function to round to any decimal point:
function roundToNearest (numbers, resolution) {
var leftShift = Math.pow(10, resolution);
return integerFormat(numbers.map(n => n * leftShift)).map(n => n / leftShift);
}
@lukealbao
lukealbao / timers.js
Last active April 17, 2017 20:32
process.nextTIck
'use strict';
// https://nodejs.org/api/process.html#process_process_nexttick_callback_args
//
// Says:
//
// "It runs before any additional I/O events (including timers) fire
// in subsequent ticks of the event loop."
//
// Question is, does "additional" mean events whose listeners are added
/*
* A conversion of the hex2ebcdic.js conversion utility. The original
* exported a function that used a big switch statement. Now it is
* a hash table that will be used by a single `encode` function.
*
* Author: Luke Albao <[email protected]>
* Copyright: First Performance Corporation
*/
// To run this from the command line:
@lukealbao
lukealbao / QuickJSQuestion.js
Last active May 28, 2019 16:30
Quick JS Question
var inventory = {
'000001': {
name: 'Banana Slicer',
price: 2.99
},
'000002': {
name: 'Three Wolves Tea Cozy',
price: 14.95
}
};
@lukealbao
lukealbao / install-zero-mq.sh
Last active August 29, 2015 14:19
Install ZeroMQ to EB AMI
#! /bin/bash -e
##
## To install ZeroMQ, you can download this file and run it directly in the shell:
##
## $> sudo wget <uri> | sh
##
## You can get the <uri> by clicking on the `Raw` button above the Gist.
## Changes:
## * In order to install the latest version, change the version number in the SOURCE
## variable below (line 15).
@lukealbao
lukealbao / sub_anagrams.py
Created February 11, 2015 17:38
Substrings contained in a string
def build_candidates(input_string, lexicon=LEXICON):
code = encode(''.join(re.findall('[a-z]+', input_string.lower())))
d = {}
for word in lexicon:
_code = lexicon.encoded(word)
if code % _code == 0:
d[word] = lexicon[word]
return d.keys()
@lukealbao
lukealbao / encode_word.py
Created February 11, 2015 16:51
string => number
def encode(text, decode=False):
"Credit for this strategy goes to http://stackoverflow.com/a/16872684"
table = {'a':2, 'b':3, 'c':5, 'd':7, 'e':11, 'f':13, 'g':17, 'h':19,
'i':23, 'j':29, 'k':31, 'l':37, 'm':41, 'n':43,'o':47, 'p':53, 'q':59,
'r':61, 's':67, 't':71, 'u':73, 'v':79, 'w':83, 'x':89, 'y':97, 'z':101}
if not decode:
return reduce(mul,[table[char] for char in text]) if text else ''
@lukealbao
lukealbao / alpha-beta-trouble.js
Last active August 29, 2015 14:14
Alpha-Beta Troubles
function rootSearch (root, game, depth) {
var alpha = -Infinity;
var beta = Infinity;
var moves = getMoves(root, game); // Array[Object{player: number, game: number}, ...]
var nodeScore;
var bestMove;
for (var i = 0, l = moves.length; i < l; i++) {
nodeScore = alphaBeta(moves[i].player, moves[i].game, depth,
alpha, beta, true);
@lukealbao
lukealbao / GH Go-to-line
Last active August 29, 2015 14:11
Go-to-line on GitHub code page
/*
* Go to hash location associated with line of code.
* Assigning line in the if clause is done because parseInt
* returns falsy if its result is NaN.
*/
function goToLine(line) {
if (line = parseInt(line)) {
document.location.hash = "#L" + line;
}