Skip to content

Instantly share code, notes, and snippets.

@righthandabacus
righthandabacus / minrange.py
Created February 16, 2019 20:19
Min range to cover all lists
#!/usr/bin/env python
"""
Given $k$ sorted lists, each of size $n$, find a range $(p,q)$ such that:
1. q-p is minimized
2. for any list, there is an element $r$, such that $p <= r <= q$
Example:
[4, 10, 15, 24]
[0, 9, 12, 20]
@righthandabacus
righthandabacus / permutation-heap.py
Created February 9, 2019 01:24
Using Heap Algorithm to generate all permutation
def heapalgo(items, n):
"""heap algorithm to generate all permutations in-place
Args:
items: list of items
n: initially the list of items, only permute on the first n-items
Generates:
a permutation of first n items of the list, in-place shuffle performed
"""
assert items, "cannot go with empty list"
@righthandabacus
righthandabacus / atexit.py
Created January 2, 2019 04:04
Exit signal - work around the lack of UNIX signal in Windows
import atexit
import time
import logging
import signal
import os
def sighandler(sigcode, x):
print("Signal Exit!!", flush=True)
print([sigcode, x], flush=True)
exitcall()
@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
@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 / 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 / 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 / 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 / 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 / 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)