Skip to content

Instantly share code, notes, and snippets.

@cpylua
cpylua / once-only.lisp
Created May 13, 2012 13:33
once-only macro in Common Lisp
(defmacro once-only ((&rest names) &body body)
(let ((gensyms (loop for n in names collect (gensym))))
`(let (,@(loop for g in gensyms collect `(,g (gensym))))
`(let (,,@(loop for g in gensyms for n in names collect ``(,,g ,,n)))
,(let (,@(loop for n in names for g in gensyms collect `(,n ,g)))
,@body)))))
(defmacro do-primes ((var start end) &body body)
(once-only (start end)
`(do ((,var (next-prime ,start) (next-prime (1+ ,var))))
@cpylua
cpylua / decorator.py
Created May 13, 2012 14:10
python meta-decorator
import functools
def decorator(d):
"Make function d a decorator: d wraps a function fn."
def _d(fn):
return functools.update_wrapper(d(fn), fn)
return _d
decorator = decorator(decorator)(decorator)
def decorator(annotation):
@cpylua
cpylua / csdn-passwords
Created May 16, 2012 06:21
CSDN leaked passwords summary
----INVALID EMAIL-----hcat18012
----INVALID EMAIL-----1111
----INVALID EMAIL-----darrenhg5atom.com
----INVALID EMAIL-----hcat19000
----INVALID EMAIL-----passport.csdn.net/modifyemail.aspx
Total records: 6428627
1620956(0.252147) user name is part of email account
1463691(0.227683, 0.902980) user name is identical to email account
Distinct passwords: 4037919(0.628115)
# of email service providers: 68815
@cpylua
cpylua / simple-equal.lisp
Created June 20, 2012 12:55
(simple-equal nil '(nil nil))
(defun simple-equal (x y)
"Tests if two lists or atoms are equal."
(or (eql x y)
(and (listp x) (listp y)
(simple-equal (car x) (car y))
(simple-equal (cdr x) (cdr y)))))
(defun simple-equal (x y)
"Tests if two lists or atoms are equal."
(or (eql x y)
@cpylua
cpylua / roc.py
Created August 29, 2012 02:30
ROC and AUC
"""
Calculate AUC of a sample
Reference:
http://binf.gmu.edu/mmasso/ROC101.pdf
"""
import matplotlib.pyplot as plt
# each element is a tuple(positive, score)
sample = [
@cpylua
cpylua / tsort.py
Created September 5, 2012 06:19
Graph topological sort
import random
import copy
graph = {
"3" : {"in" : [], "out" : ["8", "10"]},
"5" : {"in" : [], "out" : ["11"]},
"7" : {"in" : [], "out" : ["11", "8"]},
"11" : {"in" : ["5", "7"], "out" : ["2", "9", "10"]},
"8" : {"in" : ["3", "7"], "out" : ["9"]},
"2" : {"in" : ["11"], "out" : []},
@cpylua
cpylua / strlen.c
Created September 19, 2012 01:32
strlen with no conditional statement
/*
http://blog.exodusintel.com/2012/09/18/reversing-the-interview-process/
*/
typedef int (*ctr)(char *);
int strlen(char *);
int func_x(char *c) { return 1+strlen(c); }
int func_0(char *c) { return 0; }
@cpylua
cpylua / hilber.py
Created September 21, 2012 06:30
Hilbert Curve
# See http://blog.notdot.net/2009/11/Damn-Cool-Algorithms-Spatial-indexing-with-Quadtrees-and-Hilbert-Curves
hilbert_map = {
'a': {(0, 0): (0, 'd'), (0, 1): (1, 'a'), (1, 0): (3, 'b'), (1, 1): (2, 'a')},
'b': {(0, 0): (2, 'b'), (0, 1): (1, 'b'), (1, 0): (3, 'a'), (1, 1): (0, 'c')},
'c': {(0, 0): (2, 'c'), (0, 1): (3, 'd'), (1, 0): (1, 'c'), (1, 1): (0, 'b')},
'd': {(0, 0): (0, 'a'), (0, 1): (3, 'c'), (1, 0): (1, 'd'), (1, 1): (2, 'd')},
}
subquad_map = dict(
(k, dict((quad[0], (pt, quad[1])) for pt, quad in v.iteritems()))
@cpylua
cpylua / pfds
Created December 7, 2012 07:27
Notes about Purely Functional Data Structures
http://cybertiggyr.com/amo/
@cpylua
cpylua / bdyun.py
Created January 27, 2013 01:45
BaiduYun share link decoder
# -*- coding: utf-8 -*-
"""
Decode BaiduYun share link.
http://pan.baidu.com/share/link?shareid=#####&uk=######
The download link expires in a few hours, check `expires`
parameter in the url.
(c) 2013 by Charles Lee<[email protected]>