Skip to content

Instantly share code, notes, and snippets.

View johnpena's full-sized avatar

John Peña johnpena

View GitHub Profile
@johnpena
johnpena / archive_instapaper.py
Created November 6, 2010 23:30
Archives your stuff from Instapaper. This saves the archive as a mobi file, but you can change that by replacing the last URL.
import urllib, urllib2
import cookielib
def getInstapaperArchive(username, password):
login_url = "http://www.instapaper.com/user/login"
home_url = "http://www.instapaper.com/u"
kindle_url = "http://www.instapaper.com/mobi"
params = {"username":username, "password":password}
@johnpena
johnpena / mergesort.py
Created February 8, 2011 19:55
Mergesort in python
def merge(left, right):
""" Merges two sorted lists into one sorted list """
result = []
while len(left) > 0 or len(right) > 0:
if len(left) > 0 and len(right) > 0:
if left[0] <= right[0]:
result.append(left.pop(0))
else:
result.append(right.pop(0))
elif len(left) > 0:
@johnpena
johnpena / substrings.py
Created February 8, 2011 19:58
Get all of a string's substrings
def get_all_substrings(string):
substrings = []
for i in xrange(len(string)):
n = i
while n >= 0:
substrings.append(string[n:i+1])
n -= 1
return substrings
@johnpena
johnpena / normalize.py
Created February 8, 2011 20:00
Safely/sanely ransforms a string from unicode to ascii.
import unicodedata
def normalize(s):
""" Safely/sanely ransforms a string from unicode to ascii. """
return unicodedata.normalize('NFKD', unicode(s)).encode('ascii','ignore')
def sanitize(s, replace_with=None):
""" Replace control chars, unicode chars, and whitespace with '?'. """
if not replace_with: replace_with = '?'
return ''.join(c if (32 < ord(c) < 127) else replace_with for c in s)
@johnpena
johnpena / levenshtein.py
Created February 8, 2011 20:01
Levenshtein distance gives you a basic idea of how similar two strings are.
def levenshtein_distance(first, second):
"""
Find the Levenshtein distance between two strings.
Levenshtein distance gives you a basic idea of how similar two strings are.
"""
if len(first) > len(second):
first, second = second, first
if len(second) == 0:
return len(first)
@johnpena
johnpena / growlexample.py
Created February 8, 2011 20:04
Sends a notification to Grow using python
import Growl
def growlnotify(title, message, sticky=False):
"""
Sends a notification to Growl with title and message.
`sticky` specifies whether or not the user has to click
the message to make it go away.
TODO:
- allow specification of an icon
"""
@johnpena
johnpena / dictionary_sorting.py
Created February 8, 2011 20:05
Strategies for sorting dictionaries in python
import operator
def sort_dict(d):
"""
Sorts a dict by it's values, assuming the values are ints.
"""
return sorted(d.iteritems(), key=lambda (k,v): (v,k), reverse=True)
def sort_dict_list(l, key):
"""
@johnpena
johnpena / rotation.c
Created February 14, 2011 01:14
Find if a string is a rotation of another.
#include <stdio.h>
#include <string.h>
int isrotation(char*, char*);
main() {
int result = isrotation("byegood", "goodbye");
if (result == 0) {
printf("Nope.\n");
} else {
@johnpena
johnpena / stack.c
Created February 15, 2011 03:30
static vs automatic variables in c
#include <stdio.h>
void foo()
{
static int i = 1;
int j = 1;
i++; //
printf("i: %d, ", i);
j++; //
@johnpena
johnpena / reverse.c
Created February 15, 2011 04:33
Reverse a string in C without using temporary storage
#include <string.h>
void reverse(char* str) {
int i;
int len = strlen(str);
for (i = 0; i < len/2 ; ++i){
str[len] = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = str[len];