Skip to content

Instantly share code, notes, and snippets.

View maxlikely's full-sized avatar

Max Likely maxlikely

View GitHub Profile
@maxlikely
maxlikely / dict_eqaulity.py
Created October 30, 2012 19:25
Compare to dictionaries for equality
def dicts_equal(first_dict, second_dict):
'''
check if two dicts are equal
'''
# same length, same keys, same values
return len(first) == len(second) and \
all([k in first for k in second]) and \
all([v == second[k] for k,v in first.items()])
@maxlikely
maxlikely / is_permutation.py
Created October 30, 2012 19:13
Determines if two strings are permutations
def is_permutation(first_string, second_string):
'''
checks if two strings are permutations of eachother
'''
from collections import Counter
return Counter(first_string) == Counter(second_string)
@maxlikely
maxlikely / mean_avg_prec.py
Created October 23, 2012 23:38
Mean average precision
def node_mean_avg_prec(G, node, predictions):
'''
Calculates mean average precision for a user.
'''
gold = set(G[node])
indicator = [1 if p in gold else 0 for p in predictions]
prec = np.cumsum(indicator)
prec = [indicator[i] * p / float(i+1) for i, p in enumerate(prec)]
@maxlikely
maxlikely / synthesize_dev_set.py
Created October 23, 2012 23:20
Synthesize dev set
import networkx as nx
import numpy as np
def synthesize_dev_set(G, fraction=.2, test_fraction=.1):
'''
Synthesizes a dev set by randomly deleting fraction of edges in the
graph and labeling test_fraction of nodes for prediction.
train_graph, test_nodes = synthesize_dev_set(G)
@maxlikely
maxlikely / gist:3942368
Created October 23, 2012 23:08
BFS & Scraping
from rdioapi import Rdio
from collections import deque
def get_neighbors(rdio, userkey, N=1000, direction='outgoing'):
'''
direction='incoming' downloads all a users followers
direction='outgoing' downloads all a users a user is following
'''
method = 'userFollowers' if direction == 'incoming' else 'userFollowing'
@maxlikely
maxlikely / random_baseline.py
Created October 23, 2012 07:37
Random Baseline
num_vertices = len(G.nodes())
predictions = [random_integers(num_vertices, size=k)] * len(test_nodes)
@maxlikely
maxlikely / most_popular_baseline.py
Created October 23, 2012 07:36
Most Popular Baseline
@maxlikely
maxlikely / ferry_loading.py
Created February 24, 2012 02:19
Ferry Loading Naive
def ferry(cars,k):
"""Naive solution."""
if not cars: return 0 # no cars left
car = cars[0]
putleft = lambda: 1 + ferry(cars[1:], (k[0]-car,k[1]))
putright = lambda: 1 + ferry(cars[1:], (k[0],k[1]-car))
@maxlikely
maxlikely / equalization_steps.py
Created February 22, 2012 20:30
Equalization Steps
def equalization_steps ( A ):
if sum(A) % 2 == 0:
minA, maxA = min(A), max(A)
return abs(maxA - minA) / 2
else:
return -1
@maxlikely
maxlikely / los_fast.py
Created February 22, 2012 20:29
Longest Oscillating Sub-sequence Fast
def longest_oscillating_sub_sequence(A):
"""Memoize the longest oscillating sub-sequence problem.
Or maybe it was called atomic sub-sequence..."""
plusOrMinus = lambda x,y: abs(x - y) < 2
A.insert(0,A[0])
n = len(A)
LOS = [0 for i in A]