Skip to content

Instantly share code, notes, and snippets.

Radu Grigore rgrig

Block or report user

Report or block rgrig

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile


  1. bad, should handle error sensibly
  2. bad, debug builds don't even alloc
  3. bad, should handle error sensibly
  4. ?, assert ok, but code around clearly bad: the hash_function has no idea what lenght is, and no modulo done after
  5. bad, should handle error sensibly
  6. kinda ok. You probably want a way to turn off expensive assertions, though. This way you can run prog with all asserts on small tests, and with cheap asserts on big tests (an with no asserts in release -- if you so fancy).
View gen.cpp
include <stdio.h>
int main() {
const int n = 200;
int data[n];
data[0]=0; data[1]=2;
int i, j;
i = j = 0;
int state = 0;
while (j < n) {
rgrig / fib.hs
Created Apr 25, 2015
memoization in Haskell
View fib.hs
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!))
rgrig /
Last active Aug 29, 2015
computing the median for a list of integers
# - 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 /
Created Sep 22, 2014
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
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))
View gist:9ab076b294fe838f088d
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);
rgrig /
Created Jul 3, 2014
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();
rgrig / nontrans_dice_5.txt
Created Jun 25, 2014
nontransitive triples of dice with 5 faces
View nontrans_dice_5.txt
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
path digit;
numeric d;
string digit_string;
u := 5mm;
max_base = 10;
max_num = 99;
digit_string = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
color c_d[];
You can’t perform that action at this time.