Instantly share code, notes, and snippets.

View foo.md

Questions: https://github.com/regehr/assert_quiz/blob/master/assert_quiz.md

  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) {
printf("%d\n",data[j]);
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!))
View Median.py
# 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.
View tsp_long.py
#!/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
View PlumbersCoins.py
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);
}
View A.java
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();
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
View num.mp
beginfig(1)
path digit;
numeric d;
string digit_string;
u := 5mm;
max_base = 10;
max_num = 99;
digit_string = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
color c_d[];