Skip to content

Instantly share code, notes, and snippets.

@kv-kunalvyas
kv-kunalvyas / shortestAndFarthest.py
Last active February 14, 2017 02:52
Code Sample: Python function to calculate shortest and farthest points in a two dimensional cluster
def closest_and_farthest(data):
"""
Finds closest and farthest apartments from a set of data
:param data: a pandas dataframe of the data
:returns: apartment ids and rounded off distances
"""
def calc_dist(lat_long_1, lat_long_2):
"""
Calculates distance(mi) between two latitude-longitude pairs
$ git clone A
$ git clone B
$ cd A
$ git remote add B ../B
$ git fetch B
$ git branch B-master B/master
$ git merge B-master
@kv-kunalvyas
kv-kunalvyas / graph.py
Created February 29, 2016 23:31
Graphs
#TODO: bidirectional bfs/dfs,
directedGraph = {0: [1,4], 1: [3], 2: [], 3: [0,1,4], 4: [0,3]}
undirectedGraph = {0: [1,4], 1: [0,3], 2: [], 3: [1,4], 4: [0,3]}
completeGraph = {0:[1,2,3], 1:[0,2,3], 2:[0,1,3], 3:[0,1,2]}
def bfs(graph, start, path=[]):
queue = [start]
while queue:
visited = queue.pop(0)
if visited not in path:
@kv-kunalvyas
kv-kunalvyas / tree.py
Last active February 29, 2016 20:52
Tree
#TODO: insert, delete
class Node(object):
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def depth(root, depth=0):
if root.left: