Skip to content

Instantly share code, notes, and snippets.

View kragen's full-sized avatar

Kragen Javier Sitaker kragen

View GitHub Profile
#!/usr/bin/python
import time
def compute_perms(str):
if len(str) == 2:
return [ str, "%s%s" % (str[1], str[0]) ]
else:
accum = []
char = str[-1]
#!/usr/bin/python
"""Natural-order sorting, supporting embedded numbers.
foo9bar2 < foo10bar2 < foo10bar10
"""
import random, re, sys
def natsort_key(item):
chunks = re.split('(\d+(?:\.\d+)?)', item)
-- more notes on this issue at http://gist.github.com/106582 and http://gist.github.com/109036
-- Here are the table definitions I’m using.
create table visits (plano text,
"column" text,
"row" text,
route text);
create table square (plano text,
"column" text,
"row" text,
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Try out my Python implementation of the relational algebra on a real query.
I wrote it a few years back, and then I realized that the SQL version
of this query was much worse than the relational algebra version, so I
thought I would see if the query in my Python implementation of part
of the relational algebra was also better than the SQL version.
Here’s the original relational algebra source code:
Tracing to 7.238.241.207.in-addr.arpa[#ptr] via A.ROOT-SERVERS.NET, maximum of 3 retries
A.ROOT-SERVERS.NET [.] (198.41.0.4)
|\___ DILL.ARIN.NET [207.in-addr.arpa] (192.35.51.32)
| |\___ ns1.archive.org [238.241.207.in-addr.arpa] (208.70.31.236)
| \___ ns2.archive.org [238.241.207.in-addr.arpa] (208.70.31.251)
|\___ INDIGO.ARIN.NET [207.in-addr.arpa] (192.31.80.32)
| |\___ ns1.archive.org [238.241.207.in-addr.arpa] (208.70.31.236)
| \___ ns2.archive.org [238.241.207.in-addr.arpa] (208.70.31.251)
|\___ Y.ARIN.NET [207.in-addr.arpa] (192.42.93.32)
| |\___ ns1.archive.org [238.241.207.in-addr.arpa] (208.70.31.236)

(Previous notes are at http://gist.github.com/106231 and http://gist.github.com/106582. Current version is at http://canonical.org/~kragen/binary-relations.html.)

Here’s my problem, expressed in Prolog. I have two basic relations to start with:

  • visits(Page, Column, Row, Route), which says that route Route visits the map square on page Page (one of 1..36), in column Column (one of 'ABCD'), in row Row (one of 1..6).
  • square(Page, Column, Row, X, Y), which gives the X and Y positions of the given square in a global coordinate system. Physically
watchdog_dev=> create table foo (x integer) with oids;
CREATE TABLE
watchdog_dev=> insert into foo (x) values (3) ;
INSERT 65976 1
watchdog_dev=> insert into foo (x) values (3) ;
INSERT 65977 1
watchdog_dev=> select oid, * from foo;
oid | x
-------+---
65976 | 3
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""I was surprised early one morning, lying awake in bed,
to realize that the graph of 2d6 probabilities was a sort of
piecewise-linear approximation to the bell curve,
and that the 3d6 probability graph sort of looked like
a piecewise-quadratic approximation.
So I wrote this program to compute and display
vanishing triangles of dice probabilities.
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""I was surprised early one morning, lying awake in bed,
to realize that the graph of 2d6 probabilities was a sort of
piecewise-linear approximation to the bell curve,
and that the 3d6 probability graph sort of looked like
a piecewise-quadratic approximation.
So I wrote this program to compute and display
vanishing triangles of dice probabilities.
#!/usr/bin/python
# -*- coding: utf-8; -*-
"""Asks, "What are you doing right now?" at random, averaging every 15 minutes.
Results are appended to `~/waydrn15.log` by default.
"""
import sys, os, Tkinter, time, random