Skip to content

Instantly share code, notes, and snippets.

View markwatson's full-sized avatar
💻
hacking the mainframe

Mark Watson markwatson

💻
hacking the mainframe
View GitHub Profile
@markwatson
markwatson / Base File.sublime-settings
Created November 8, 2011 17:27
Sublime user file settings
{
"caret_style": "phase",
"color_scheme": "Packages/Color Scheme - Default/Blackboard.tmTheme",
"default_line_ending": "unix",
"draw_white_space": "all",
"ensure_newline_at_eof_on_save": true,
"font_face": "ProggySquareTT",
"font_size": 12,
"line_padding_bottom": 1,
"rulers":
@markwatson
markwatson / knapsack.scala
Created February 11, 2012 15:32
Branch and Bound Knapsack Solution
// Branch and bound knapsack problem
import collection.mutable.PriorityQueue
/** This class represents a thing we can put in the napsack.
*/
class Thing(wIn:Int, vIn:Int) extends Ordered[Thing] {
val w = wIn
val v = vIn
def v_w = v / w
override def toString = "w=%d, v=%d, v/w=%d".format(w, v, v_w)
@markwatson
markwatson / print_unique.py
Created March 8, 2012 18:29
A Python class that only prints unique files. Useful for debugging inner loops.
class PrintUnique(object):
"""
A class that keeps track of what it has printed to stdout and won't
print anything twice.
"""
printed = set()
@classmethod
def write(cls, s):
"""
@markwatson
markwatson / eq.py
Created March 22, 2012 17:59
Find out if all values in a sequence are equal.
def eq(*vals):
"""
Finds out if a bunch of values are equal to each other.
"""
if len(vals) == 0:
return False
else:
return reduce(lambda x, y: x if x == y else not x, vals) == vals[0]
@markwatson
markwatson / iter_utils.py
Created April 13, 2012 19:39
Random python functions for working with iterators.
"""
A number of functions for working with iterators.
"""
def all(iterable):
for element in iterable:
if not element:
return False
return True
@markwatson
markwatson / coroutine_io.py
Created April 26, 2012 19:07
A python IO class that writes to a coroutine.
import io
class CoroutineIO(io.TextIOBase):
"""
Creates an writable IO interface to a coroutine.
"""
def __init__(self, coroutine):
"""
Creates a new IO object with a coroutine. The
coroutine should take no arguments.
@markwatson
markwatson / stats.py
Created May 3, 2012 20:37
Random stats functions
def mean(x): return sum(x) / len(x)
def std_dev(x, mx=None):
if mx is None:
mx = mean(x)
return math.sqrt(sum((i - mx)**2 for i in x)/len(x))
@markwatson
markwatson / pretty_json.py
Last active December 12, 2015 04:58
A sublime text plugin that pretty formats all the select JSON blobs.
import sublime, sublime_plugin
import json
import re
class PrettyJsonCommand(sublime_plugin.TextCommand):
def run(self, edit):
re_json_p = re.compile(r'^\s*(\w+)\s*\(\s*(.*)\s*\)\s*$')
try:
for region in self.view.sel():
@markwatson
markwatson / merge_tuples.rb
Created March 11, 2013 21:40
Merges an array of arrays by adding the elements together.
# Takes an array of totals_tuples and combines them into a single tuple.
def self.merge_tuples(tuples)
tuples.inject([]) do |xs, ys|
l = [xs.length, ys.length].max
zs = []
l.times.each do |i|
zs[i] = (xs[i] || 0) + (ys[i] || 0)
end
zs
end
@markwatson
markwatson / time_card.py
Created July 16, 2013 04:21
A simple time tracker script.
#/usr/bin/env python
"""
time_cards.py
This utility tracks time.
"""
import datetime
import atexit