Skip to content

Instantly share code, notes, and snippets.

View alanduan's full-sized avatar

Alan Duan alanduan

View GitHub Profile
@alanduan
alanduan / prime.py
Created July 19, 2012 02:43
get the nth prime number.
import math
def prime(n):
p = [2]
x = 3
while len(p) < n:
isprime = True
for i in p:
if i > math.sqrt(x):
break
@alanduan
alanduan / fibonacci.py
Created July 19, 2012 07:48
Get the nth Fibonacci number.
def f_simple_yet_stupid(n):
if n == 1 or n == 2:
return 1
return f(n - 1) + f(n - 2)
def f(n):
a = [0, 0, 1]
for i in range(n - 1):
a[:2] = a[1:]
a[2] = a[0] + a[1]
@alanduan
alanduan / test_infinitescroll.html
Created July 20, 2012 05:15
try infinite scroll
<script src=jquery.js></script>
<script src=jquery.infinitescroll.js></script>
<div id="content">
<div class=navigation><a href="page1.html">page1</a></div>
<div class=post>
<p>root:x:0:0:root:/root:/bin/bash
<p>daemon:x:1:1:daemon:/usr/sbin:/bin/sh
<p>bin:x:2:2:bin:/bin:/bin/sh
<p>sys:x:3:3:sys:/dev:/bin/sh
@alanduan
alanduan / name of symbols.md
Created August 29, 2013 07:15
Common Documentation
\   backslash
`   backtick
*   asterisk
_   underscore
{}  curly braces
[]  square brackets
()  parentheses
#   hash mark
+   plus sign
@alanduan
alanduan / ad
Created September 5, 2013 07:15
ad
45rt$RFV
@alanduan
alanduan / cgi_demo.cgi
Created September 10, 2013 06:35
cgi demo
#!/usr/bin/env python
import cgi
import os
import sys
import Cookie
def init_page(user='guest'):
return '''Content-Type: text/html
@alanduan
alanduan / gist:6988109
Created October 15, 2013 08:00
buffer disabled
class Unbuffered:
def __init__(self, stream):
self.stream = stream
def write(self, data):
self.stream.write(data)
self.stream.flush()
def __getattr__(self, attr):
return getattr(self.stream, attr)
import sys
@alanduan
alanduan / heartbeat.py
Created October 13, 2015 00:28
heartbeat demo
import threading
def foo():
alive = True
c = threading.Condition()
def heartbeater():
heartbeat_interval = 60
heartbeat_threshold = 10
c.acquire()
while alive:
import os
import re
from itertools import product
# bx(n) generates all bit strings with length n
bx = lambda x: (('{:0%db}' % x).format(i) for i in xrange(2**x))
# bit8s(s) iterates every 8-bit strings from s
bit8s = lambda x: (x[i:i+8] for i in range(0, len(x), 8))
from itertools import product
class ConsoleDisplay(object):
def __init__(self, win, rows, cols):
win.resize(rows, cols)
self._win = win
self.rows = rows
self.cols = cols