Skip to content

Instantly share code, notes, and snippets.

import random
alphabet = 'abcdefghijklmnopqrstuvwxyz '
def random_letter():
return alphabet[random.randint(0,len(alphabet)-1)]
def random_string(length):
return ''.join([random_letter() for i in range(length)])
@vilterp
vilterp / x
Created February 2, 2009 01:32
strikethrough ubiquity command
function cmd_strikethrough() {
var doc = context.focusedWindow.document;
if (doc.designMode == "on")
doc.execCommand("strikethrough", false, null);
else
displayMessage("You're not in a rich text editing field.");
}
cmd_italic.description = "If you're in a rich-text-edit area, strikes through the selected text.";
@vilterp
vilterp / converter.py
Created February 6, 2009 01:25
converts from one unit to another
conversions = [
# length
('inches','feet',lambda x: x/12.0),
('feet','inches',lambda x: x*12),
('meters','feet',lambda x: x*3.2808399),
('feet','meters',lambda x: x*0.3048),
('inches','centimeters',lambda x: x*2.54),
('meters','centimeters',lambda x: x*100),
('centimeters','meters',lambda x: x/100),
# stoichiometry
@vilterp
vilterp / x
Created February 8, 2009 15:30
CmdUtils.CreateCommand({
name: "gcal",
takes: {"expression" noun_arb_text},
description: "Uses Google Calculator to evaluate an expression",
icon: "http://www.google.com/favicon.ico/",
homepage: "http://davanum.wordpress.com/",
author: { name: "Davanum Srinivas", email: "davanum AT gmail.com"},
help: "Try it out: issue "calc 22/7 - 1".",
preview: function(pblock, statusText) {
# loosely based on Rails' excerpt helper
# http://rails.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M001713
# TODO: don't break in the middle of words
def excerpts(text, word, options={})
omission = options[:omission] || '...'
radius = options[:radius] || 5
word.class == String ? re = /(#{Regexp.escape(word)})/ : re = word
return nil unless text =~ re
ans = ""; start = 0; last_range = -2..-1
@SuppressWarnings("unchecked")
public class BST extends BinaryTree {
@Override
public Comparable find(Comparable key) {
return findNode(key).getValue();
}
@vilterp
vilterp / sudoku.hb
Created June 19, 2009 22:15
sudoku in Hobbes! bug #94 evident in line 6
class Board {
def init(values) {
@values = values
}
def poss(r,c) {
(((1 to 9).toSet - self.taken_in_col(c)) - self.taken_in_row(r)) - self.taken_in_box(r,c)
}
def taken_in_col(c) {
taken = new Set
for row in 0 to 8 {
@vilterp
vilterp / diff.html
Created July 7, 2009 02:03
very unoptimized diff implementation & test harness based on http://is.gd/1ppey
<html>
<head>
<title>Diff</title>
<style type="text/css" media="screen">
ins {
background-color: lightgreen;
text-decoration: none;
}
del {
background-color: pink;
CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
MAX_RADIX = len(CHARS)
class Bignum:
def __init__(self, digits, radix=10):
# set self.radix
if radix < MAX_RADIX:
self.radix = radix
else:
import csv
import datetime
import time
import pprint
import itertools
def get_data():
f = open('DATA_1334.csv')