Skip to content

Instantly share code, notes, and snippets.

View gvx's full-sized avatar

Jasmijn Wellner gvx

  • None
  • The Netherlands
View GitHub Profile
@gvx
gvx / bf2c.py
Created May 25, 2012 07:39 — forked from jdp/bf2c.py
Naive brainfuck-to-C transpiler
#!/usr/bin/env python
import argparse
import sys
def tokenize(source):
return list(source)
def parse(tokens):
@gvx
gvx / stop.c
Created September 19, 2012 23:01
A small program that goes to sleep right away and when you wake it, goes right back to sleep.
#include <signal.h>
void stop_self()
{
raise(SIGSTOP);
}
int main()
{
struct sigaction act;
@gvx
gvx / listref.py
Created October 3, 2012 22:28
Simplistic demonstration of Python GC behaviour
import gc
import weakref
class Object(object): pass
l = [Object()]
l[0].l = l
REFS = weakref.WeakKeyDictionary()
REFS[l[0]] = True
@gvx
gvx / units.l
Created October 15, 2012 17:56
Thingy to learn how to use Flex and Bison
%{
#include "units.tab.c"
%}
%option noyywrap
int -?[0-9]+
%%
[ \t] /*empty*/
@gvx
gvx / gist:3954076
Created October 25, 2012 17:08
Daily Programmer #107 difficult
import sys
vowels = set('aeiouy')
consonants = set('bcdfghjklmnpqrstvwxz')
impossible_digraphs = ['aa', 'bg', 'bh', 'bk', 'bq', 'bw', 'bx', 'bz', 'cb', 'cf', 'cg', 'cj', 'cm', 'cp', 'cv', 'cw', 'cx', 'cz', 'dx', 'dz', 'fc', 'fd', 'fg', 'fj', 'fk', 'fn', 'fp', 'fq', 'fv', 'fw', 'fx', 'fz', 'gc', 'gj', 'gk', 'gq', 'gv', 'gx', 'gz', 'hj', 'hk', 'hq', 'hv', 'hx', 'hz', 'ij', 'iy', 'jb', 'jc', 'jd', 'jf', 'jg', 'jh', 'jj', 'jk', 'jl', 'jm', 'jn', 'jp', 'jq', 'jr', 'js', 'jt', 'jv', 'jw', 'jx', 'jy', 'jz', 'kq', 'kv', 'kx', 'kz', 'lj', 'lx', 'md', 'mj', 'mk', 'mq', 'mx', 'mz', 'pg', 'pj', 'pq', 'pv', 'px', 'pz', 'qa', 'qb', 'qc', 'qd', 'qe', 'qf', 'qg', 'qh', 'qi', 'qj', 'qk', 'ql', 'qm', 'qn', 'qo', 'qp', 'qq', 'qr', 'qs', 'qt', 'qv', 'qw', 'qx', 'qy', 'qz', 'rx', 'sx', 'sz', 'tq', 'tx', 'uj', 'uq', 'uw', 'uy', 'vb', 'vc', 'vd', 'vf', 'vg', 'vh', 'vj', 'vk', 'vl', 'vm', 'vn', 'vp', 'vq', 'vr', 'vs', 'vt', 'vv', 'vw', 'vx', 'vz', 'wj', 'wk', 'wp', 'wq', 'wv', 'ww', 'wx', 'wz', 'xb', 'xf', 'xg', 'xj', 'xk', 'xm',
@gvx
gvx / testveri.py
Last active December 11, 2015 17:49
An experiment with metaclasses, with dynamic type verification
from veri import Verified
class Something(metaclass=Verified):
# a single type
some_int = int
some_str = str
# a 'struct' using tuples
some_tuple = (int, int, str)
# a list of integers
@gvx
gvx / gist:5696885
Created June 3, 2013 08:35
Extract page numbers that haven't been included in index.txt yet
last = None
with open('index.txt') as f:
for line in f:
new = int(line.decode('utf-8').split('\t', 1)[0])
if last is not None:
if last - new > 1:
if last - new == 2:
print new + 1
else:
@gvx
gvx / roundrect.lua
Created February 18, 2014 15:13
Rounded rectangles
function love.graphics.roundrect(mode, x, y, width, height, xround, yround)
local points = {}
local precision = (xround + yround) * .1
local tI, hP = table.insert, .5*math.pi
if xround > width*.5 then xround = width*.5 end
if yround > height*.5 then yround = height*.5 end
local X1, Y1, X2, Y2 = x + xround, y + yround, x + width - xround, y + height - yround
local sin, cos = math.sin, math.cos
for i = 0, precision do
local a = (i/precision-1)*hP
class LinkedList:
next = None
val = None
def __init__(self, val):
self.val = val
def add(self, val):
if self.next is None:
self.next = LinkedList(val)
@gvx
gvx / gist:9765299
Created March 25, 2014 16:15
2 woorden 9 letters
from random import choice
with open('/usr/share/dict/nederlands') as f:
words = f.read().split('\n')
def get_random_pair():
w1 = choice(words)
w2 = choice(words)
return len(w1) + len(w2), w1, w2