Skip to content

Instantly share code, notes, and snippets.

@vidyavnv
vidyavnv / removeDups.py
Last active January 4, 2016 09:09
Remove duplicates from mongodb using map reduce
from pymongo import MongoClient
from bson.code import Code
client = MongoClient()
db = client["dbname"]
col = db["collName"]
map = Code("function(){ if(this.fieldName){emit(this.fieldName,1);}}")
reduce = Code("function(key,values) {"
@vidyavnv
vidyavnv / angry.cpp
Last active December 25, 2015 23:59
/*Bill Gates is on one of his philanthropic journeys to a village in Utopia. He has N packets of candies and
would like to distribute one packet to each of the K children in the village (each packet may contain
different number of candies). To avoid a fight between the children, he would like to pick K out of N packets
such that the unfairness is minimized.
Suppose the K packets have (x1, x2, x3,….xk) candies in them, where xi denotes the number of candies in the
ith packet, then we define unfairness as
Sum [abs(X(i) - X(j))] 1<=i<j<=N