Skip to content

Instantly share code, notes, and snippets.

View rgrig's full-sized avatar

Radu Grigore rgrig

View GitHub Profile
beginfig(1)
path digit;
numeric d;
string digit_string;
u := 5mm;
max_base = 10;
max_num = 99;
digit_string = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
color c_d[];
@rgrig
rgrig / nontrans_dice_5.txt
Created June 25, 2014 08:28
nontransitive triples of dice with 5 faces
22225 13333 11444
23333 12255 11444
23333 22225 11444
33333 12255 11444
33333 22225 11444
33333 22235 11444
33333 22245 11444
33333 22255 11444
33333 22255 11445
33333 22255 12444
@rgrig
rgrig / A.java
Created July 3, 2014 07:45
getOnlyElement
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class A {
public static <T> T getOnlyElement(Iterable<T> xs, Optional<T> e, Optional<T> xx) {
Iterator<T> i = xs.iterator();
if (!i.hasNext()) {
if (e.isPresent()) return e.get();
int power(int x, unsigned int y) {
if (y == 0) return 1;
if (y == 2) return x * x;
if (y&1) return x * power(power(x, y/2),2);
return power(power(x, y/2),2);
}
from sys import stdin, stdout
import random, math
def tour_length(G, xs):
s = sum(G[xs[i-1]][xs[i]] for i in range(len(xs)))
n = len(xs)
return s + min(G[0][xs[i]] - max(G[xs[i]][xs[(i+1)%n]], G[xs[i]][xs[(i-1)%n]])
for i in range(n))
@rgrig
rgrig / tsp_long.py
Created September 22, 2014 07:03
some rather bad euclidean tsp solver
#!/usr/bin/env python3
from copy import copy
from math import atan2, pi, sqrt
from random import randrange, seed, shuffle
from sys import exit, stderr
from time import time
eps = 1e-9
@rgrig
rgrig / Median.py
Last active August 29, 2015 14:11
computing the median for a list of integers
# NOTES:
# - I didn't pay attention to what happens for lists of even length
# - Since the data is random, the algorithms need not be randomized. I put
# randomization in because calls to randrange/sample/etc might take
# significant time, and it's not really OK to ignore that time.
# - Since the data is random, Python's sort should be disadvantaged, at least
# when M >> N. (The main idea of Timsort is that it exploits patterns
# in data.)
# - As expected, Timsort does better when N>M than when M>N. I didn't expect it
# to bo _so_ much better, though.
@rgrig
rgrig / fib.hs
Created April 25, 2015 18:08
memoization in Haskell
import Data.Array
import Debug.Trace
tabulate :: (Ix a) => (a,a) -> (a -> b) -> Array a b
tabulate bounds f = array bounds [(i,f i) | i <- range bounds]
dp :: (Ix a) => (a,a) -> ((a->b) -> a -> b) -> a -> b
dp bounds f = (memo!) where
memo = tabulate bounds (f (memo!))
\documentclass{article}
%include polycode.fmt
\begin{document}
> import Array
> import Data.Bits
> import Data.Char
> import Data.Function
> import qualified Data.IntSet as IS
import qualified Data.IntMap as M
import Maybe
fillBin capacity ws@(w:ws') weight count
| newWeight > capacity = (ws, weight, count)
| otherwise = fillBin capacity ws' newWeight (succ count)
where newWeight = weight + w
go period capacity bins weights acc state seen
| bins == 0 = acc