This file contains 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
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 |
This file contains 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
def ps(A): | |
setA = set(A) | |
for i,a in enumerate(A): | |
if a in setA: setA.remove(a) | |
if not setA: return i | |
This file contains 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
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] |
This file contains 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
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] |
This file contains 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
def equalization_steps ( A ): | |
if sum(A) % 2 == 0: | |
minA, maxA = min(A), max(A) | |
return abs(maxA - minA) / 2 | |
else: | |
return -1 |
This file contains 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
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)) |
This file contains 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
# find the most popular nodes | |
top_k = sorted(G.in_degree().items(), key=lambda (node, degree): -degree)[:k] | |
# recommend them | |
predictions = [top_k] * len(test_nodes) |
This file contains 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
num_vertices = len(G.nodes()) | |
predictions = [random_integers(num_vertices, size=k)] * len(test_nodes) |
This file contains 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 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' |
This file contains 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 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) |
OlderNewer