Skip to content

Instantly share code, notes, and snippets.

@jsundram
jsundram / ladder.cxx
Created January 27, 2011 00:37
problem 1
int step(int curr, int steps, const int n)
{
curr += steps;
if (curr < n)
return step(curr, 1, n) + step(curr, 2, n);
if (curr == n)
return 1;
return 0;
}
@jsundram
jsundram / list.cxx
Created January 27, 2011 01:32
reverse last 5 nodes of a linked list
// Here's what the linked list looks like.
// I've assumed int values, so you can see that it works
class Node
{
public:
Node() : next(NULL), value(0) {}
Node(int v) : next(NULL), value(v){}
Node* next;
int value;
};
@jsundram
jsundram / detect_loop.cxx
Created January 27, 2011 01:47
detect loop (I've seen this one before)
class Node
{
public:
Node() : next(NULL), value(0) {}
Node(int v) : next(NULL), value(v){}
Node* next;
int value;
};
bool detect_loop(Node* head)
float root(float n, float epsilon=.000001f)
{
if (n <= 0.0f)
return 0.0f;
float guess = n/2.1f;
float error = n - guess*guess;
while (epsilon < fabs(error))
{
guess += error / 2;
@jsundram
jsundram / binary_search.cxx
Created February 10, 2011 01:38
practice
bool find(int* a, int n, int i)
{
int lo = 0;
int hi = n-1;
while (lo <= hi)
{
int mid = a[(hi + lo) / 2]; // TODO: Overflow
printf("lo: %d, mid: %d, hi: %d\n", lo, mid, hi);
if (mid == i)
return true;
@jsundram
jsundram / haversine.py
Created April 14, 2011 17:38
simple haversine implementation from description on wikipedia
from math import pi, sin, cos, atan2, sqrt
def haversine_distance(loc1, loc2):
"""Haversine formula - give coordinates as (lat_decimal, lon_decimal) tuples. Returns distance in miles"""
earth_radius = 6371.0 # km
to_radians = lambda x : x * pi / 180.0
lat1, lon1 = map(to_radians, loc1)
lat2, lon2 = map(to_radians, loc2)
@jsundram
jsundram / to_wordle.py
Created May 3, 2011 17:08
Convert a hadoop output file to a format suitable for use by Wordle.net
import sys
def to_wordle(s):
f = open(s)
g = open('wordle.txt', 'w')
for line in f:
token, count = line.strip().split('\t')
g.write('%s: %s\n' % (token, count))
f.close()
g.close()
@jsundram
jsundram / colors.py
Created June 7, 2011 14:39
Expose D3 Colors for matplotlib
from matplotlib.colors import hex2color
# color schemes Courtesy of the excellent mbostock's D3.js project
d3_10 = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf']
d3_20 = ['#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c', '#98df8a', '#d62728', '#ff9896', '#9467bd', '#c5b0d5', '#8c564b', '#c49c94', '#e377c2', '#f7b6d2', '#7f7f7f', '#c7c7c7', '#bcbd22', '#dbdb8d', '#17becf', '#9edae5']
d3_20b = ['#393b79', '#5254a3', '#6b6ecf', '#9c9ede', '#637939', '#8ca252', '#b5cf6b', '#cedb9c', '#8c6d31', '#bd9e39', '#e7ba52', '#e7cb94', '#843c39', '#ad494a', '#d6616b', '#e7969c', '#7b4173', '#a55194', '#ce6dbd', '#de9ed6']
d3_20c = ['#3182bd', '#6baed6', '#9ecae1', '#c6dbef', '#e6550d', '#fd8d3c', '#fdae6b', '#fdd0a2', '#31a354', '#74c476', '#a1d99b', '#c7e9c0', '#756bb1', '#9e9ac8', '#bcbddc', '#dadaeb', '#636363', '#969696', '#bdbdbd', '#d9d9d9']
@jsundram
jsundram / blues.py
Created June 24, 2011 21:21
Colors of blue, according to wikipedia (58)
# from http://en.wikipedia.org/wiki/Category:Shades_of_blue
blues = {'Air Force': '#5D8AA8',
'Alice': '#F0F8FF',
'Ao': '#0000BB',
'Azure': '#007FFF',
'Baby': '#89CFF0',
'Bleu de France': '#318CE7',
'Blue': '#0000FF',
'Bondi': '#0095B6',
'Brandeis': '#0070FF',
@jsundram
jsundram / cull.py
Last active April 5, 2023 15:22
Check if lat long is inside the bounds of the continental US (box model, not shape)
# http://en.wikipedia.org/wiki/Extreme_points_of_the_United_States#Westernmost
top = 49.3457868 # north lat
left = -124.7844079 # west long
right = -66.9513812 # east long
bottom = 24.7433195 # south lat
def cull(latlngs):
""" Accepts a list of lat/lng tuples.
returns the list of tuples that are within the bounding box for the US.
NB. THESE ARE NOT NECESSARILY WITHIN THE US BORDERS!