Skip to content

Instantly share code, notes, and snippets.

@righthandabacus
righthandabacus / unzip.rb
Created February 2, 2018 19:21
Extract zip file with filename encoding conversion
#!/usr/bin/env ruby
require 'optparse'
require 'zip'
require 'fileutils'
# parse command line arguments
options = {}
OptionParser.new do |opt|
opt.on('-f FROM_ENC') {|o| options[:from_enc] = o}
@righthandabacus
righthandabacus / kalman.py
Created February 3, 2018 05:45
Smoothing data using Kalman filter
# Kalman filtering
# Prediction:
# x_pre[k] = A*x[k-1] + B*u[k]
# P_pre[k] = A*P[k-1]*A' + Q
# Measurement update:
# K[k] = P_pre[k]*H'*(H*P_pre[k]*H'+R)**(-1)
# x[k] = x_pre[k] + K[k]*(z[k]-H*x_pre[k])
# P[k] = (I-K[k]*H)*P_pre[k]
#
# Simplified with A=H=1, B=0, Q=0:
@righthandabacus
righthandabacus / dedup.py
Last active February 3, 2018 21:23
Remove duplicate files in a directory
#!/usr/bin/env python
from __future__ import print_function
import argparse
import hashlib
import itertools
import os
import multiprocessing as mp
import sys
@righthandabacus
righthandabacus / hkid.py
Created February 16, 2018 03:51
Generate rainbow table for all HKID: To prove hashing HKID is not a security measure
from __future__ import print_function
import itertools
import hashlib
# generate a rainbow table for HKID
# 2017-02-11
def checkdigit(a, i, j, k, l, m, n):
numeric = [ord(a) - 65 + 1, int(i), int(j), int(k), int(l), int(m), int(n)]
weight = range(8,1,-1)
@righthandabacus
righthandabacus / crawlturnout.py
Created March 13, 2018 18:40
收集投票率數據並寫入google spreadsheet
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import datetime
import os
import subprocess
import time
import urlparse
from StringIO import StringIO
@righthandabacus
righthandabacus / dlx.cc
Created May 18, 2018 21:13
Donald Knuth's DLX algorithm on the Dancing Link paper
/* Knuth, Dancing Link, 2000
*
* This program solve the covering set problem using the DLX algorithm in the
* abovementioned paper.
*/
#include <iostream>
#include <vector>
#include <string>
#include <memory> // for shared_ptr
@righthandabacus
righthandabacus / randomwalk.jl
Created May 29, 2018 15:50
Plotting random walk (in Julia)
"""
generate n random walks from 0 to T with N steps
Returns:
t (range) of time from 0 to T with N steps
W (nxN array) of random walks
"""
function rwalks(T,N,n)
dt = T/N
dW = sqrt(dt)*randn(n,N)
@righthandabacus
righthandabacus / multivariate.jl
Created June 8, 2018 16:07
Generating multivariate normal distribution with plot
# 2D multivariate normal
# with covariance matrix [3 2; 2 5], i.e. Var(x)=3, Var(y)=5, Cov(x,y)=2
# Break down covariacne matrix with Cholesky decomposition and multiply with iid std normal random numbers
Sigma = [4 9; 9 25]
S = chol(Sigma) # upper triangular part, i.e. S' * S == Sigma
iidnorm = randn(2,1000)
rv = S' * iidnorm # 2x1000 matrix of 1000 random number pairs conforms to the covariance matrix Sigma
# Plot in 2D
@righthandabacus
righthandabacus / subcommand.py
Created August 7, 2018 17:00
python argparse sub command decorator
import argparse
# Expose argparser into global scope
_parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
_subcmds = _parser.add_subparsers(
title="Commands",
description="Available actions",
dest="subcmd")
# Subcommand argparse decorator
@righthandabacus
righthandabacus / stalinsort.py
Created November 3, 2018 20:30
Stalin Sort
def stalinsort(unsorted):
"""Stalin sort: O(n) sort algorithm that iterate down a
list of elements, any element which is out of order is
eliminated.
https://www.facebook.com/ProgrammersCreateLife/photos/a.241809332534619/1934139766634892/?type=1&theater
Args:
unsorted: Iterable of elements that support >= operator
Returns:
A list of subset of the input