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); |
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
op = { | |
"!!": function(x) { return !!x; }, | |
"!": function(x) { return !x; }, | |
"&&": function(x, y) { return x && y; }, | |
"||": function(x, y) { return x || y; }, | |
"~": function(x) { return ~x; }, | |
"negative": function(x) { return -x; }, | |
"+": function(x, y) { return x + y; }, |
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
// observable is a DOM node, document, window, etc. | |
// eventName is the string event name, say "load" or "click" | |
// handler is the callback function. | |
function addEventListener(observable, eventName, handler) { | |
if ( observable.addEventListener ) { | |
observable.addEventListener(eventName, handler, false); | |
} else if ( observable.attachEvent ) { | |
observable.attachEvent("on" + eventName, handler); | |
} else { | |
// error handling? |
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
#include <iostream> | |
#define SIZE(type) std::cout << #type ": " << sizeof(type) << " bytes\n"; | |
int main(int argc, char** argv ) { | |
SIZE(char); | |
SIZE(short); | |
SIZE(int); | |
SIZE(long); | |
SIZE(float); |
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
# padding/unpadding per PKCS7 or PKCS5. | |
class PKCS7(object): | |
def __init__(self, block_size): | |
self.block_size = block_size | |
def pad(self, string): | |
padding_number = self.block_size - len(string) % self.block_size | |
if padding_number == self.block_size: | |
return string |
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 math | |
class Magnitude(object): | |
'''A descriptor that can be added to any N-dimensional point to provide a read/writable magnitude property.''' | |
def __init__(self, *attributes): | |
self.attributes = attributes | |
def __get__(self, instance, owner): | |
sum_of_squares = 0.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
# snippet from http://www.madboa.com/geek/openssl/#encrypt-simple | |
# encrypt file.txt to file.enc using 256-bit AES in CBC mode | |
openssl enc -aes-256-cbc -salt -in file.txt -out file.enc | |
To decrypt file.enc you or the file’s recipient will need to remember the cipher and the passphrase. | |
# decrypt binary file.enc | |
openssl enc -d -aes-256-cbc -in file.enc |
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 pipe(inputCursor, outputCursor, outputTablename): | |
''' | |
Read rows from the inputCursor and immediately INSERT them into the | |
outputTablename using the supplied outputCursor. There are faster | |
mechanisms like copy_from() for particular databases but this is extremely | |
portable: not only do the inputCursor and outputCursor not have to be for | |
the same database, they can be for different database engines without | |
problem. | |
''' |
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('%s@gmail.com 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 |
OlderNewer