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 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()]) |
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 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) |
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 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)] |
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) |
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
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
# 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
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
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 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] |
NewerOlder