Skip to content

Instantly share code, notes, and snippets.

def from_number(r)
third = 1.0 / 3
if r>= 0 and r<= third then choice = "r" end
if r >=third and r<= 2*third then choice = 'p' end
if r>= 2*third and r<= 1 then choice ='s' end
return choice
end
@teh
teh / avahi_publish.py
Created June 8, 2012 08:41
avahi publish
#! /usr/bin/env python
# avahi-alias.py
import avahi, dbus
from encodings.idna import ToASCII
import logging
# Got these from /usr/include/avahi-common/defs.h
CLASS_IN = 0x01
TYPE_CNAME = 0x05
@teh
teh / prar.py
Created June 15, 2012 13:59
Script to download Prairie Home Companion show
"""
Script to download a show from http://prairiehome.publicradio.org/
Depends on rtmpdump (apt-get install rtmpdump)
Usage:
# List shows in January
python prar.py 2012 1
# Download show with index 0
python prar.py 2012 1 0
import System.Environment
import Data.Array.Repa as R
import Data.Array.Repa.Algorithms.Randomish as RR
import Criterion.Main
import Criterion.Config
ra = RR.randomishDoubleArray (ix2 1000 3) 0 1 1
main = defaultMainWith defaultConfig {cfgSamples = ljust 10} (return ()) [
bgroup "pairwise-random" [
@teh
teh / sudoku_smt.py
Created October 17, 2012 07:15
sudoku generator for smtlib input format
import numpy
MODEL = """\
53..7....
6..195...
.98....6.
8...6...3
4..8.3..1
7...2...6
.6....28.
@teh
teh / macchanger.py
Created November 10, 2012 14:55
mac changer
import dbus
bus = dbus.SystemBus()
NM = 'org.freedesktop.NetworkManager'
def get_manager():
proxy = bus.get_object(NM, '/org/freedesktop/NetworkManager')
return dbus.Interface(
proxy, NM)
import random, turtle
def maze(w=10, h=10):
# This is the random spanning tree implementation
seen = set()
# Add some walls to confine the maze
for i in xrange(-1, w+1):
seen.add((i, -1))
seen.add((i, h+1))
@teh
teh / grind_coarseness.py
Created June 8, 2013 14:39
Measure average grind coarseness from a picture.
from PIL import Image
import numpy
from skimage.filter import sobel
from skimage.morphology import watershed
from scipy import ndimage as nd
grind = numpy.asarray(Image.open('grind.png')).mean(axis=2)
edges = sobel(grind)
markers = numpy.zeros_like(grind)
@teh
teh / n_queens.py
Created October 5, 2013 18:04
Solve n-queens with pycosat.
# Solve n-queens problem with picosat
import pycosat
import numpy
import itertools
def get_cnf(N):
cnf = []
# * convert to object because pycosat expects 'int's
# * add 1 because 0 is reserved in pycosat
@teh
teh / sudoku.py
Last active January 31, 2020 17:43
Solve sudoku with a SAT solver.
import pycosat
import numpy
import itertools
WORLDS_HARDEST_RIDDLE_ACCORDING_TO_TELEGRAPH = """\
8........
..36.....
.7..9.2..
.5...7...
....457..