Skip to content

Instantly share code, notes, and snippets.

# -*- coding: utf-8 -*-
top_level_domains=[
u'ac',
u'com.ac',
u'edu.ac',
u'gov.ac',
u'net.ac',
u'mil.ac',
u'org.ac',
u'ad',
#include <iostream>
using namespace std;
class memoize{
public:
memoize(int);
double fib(int);
private:
double *memo;
int n;
class Memoize:
def __init__(self, function):
self.function=function
self.memoized={}
def __call__(self, *args):
try:
return self.memoized[args]
except KeyError:
self.memoized[args]=self.function(*args)
return self.memoized[args]
import functools, json
from siteparse import IndividualPage
GRAPH={'a':['b','c'], 'b': ['d'], 'c': ['d', 'a'], 'd': ['b', 'c']}
import urllib2, BeautifulSoup, urlparse
class IndividualPage(object):
def __init__(self, url):
self.source=urllib2.urlopen(url)
def permute(queue, final = []):
for item in queue:
remaining_queue, pending = [e for e in queue if e is not item], final + [item]
if remaining_queue:
for more in permute(remaining_queue, pending):
yield more
else:
yield pending
@dagoof
dagoof / throttle.py
Created November 17, 2010 13:28
throttling decorator
import datetime
from functools import wraps
def total_seconds(td):
return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10.0**6
def throttle(time):
def _throttle(f):
f.time = time
f.time_since_last = datetime.datetime.now() - datetime.timedelta(seconds = time)
@dagoof
dagoof / json-decoder.scm
Created July 13, 2011 00:47
scheme JSON decoder, uses list operations on string->list instead of streams. dicts map to gambit scheme tables
(load "~~/lib/syntax-case")
(define-syntax let-values
(syntax-rules
()
((_ (mvbinding ...) . body)
(let-values foo (mvbinding ...) . body))
((_ name () . body)
(let name () . body))
((_ name ((vars mv) . mvbindings) . body)
@dagoof
dagoof / json-encoder.scm
Created July 15, 2011 11:29
scheme json encoder; assumes gambit scheme tables for dicts
(load "~~/lib/syntax-case")
(define-syntax case-cond
(syntax-rules
(else)
((_ e (c r) ... (else d))
(cond ((c e) r) ... (else d)))))
(define (curry f . c) (lambda x (apply f (append c x))))
@dagoof
dagoof / chain.scm
Created July 16, 2011 05:58
Scheme expression threading (->>)
; Scheme implementation of 'expression threading'.
; chains the given argument through each expression; such as
; (->> 5
; (+ 3)
; (* 2)) === 16
;
; or even better:
;
; (->>
; "lets find some even chars!"
@dagoof
dagoof / partial.go
Created October 13, 2011 14:18
quick test of first class functions in go
package main
import "fmt"
type IntegerReduce func(...int) int
func main() {
sum := func(n ...int) int {
sum := 0
for _, v := range n {
sum += v