Skip to content

Instantly share code, notes, and snippets.

import sys
import itertools
from sys import argv
from itertools import combinations
from sets import Set
def firsthashfun(tuplex):
total1 = 0
for i in tuplex:
@chelsea1992
chelsea1992 / gist:5b4ab5ffadbb379b2481fcecd3df6596
Last active April 15, 2017 07:06
Collaborative Filtering Algorithm
import sys
import math
users = {}
def user_ave(user_a):
num = 0
user_sum = 0
for k, v in user_a.iteritems():
import itertools
import sys
#from itertools import combinations
import random
#def generate sample
if __name__ == '__main__':
filename = sys.argv[1]
supportNum = int(sys.argv[2])
@chelsea1992
chelsea1992 / gist:6c725a24d358763097bebe8223c2014a
Created April 15, 2017 07:11
Community Detection, Girvan-Newman algorithm
import networkx as nx
import sys
from collections import deque
import community as cm
from collections import defaultdict
import matplotlib.pyplot as plt
# calculate the betweeness
def cal_betweeness(graph):
@chelsea1992
chelsea1992 / K-Means Clustering Algorithm
Last active April 16, 2017 01:52
K-Means Clustering Algorithm
import sys
import math
def distance(list1,list2):
dis = 0
for i in range(0,point_dimension):
dis += (list1[i]-list2[i])**2
sqrt_dis = math.sqrt(dis)
return sqrt_dis
@chelsea1992
chelsea1992 / process the lax.json File
Last active April 16, 2017 01:52
process the lax.json File
import json
import sys
import math
def convert_item(item):
year = str(item['year']).split('-')[0]
terminal = item['terminal']
if terminal == 'Terminal 1':
terminal = 't1'
@chelsea1992
chelsea1992 / LeetCode
Created April 16, 2017 01:51
LeetCode,525. Contiguous Array
public class Solution {
public int findMaxLength(int[] nums) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) nums[i] = -1;
}
Map<Integer, Integer> map = new HashMap<>();
map.put(0, -1);
int sum = 0, max = 0;
for(int i = 0; i<nums.length; i++){
sum += nums[i];
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/