Skip to content

Instantly share code, notes, and snippets.

from math import *
a = 1.5830872612179103E+00
b = 7.3465267498264183E+01
c = -3.0305229706863263E-01
d = 7.3692380600745080E-03
f = 4.2772859170011763E-07
j = 1.4850479626806965E+01
k = -1.6117222974925229E+00
stretch = lambda N, R: c + log(N+b)*a +f*N + d*R + k*log(R+j)
@andres-erbsen
andres-erbsen / estonian.tex
Created March 7, 2012 14:05
(Xe/PDF) LaTex Eesti keelse teksti (Unicode, standardsõnad) kirjutamine. \include{estonian.tex}
% xelatex conditional
\newif\ifxelatex
\ifx\XeTeXglyph\undefined
\xelatexfalse
\else
\xelatextrue
\fi
% Me tahame kirjutada eesti keeles ja kasutada ka mitteameerikalikke tähti
\ifxelatex
@andres-erbsen
andres-erbsen / frac.cpp
Created March 6, 2012 16:47
Minimal fraction class in c++, for olympiad use mostly. Everything except addition (and reduce maybe) is trivial.
#include <cstdio>
typedef long long int lld;
typedef long long unsigned int llu;
lld gcd(lld a, lld b) {
lld temp;
if (b > a) {
// swap
temp = a;
@andres-erbsen
andres-erbsen / komposteeringud.py
Created March 6, 2012 16:45
Arvutab praktikas eristavate bussipiletikomposteeringute arvu (Tallnna jaoks maksimaalselt 211). Proovige veel väita, et neid on tuhandeid.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def possibilities(items, length):
items = tuple(items)
if length < 1:
yield []
else:
for others in possibilities(items,length-1):
for item in items:
@andres-erbsen
andres-erbsen / anybase.py
Created March 6, 2012 16:42
An attempt of writing an universal radix/base converter, that supports both math-style and internet-style number systems.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
@andres-erbsen
andres-erbsen / cyclicmask.py
Created March 6, 2012 16:39
Mask (for signal/image processing), also an example of how code that looks great in one language (Haskell) can look awful in another.
def cyclicmask(A,B,(ki,kj)=(0,0)):
""""
Applies a mask B to A, centering at (ki,kj) and wrapping around the edges.
""""
lAx = len(A)
lA = [len(A[i]) for i in range(lAx)]
return [[ # each element in the result is
sum( # over di
sum( # over dj
A[(i+ki+di) %lAx][(j+kj+dj) % lA[i]]*B[di][dj]
@andres-erbsen
andres-erbsen / fifteenpuzzle.c
Created November 15, 2011 20:45
Solve "fifteen puzzle" of size M*N in O((M+N)*M*N) time and determine solveability in O(M*N) time. Originally started for an EIO task.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <malloc.h>
// input file is named pusle.sis and on the first row there should be
// dimensions of the puzzle, then all tiles should follow (separated by whitespace)
// Example:
// 2 3
// 1 2 3
@andres-erbsen
andres-erbsen / gist:1307797
Created October 23, 2011 19:45
Use a directory as a task queue. Process files in they were added and keep track of progress.
import os, glob
import cPickle as pickle
class QueueDir:
def __init__(self,dir,pattern='*',statefilename='.queuedir.pc'):
self.statefilename = statefilename
self.pattern = pattern
self.dir = dir
self.file_in_progress_mtime = None
@andres-erbsen
andres-erbsen / cached.py
Created October 23, 2011 19:28
Caching decorator for python, like functools.lru_cache in Python 3.2+ without LRU. Written for easy debugging of dynamic programming algorthms. Also includes that caches to a pickle file, but this is only useful for very timeconsuming calls like web reque
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import wraps
try:
import cPickle as pickle
except:
import pickle
def cached(fn):
cache = {}
@andres-erbsen
andres-erbsen / itershuffle.py
Created October 23, 2011 19:22
Shuffle for python iterators. This works by holding `bufsize` items back and yielding them sometime later. This is NOT 100% random, proved or anything.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
def itershuffle(iterable,bufsize=1000):
"""Shuffle an iterator. This works by holding `bufsize` items back
and yielding them sometime later. This is NOT 100% random, proved or anything."""
iterable = iter(iterable)
buf = []