Skip to content

Instantly share code, notes, and snippets.

View bdw's full-sized avatar
💭
Hacking

Bart Wiegmans bdw

💭
Hacking
View GitHub Profile
@bdw
bdw / Dictionary.php
Created January 7, 2013 21:42
PHP on-disk dictionary. It stores a key-value hash as a binary tree (depth-first), with a 'right offset pointer'. A long time ago, I had methods that manipulated this on-disk, but unfortunately, I don't anymore. I will figure out how, though, and rewrite this into a more honest language.
<?php
class Dictionary {
var $file;
var $index;
var $current;
function Dictionary($file) {
$this->file = $file;
$this->index = array();
@bdw
bdw / tree.go
Last active December 10, 2015 19:58
Binary tree in Go
package main
import (
"fmt"
"strconv"
"io"
"encoding/binary"
)
type BinaryTree struct {
@bdw
bdw / genius.py
Last active June 18, 2019 23:35
Genius - a roman daemon. This is a python context manager which tries to ensure it is the only process identified by a pidfile.
import os
import fcntl
import signal
import time
import contextlib
class Genius(object):
'''
A genius is basically a roman daemon. It differs from python-daemon
in the following ways:
import random
import io
import time
def profile(func):
def wrapper(*args):
start = time.clock()
rv = func(*args)
end = time.clock()
@bdw
bdw / life.py
Created May 28, 2013 21:54
Game of Life met pygame en numpy.
#!/usr/bin/env python
import argparse
import numpy
import pygame
pygame.init()
from pygame.locals import QUIT # Dat deze uit z'n namespace moet is echt stom
# 3 regels:
# 1. Levende cellen met minder dan 2 levende buren gaan dood
@bdw
bdw / volume.js
Created December 26, 2013 16:55
function to calculate volume contained in the 'pools' in a two-dimensional array.
function calculateVolume(heights) {
var left = 0, right = 1, volume = 0;
while (right < heights.length) {
var level = heights[left];
if (heights[right] <= level) {
// tag the 'left cursor' along
left = right;
right = right + 1;
} else {
@bdw
bdw / ve.py
Created February 14, 2014 08:39
Really easy virtualenv wrapper
#!/usr/bin/env python
import argparse
import os
import os.path
import sys
import subprocess
import shutil
try:
import virtualenv
<h3>What is your e-mail address and IRC nick?</h3>
<p>bartwiegmans@gmail.com, brrt on freenode</p>
<h3>What is your web page, blog, or microblog?</h3>
<p><a href="https://plus.google.com/u/0/+BartWiegmans/posts">I have a google+ page</a> - although I don't actually post a lot of things there.</p>
@bdw
bdw / compact.py
Created March 21, 2014 23:02
An algorithm to compact arrays (python). I figured it out but I can't explain why it works.
def compact(a_list):
# strategy - find the first empty and nonempty items
empty, nonempty = 0, 0
while empty < len(a_list) and nonempty < len(a_list):
# ok, so in principle this works really well... why?
# what happens is that nonempty looks for the earliest nonempty
# element and empty looks for the earliest empty element
if a_list[nonempty] is None:
nonempty += 1
@bdw
bdw / broken-re.py
Created March 25, 2014 14:11
showing python re is subtly broken
#!/usr/bin/env python
import re
s = r'"foo \" bar" baz "quix"'
# same regex right?
p_a = r'"(\\"|[^"])*"'
p_b = r'"([^"]|\\")*"'
# no
print(re.match(p_a, s).group(0))