Skip to content

Instantly share code, notes, and snippets.

Books

Algorithms and Data Structures

The Art of Computer Programming (Knuth)

Programming Pearls (Bentley)

Data Structures and Algorithms (Aho, Hopcroft, Ullman)

@gigamonkey
gigamonkey / bits.py
Last active December 20, 2015 19:18
Solution to Oscar's challenge to come up with a way of encoding bit sets by assigning numbers such that bit sets with fewer on bits get lower numbers and the numbers can be mapped back to the bit sets. This is useful if the resulting numbers are then encoded in some sort of variable length encoding, and bit sets with small numbers of on bits are…
#!/usr/bin/env python3
def population(bits, start=0):
return sum(bits[i] for i in range(start, len(bits)))
def product(iterable):
import functools, operator
return functools.reduce(operator.mul, iterable, 1)
def n_choose_k(n, k):
@gigamonkey
gigamonkey / twophase.scala
Last active December 22, 2015 13:29 — forked from johnynek/twophase.scala
What I came up with while mucking around trying to understand your code.
// Run this with scala <filename>
import java.util.concurrent.atomic.AtomicLong
val txIds = new AtomicLong(0)
// Dummied up transaction id provider
def nextTxId = txIds.incrementAndGet
/**
@gigamonkey
gigamonkey / expressions.in
Created September 16, 2013 05:18
Query splitting coding challenge example inputs
T
F
v1
w1
(and v1 v2)
(and w1 w2)
(and v1 w2)
(or v1 v2)
(or w1 w2)
(or v1 w2)
@gigamonkey
gigamonkey / gist:7489116
Created November 15, 2013 18:19
Random doubles in range
import scala.annotation.tailrec
import scala.util.Random
/*
* Pick a random number between to double values, inclusive.
*/
@tailrec def between(low: Double, high: Double, r: Random): Double = {
if (low == high) {
low
} else {
@gigamonkey
gigamonkey / keybase.md
Created March 22, 2014 21:59
Verifying my identity for keybase.io

Keybase proof

I hereby claim:

  • I am gigamonkey on github.
  • I am peterseibel (https://keybase.io/peterseibel) on keybase.
  • I have a public key whose fingerprint is 54F4 4A67 9127 A7FB 5398 B1E1 B4A6 DD7D 735E 60D3

To claim this, I am signing this object:

@gigamonkey
gigamonkey / easy.txt
Last active September 23, 2015 05:08
My first Haskell program -- it didn't work the first time it type checked but I got it there. Simple Sudoku solver based on the search part of Peter Norvig's essay about solving every Sudoku.
. 5 . | . . 1 | 4 7 9
. . 2 | 7 . . | . . 8
. . . | . 4 6 | 2 . .
------+-------+------
. 4 6 | . . 9 | 5 3 7
. . . | . 6 . | . . .
8 9 3 | 5 . . | 6 4 .
------+-------+------
. . 9 | 6 1 . | . . .
1 . . | . . 2 | 3 . .
@gigamonkey
gigamonkey / lisp.py
Created October 22, 2014 21:29
Half implemented (or less) lisp interpreter
#!/usr/bin/env python3
# Reader -- parses text to something, some structure.
# Python lists for lists.
# Instances of Symbol for symbols.
# Numbers for numbers.
# Strings for strings.
# ??? for functions
@gigamonkey
gigamonkey / Foo.java
Created December 29, 2014 17:30
YYYY vs yyyy
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.GregorianCalendar;
public class Foo {
public static void main(String[] argv) {
TimeZone utc = TimeZone.getTimeZone("UTC");
@gigamonkey
gigamonkey / word-search.hs
Last active August 29, 2015 14:17
Word search solver
import Data.Array
import Data.Maybe
import Data.List.Split
import System.Environment
import System.IO
direction "n" = \(r, c) -> (r - 1, c)
direction "s" = \(r, c) -> (r + 1, c)
direction "e" = \(r, c) -> (r, c + 1)
direction "w" = \(r, c) -> (r, c - 1)