Skip to content

Instantly share code, notes, and snippets.

Avatar
💻
hacking the mainframe

Mark Watson markwatson

💻
hacking the mainframe
View GitHub Profile
@markwatson
markwatson / coroutine_io.py
Created Apr 26, 2012
A python IO class that writes to a coroutine.
View coroutine_io.py
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 / iter_utils.py
Created Apr 13, 2012
Random python functions for working with iterators.
View iter_utils.py
"""
A number of functions for working with iterators.
"""
def all(iterable):
for element in iterable:
if not element:
return False
return True
@markwatson
markwatson / eq.py
Created Mar 22, 2012
Find out if all values in a sequence are equal.
View eq.py
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 / print_unique.py
Created Mar 8, 2012
A Python class that only prints unique files. Useful for debugging inner loops.
View print_unique.py
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 / knapsack.scala
Created Feb 11, 2012
Branch and Bound Knapsack Solution
View knapsack.scala
// 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)
View Base File.sublime-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":
View lagrange.py
# Tested on Windows 7 and Python 2.7
# the code
def lagrangian_interpolate(samples):
"""
Takes some samples as a list of tuples and returns a function that's
a lagrangian interpolation of all the samples.
"""
X = 0 # the tuple index of the X variable in the samples
Y = 1 # the tuple index of the Y variable in the samples
View gist:951307
def super_split(string, delim):
segment = ''
for c in string:
if c in delim:
yield segment
segment = ''
else:
segment += c
yield segment
View Dmoz.py
import mechanize
from BeautifulSoup import BeautifulSoup
class Dmoz(object):
def __init__(self):
self.br = mechanize.Browser()
def get_page_urls(self, term):
result = self.br.open("http://www.dmoz.org/search?q="+term)
result_html = result.read()
@markwatson
markwatson / AdjacencyMatrix
Created Mar 2, 2011
Not finished just saving it here...
View AdjacencyMatrix
class AdjacencyMatrix[T: Manifest](size: Int) {
// initialize it
// (It's in column vector format)
val matrix = Array.ofDim[T](size, size)
// helper functions
// takes a list of 3-tuples in the form: (from, to, weight)
def setConnections(connections: List[(Int, Int, T)]) = {
for (x <- connections) {
// make matrix[to][from] = weight