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 simple wrapper around BeautifulSoup to quickly find the URLs of all resources referenced by an HTML page.''' | |
import urllib2 | |
import urlparse | |
import BeautifulSoup | |
def make_soup(url): | |
page = urllib2.urlopen(url) | |
contents = page.read() | |
soup = BeautifulSoup.BeautifulSoup(contents) |
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 poplib, email, getpass | |
M = poplib.POP3_SSL('pop.gmail.com', 995) | |
username = raw_input("gmail username: ") | |
M.user(username) | |
M.pass_(getpass.getpass('%[email protected] password: ' % username)) | |
status, emails, octets = M.list() | |
if status.startswith('+OK'): | |
print 'new emails:', len(emails) |
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
"""Solves the 8-queens problem (and its N-queens generalization.)""" | |
# It is clear that since no two queens can occupy the same column, and there are | |
# as many columns as queens to place, then each column must contain one and only one | |
# queen. The only question is, on which row to place each queen? | |
# A "left-board" is a way to represent a partial solution to the n-queens problem. | |
# It is simply a list of integers, each indicating the row occupied by the queen | |
# in each column. Because it is a partial solution, it can have fewer than 8 elements, | |
# or even be empty. For example, [0, 2] indicates a that the queen in the left-most |
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 collapse_intervals(intervals): | |
'''intervals is an iterable of 2-tuples representing the start and end of | |
closed intervals. returns a list of 2-tuples after collapsing any | |
overlapping intervals. For example: | |
collapse_intervals([(1,5), (9,10), (4,7)]) -> [(1,7), (9,10)] | |
The algorithm doesn't care if intervals are out of order, or if two points | |
of the same interval are out of order. It also doesn't require the points |
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
cat ~/.ssh/id_rsa.pub | ssh $USER@$SERVER "umask 077; test -d ~/.ssh || mkdir ~/.ssh ; cat >> ~/.ssh/authorized_keys" |
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
# http://en.wikipedia.org/wiki/RSA_(algorithm) | |
p = 61 | |
q = 53 | |
n = p*q | |
totient = (p-1)*(q-1) | |
# here, e is 2^4 + 1; in real world applications, e is often 2^16 + 1 | |
e = 17 |
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
""" | |
Example using Python's itertools and generators to implement the classic FizzBuzz problem using pythonic iterators. | |
Output looks like this: | |
$ python fizz_buzz_with_iterator.py | |
1 | |
2 | |
Fizz |
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
#! /usr/bin/env python | |
""" | |
Simple wrapper around Github's gist HTTP API. | |
""" | |
import argparse | |
import logging | |
import requests | |
import json | |
from getpass import getuser, getpass |
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
logs/ |
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
arima.simulation <- function(linear=1, exponential=0, seasonal=0, season=25, random=1, ahead=10) { | |
sim.data <- (1:100 * linear/100) + exponential*exp( (1:100/100))/exp(1) + seasonal*sin(1:100 * 2 * pi / season ) + random*rnorm(100); | |
sims <- ts(sim.data, frequency=25) | |
sim.model <- auto.arima(sims); | |
sim.past.future <- c(as.vector(fitted(sim.model)),as.vector(forecast(sim.model, h=ahead)$mean)); | |
ylimits = c( min(sim.past.future), max(sim.past.future)) | |
plot(sim.data, pch=20, col=rgb(0,0,1, alpha=0.5), ylim=ylimits, xlim=c(0,length(sim.past.future))); | |
lines(seq_along(sim.past.future), sim.past.future, col=rgb(0,1,0, alpha=0.75)); | |
return(sim.model); |