Skip to content

Instantly share code, notes, and snippets.

View maxlikely's full-sized avatar

Max Likely maxlikely

View GitHub Profile
@maxlikely
maxlikely / los.py
Created February 22, 2012 20:24
Longest Oscillating Sub-sequence Naive
def los(A,k):
"""Auxiliary function for additional parameter k"""
if len(A) < 2: return 0
plusOrMinus = lambda x, y: abs(x, y) < 2
# don't take element 0
m = las(A[1:], k)
# take element 0
@maxlikely
maxlikely / ps.py
Created February 22, 2012 20:26
Prefix Set
def ps(A):
setA = set(A)
for i,a in enumerate(A):
if a in setA: setA.remove(a)
if not setA: return i
@maxlikely
maxlikely / equi.py
Created February 22, 2012 20:27
Equilibrium Index
def equi ( A ):
if len(A) < 1: return -1
if len(A) == 1: return 0
lsum = [0]
rsum = [0]
for i,a in enumerate(A):
lsum.append(lsum[i] + a)
lsum = lsum[:len(lsum)-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]
@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 / 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 / most_popular_baseline.py
Created October 23, 2012 07:36
Most Popular Baseline
@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 / 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 / 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)