Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / create.txt
Created October 15, 2011 18:47
Create Table
CREATE TABLE sqf_2010
(
id serial,
year text,
pct text,
ser_num text,
datestop text,
timestop text,
recstat text,
inout text,
@jsundram
jsundram / sqrt.cpp
Created November 5, 2011 16:19
Newton-Raphson square root implementation
double root_NR(double n, double epsilon=.0000000001)
{
if (n < 0) return 0.0; // throw?
// http://mathworld.wolfram.com/SquareRootAlgorithms.html
// Both Bhaskara-Brouncker and Newton's iteration use
// (1 + n) / 2 instead of just guessing n/2.
double r = 0.5 + 0.5 * n;
double f = r*r - n;
while (epsilon < abs(f))
// code taken from here:
// http://www.analogpixel.org/blog/2011/09/13/msced-148-boxy-lady-take-2/#more-764
// and formatted for readability.
PImage pic1;
PGraphics pg;
boolean grow;
int box_size;
int MAXS = 40;
int MINS = 1;