Skip to content

Instantly share code, notes, and snippets.

View amcameron's full-sized avatar

Andrew Cameron amcameron

View GitHub Profile
@amcameron
amcameron / pogo_cps.py
Last active September 17, 2019 20:32
Pokemon GO CP values
"""The code contained here is meant to find possible combinations of CP values,
given a list of species and their base stats. This is useful when participating
in, e.g., a "CP counting game" wherein people post screenshots of their Pokemon
always posting a Pokmeon exactly 1 CP higher than the previous poster. If
certain CP values are impossible to obtain, or very rare, then this is of
interest to those people."""
from collections import defaultdict
from itertools import product
from math import sqrt
from cffi import FFI
ffi = FFI()
ffi.cdef('int classify(float x, float y, int num_class, float* class_x, float* class_y);')
libcls = ffi.dlopen('libclassify.A')
xmeans = ffi.new("float[2]", (5.0, 2.0))
ymeans = ffi.new("float[2]", (5.0, 2.0))
print '(3.0, 3.0): %d' % libcls.classify(3.0, 3.0, 2, xmeans, ymeans) # (3.0, 3.0): 1
@amcameron
amcameron / example.m
Created August 2, 2013 16:02
Expanding (input and output) argument lists in MATLAB. Note that, for output arguments, you must preallocate a cell array of the correct size. Make the cell array smaller than the number of output arguments, and the output arguments will be truncated to fit into the cell array. Make it too large, and you'll get an error.
% A demonstration - expanding cell matrices as a list of input arguments,
% and returning a list of output arguments to a single cell matrix.
% Set up some vectors representing axis grid spacings.
d1 = 2:3;
d2 = 2:5;
d3 = 1:3;
% Make a cell array to hold them.
din = {d1, d2, d3};
@amcameron
amcameron / notes-processor
Created November 11, 2012 18:24
A simple script to try processing a photo of a page of notes on a dark background, to extract the paper and convert to B&W.
% Open image; convert to grayscale.
img = imread('PB110010.JPG');
im = double(rgb2gray(img));
% Normalize image to [0, 1].
im = im/max(im(:));
% Threshold the image using Otsu's method.
threshold = graythresh(im);
im_bw = im2bw(im, threshold);
@amcameron
amcameron / gist:3932997
Created October 22, 2012 18:00
Phase plot of a system with a complex conjugate pair, zeta = 0
s = tf('s');
h = 1/((s/5)^2 + 1);
[mag, phase, w] = bode(h);
phase = phase(:);
semilogx(w, phase)
ylim([-190 10])
xlabel('\omega')
ylabel('Phase (degrees)')
title('Phase plot for complex conjuage pair (\zeta=0, \omega_n = 5)')
@amcameron
amcameron / twins.py
Created January 3, 2012 00:52
Monte Carlo approximation of the Twins problem
# A woman has two children (perhaps they are twins).
# There is a 50% chance that each child will be male, and a 50% chance it will be female.
# The genders of the two children are independent.
# Given that at least one of the children is female, what is the likelihood that both are female?
from random import choice
genders = ["male", "female"]
num_runs = 10000
num_females = [0 for i in xrange(num_runs)]
@amcameron
amcameron / photography-quotes.markdown
Created December 1, 2011 18:48
Quotes about photography

It is impossible for a photographic print to duplicate the range of brightnesses (luminances) of most subjects, and thus photographs are to some degree interpretations of the original subject values. Much of the creativity of photography lies in the infinite range of choices open to the photographer between attempting a nearly literal representation of the subject and freely interpreting it in highly subjective "departures from reality." My work, for example, is frequently regarded as "realistic," while in fact the value relationships within most of my photographs are far from a literal transcription of actuality. I employ numerous photographic controls to create >an image that represents "the equivalent of what I saw and felt" (to paraphrase

@amcameron
amcameron / template.html
Created July 4, 2011 19:50
A Flask/Jinja2 template for a webapp animated using Raphaël.js
<!DOCTYPE HTML>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Subreddit Graph</title>
<script src="{{ url_for('static', filename='raphael-min.js') }}"
type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
window.onload = function() {
@amcameron
amcameron / iTunes
Created June 11, 2011 20:33
Prevent iTunes from opening if another media player is already running.
#!/usr/bin/env python
"""Prevent iTunes from opening if another media player is already running.
Usage: rename iTunes binary to iTunesX; place this script in same folder
and make it executable.
Thanks to "andrew pz" and "dmonner" from this discussion thread:
http://discussions.apple.com/thread.jspa?threadID=2122639&start=30&tstart=0"""
import sys, os, subprocess
@amcameron
amcameron / remdup.py
Created March 12, 2011 05:36
remove duplicates in a list, using recursion instead of loops.
def remdup(l, dup=None):
# If has zero or one elements, there are no duplicates.
if len(l) < 2:
return l
# If there's a duplicate to remove, remove it and recurse until ValueError
# is raised, which means there are none left to remove. Since lists are
# mutable, we don't have to capture this.
if dup is not None:
try: