Skip to content

Instantly share code, notes, and snippets.

@mkmojo
mkmojo / tree_to_dlist.py
Created May 31, 2016 21:30
Convert binary tree to doubly linked list
class Solution:
def __init__(self):
self.dummy = DoublyListNode(0)
self.pre = self.dummy
"""
@param root, the root of tree
@return: a doubly list node
"""
@mkmojo
mkmojo / K_th_largest.py
Created May 31, 2016 21:25
Find the kth largest number in an Array
class Solution:
# @param k & A a integer and an array
# @return ans a integer
def kthLargestElement(self, k, A):
return self.helper(A, 0, len(A) - 1, k)
def helper(self, arr, begin, end, k):
pos = self.partition(arr, begin, end)
if pos == len(arr) - k:
return arr[pos]
@mkmojo
mkmojo / Nuts_and_Bolts.cpp
Created February 3, 2016 18:12
Nuts and Bolts
/**
* class Comparator {
* public:
* int cmp(string a, string b);
* };
* You can use compare.cmp(a, b) to compare nuts "a" and bolts "b",
* if "a" is bigger than "b", it will return 1, else if they are equal,
* it will return 0, else if "a" is smaller than "b", it will return -1.
* When "a" is not a nut or "b" is not a bolt, it will return 2, which is not valid.
*/
@mkmojo
mkmojo / SurroundedRegions.cpp
Created January 19, 2016 03:51
UnionFind Surrounded Regions
class UnionFind {
map<int, int> father;
public:
UnionFind(set<int> ids) {
for(auto && id : ids) {
father[id] = id;
}
}
int find(int x) {
@mkmojo
mkmojo / NumberOfIslands.cpp
Created January 18, 2016 20:39
Solution to LintCode Number of Islands
class UnionSet {
map<int, int> father;
public:
UnionSet(set<int> &labels) {
for(auto label : labels) {
father[label] = label;
}
}
int find(int x) {
@mkmojo
mkmojo / gist:b6d137ff45a20ccfbd74
Created July 7, 2015 16:10
Makefile to compile elki
MLIBS=elki.jar
JPATH=/usr/lib/jvm/java-1.7.0/bin#OPENJDK might not be able to compile the elki.jar, use sun JDK instead in this case
JC=${JPATH}/javac
all:
rm -rf knn
mkdir knn
${JC} -classpath /u/qqiu/hadoop-2.6.0/:${MLIBS} -d knn *.java
jar -cvf ./knn.jar -C knn/ . # for hadoop

Building Spark for PySpark use on top of YARN

Build Spark on local machine (only if using PySpark; otherwise, remote machine works) (http://spark.apache.org/docs/latest/building-with-maven.html)

export MAVEN_OPTS="-Xmx2g -XX:MaxPermSize=512M -XX:ReservedCodeCacheSize=512m"
mvn -Pyarn -Phadoop-2.4 -Dhadoop.version=2.4.0 -DskipTests clean package

Copy the assembly/target/scala-2.10/...jar to the corresponding directory on the cluster node and also into a location in HDFS.

@mkmojo
mkmojo / gist:cec0d37ab41037b59e89
Created June 9, 2015 15:06
Sample input file for KNN-PowerGraph.cpp
1 8.07863732582 6.89913788283 9.65340224098 4.65855289466
2 2.88736973841 7.27851173799 2.18154903957 2.54048576876
3 4.04134461747 7.47589619406 5.26357492455 3.28074786709
4 7.30918042607 2.54359931302 1.7726482863 4.88680393164
5 7.52561646034 6.61883806964 1.69578839474 5.72902598104
6 5.86769959789 8.59627770166 7.89272514467 4.74125463299
7 7.65744073601 4.33857287375 4.85858435305 4.06937502896
8 3.77954000327 9.51490340284 8.18864353524 8.15764903562
9 2.01243115106 6.47354043358 9.2865473691 2.01670960163
10 6.95064297481 8.05219452269 8.82407485682 3.25577533103
@mkmojo
mkmojo / knn.cpp
Last active August 29, 2015 14:22
KNN written in PowerGraph
//Please scroll down to the bottom for explanations
#include <graphlab.hpp>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <assert.h>
#include <ctime>
@mkmojo
mkmojo / HW7_.idea_.name
Created April 5, 2014 20:03
Spring2014 Machine Learning Homework#7
HW7