This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A number of functions for working with iterators. | |
""" | |
def all(iterable): | |
for element in iterable: | |
if not element: | |
return False | |
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"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": |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def super_split(string, delim): | |
segment = '' | |
for c in string: | |
if c in delim: | |
yield segment | |
segment = '' | |
else: | |
segment += c | |
yield segment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |