Skip to content

Instantly share code, notes, and snippets.

View richard-to's full-sized avatar

Richard To richard-to

View GitHub Profile
@richard-to
richard-to / simple_calc_with_vars.jison
Created December 24, 2013 06:30
Simple calculator language with variable substitution using jison
%lex
%{
var parser = yy.parser;
%}
%%
\n return 'NEWLINE'
\s+ /* skip whitespace */
@richard-to
richard-to / owari.py
Created December 24, 2013 06:43
Owari with Alpha Beta Pruning and MySQL transposition table. Can't remember if this code actually works anymore, but was using it to try to find all possible moves in Owari. Unfortunately there are probably over a hundred million moves and my old desktop ran out of RAM at about 22 million. Only 4GB installed.
import time
import MySQLdb as mdb
from warnings import filterwarnings
import config
filterwarnings('ignore', category = mdb.Warning)
conn = mdb.connect(*config.db)
@richard-to
richard-to / gist:8122471
Last active January 1, 2016 09:08
Running list basic numpy functions usage so I don't forget.
# Create a numpy array from a list
data = np.array(listData)
# Get a vector of elements in 2nd column -> [1, 2, 3]
# Remeber that the arrays are 0 indexed so technically it would be the 3rd column
colVec = data[:,2]
# Get a vector of elements 1st row -> [1, 2, 3]
# Don't forget the comma!
rowVec = data[1,:]
@richard-to
richard-to / fisher-yates-shuffle.js
Created January 21, 2014 06:51
Fisher Yates Shuffle implementation in javascript that returns shallow clone of new list
// Fisher-Yates Shuffle
var shuffle = function(list_in) {
var pick, temp;
var list = list_in.slice(0);
var i = list.length;
while (--i > 0) {
pick = Math.floor(Math.random() * i);
temp = list[i];
list[i] = list[pick];
list[pick] = temp;
@richard-to
richard-to / image_lossy_optimize.sh
Created January 22, 2014 07:01
Simple bash script to apply lossy optimization to jpgs in folder
#!/bin/bash
rm -rf out
mkdir out
for d in $(ls)
do
if [[ $d == *.jpg ]]
then
convert $d "out/$d.png"
@richard-to
richard-to / gist:8797504
Created February 4, 2014 03:01
Google App Script to parse specific emails and write to Google Sheets
// Modified from http://pipetree.com/qmacro/blog/2011/10/automated-email-to-task-mechanism-with-google-apps-script/
// Globals, constants
var LABEL_PENDING = "pending";
var LABEL_DONE = "done";
// processPending(sheet)
// Process any pending emails and then move them to done
function processPending_(sheet) {
var insertionSort = function(list) {
var value = 0;
var pos = 0;
for (var i = 1; i < list.length; ++i) {
value = list[i];
pos = i - 1;
while (pos >= 0 && value < list[pos]) {
list[pos + 1] = list[pos];
--pos;
}
var bubbleSort = function(list) {
var n = list.length;
var swapped = false;
var i = 0;
var temp = 0;
do {
swapped = false;
for (i = 1; i < n - 1; ++i) {
if (list[i - 1] > list[i]) {
temp = list[i];
string type2str(int type) {
string r;
uchar depth = type & CV_MAT_DEPTH_MASK;
uchar chans = 1 + (type >> CV_CN_SHIFT);
switch ( depth ) {
case CV_8U: r = "8U"; break;
case CV_8S: r = "8S"; break;
case CV_16U: r = "16U"; break;
@richard-to
richard-to / data.js
Last active August 29, 2015 13:57
Experimenting with modified Huffman codes to compress sprite image data
var waterData = [55,56,56,56,56,56,56,57,55,56,56,56,56,55,55,56,56,56,57,56,56,56,56,56,55,55,56,57,57,56,56,56,55,55,55,56,56,56,56,57,55,56,56,56,56,56,55,56,56,56,57,56,56,56,56,56,55,55,56,56,57,56,56,56,56,56,55,56,56,56,56,56,57,55,56,56,56,56,55,55,57,57,57,56,56,56,56,55,55,56,56,56,57,56,56,56,56,56,56,55,55,57,56,56,57,55,56,56,56,55,55,55,57,56,56,57,57,57,56,55,56,56,56,57,57,56,56,56,57,57,57,57,55,56,57,57,57,55,56,56,56,55,56,57,56,56,56,56,56,57,57,55,57,57,57,57,57,57,57,57,56,56,56,57,56,55,56,56,56,55,57,56,55,56,57,57,56,56,56,56,56,55,55,55,55,55,55,56,56,56,56,57,56,56,56,57,56,55,55,55,55,56,57,56,55,55,55,55,55,56,56,56,55,55,57,56,56,56,55,55,56,56,56,57,55,55,55,55,55,55,56,56,55,55,55,56,55,55,56,56,55,55,56,55,55,56,57,56,56,56,56,55,55,55,55,55,56,55,55,56,56,57,57,56,56,56,55,55,55,57,56,56,56,55,55,55,57,57,57,56,56,56,56,55,56,56,56,56,56,55,55,56,56,56,57,56,56,56,57,55,57,57,57,57,56,56,55,57,57,56,56,56,56,56,56,55,56,56,56,56,56,56,55,56,56,56,57,57,57,57,57,55,57,56,56,57