This file contains hidden or 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
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; | |
} |
This file contains hidden or 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
// 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; | |
}; |
This file contains hidden or 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
class Node | |
{ | |
public: | |
Node() : next(NULL), value(0) {} | |
Node(int v) : next(NULL), value(v){} | |
Node* next; | |
int value; | |
}; | |
bool detect_loop(Node* head) |
This file contains hidden or 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
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; |
This file contains hidden or 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
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; |
This file contains hidden or 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
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) |
This file contains hidden or 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 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() |
This file contains hidden or 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
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'] |
This file contains hidden or 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
# 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', |
This file contains hidden or 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/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! |
OlderNewer