Skip to content

Instantly share code, notes, and snippets.

View st0le's full-sized avatar

Gaurav Kamath st0le

View GitHub Profile
@st0le
st0le / pqueue
Created May 5, 2013 20:32
min/max pqueue implementation with tie breaker
import heapq as hp
class pqueue:
def __init__(self, minheap = True):
self.heap = []
self.mul = 1 if minheap else -1
self.count = 0
def size(self):
@st0le
st0le / Rotation Point
Created May 11, 2013 05:02
Find Rotation Point
def find_rotation_binary_search(L):
left,right = 0,len(L) - 1
while L[left] > L[right]:
mid = (left + right) / 2
if L[mid] > L[right]:
left = mid + 1
else:
right = mid
return left
@st0le
st0le / Naive Rotation Point
Created May 11, 2013 05:03
Naive Rotation Point
import random
L = 10
lst = [random.randint(0,100) for _ in xrange(L)]
lst.sort()
K = random.randint(0,L)
lst = lst[K:] + lst[:K] # rotate by random K
def find_rotation_naive(L):
for i in xrange(len(L) - 1):
if L[i] > L[i+1]:
@st0le
st0le / Dijkstra's shortest path algorithm.
Last active December 17, 2015 07:39
Dijkstra's shortest path algorithm.
import pqueue
def dijkstra(graph,src,dst):
min_dist = dict() # stores shortest path from src, ie min_dist[u] = w denotes, weight of shortest path (src->u) = w
prev_vertex = dict() # prev_vertex[u], stores the previous node on the shortest path from src to u
pq = pqueue.pqueue()
pq.push( (0, src) )
min_dist[src] = 0 # distance to source is 0.
prev_vertex[src] = None # no previous node for src
while not pq.empty():
@st0le
st0le / max_diff_naive
Created May 18, 2013 04:40
max_diff_naive
def max_diff_naive(L):
max_diff = start = end = 0
for i in xrange(len(L)):
for j in xrange(i+1,len(L)):
if max_diff < L[j] - L[i]:
start,end = i,j
max_diff = L[j] - L[i]
return max_diff,start,end
@st0le
st0le / max_diff_linear
Created May 18, 2013 04:41
max_diff_linear
def max_diff_linear(L):
min_index = 0
max_diff = start = end = 0
for i in xrange(len(L)):
if L[i] < L[min_index]: min_index = i
if max_diff < L[i] - L[min_index]:
max_diff = L[i] - L[min_index]
start,end = min_index,i
return max_diff,start,end
@st0le
st0le / Word Ladder
Created May 20, 2013 00:19
Word Ladder
import sys,graphs,itertools
def prepare_graph():
diff_by_1 = lambda (x,y): len(x) == len(y) and sum(1 for a,b in zip(x,y) if a != b) == 1
graph = {}
for x,y in filter(diff_by_1,itertools.product(words,repeat=2)):
if x not in graph: graph[x] = []
if y not in graph: graph[y] = []
graph[x].append( ( y , 1) )
graph[y].append( ( x , 1) )
@st0le
st0le / Subset Sum
Created May 23, 2013 02:23
Subset Sum
import random
rand_list = sorted([random.randint(-100,100) for _ in xrange(7)])
K = random.randint(0,200)
def subset_sum(lst,K):
lst.sort()
sz = len(lst)
def subset_sum_helper(index,s):
@st0le
st0le / 2 Missing Duplicate
Last active December 17, 2015 19:29
2 Missing Duplicate
import random
r = {random.randint(10,100) for i in xrange(10)}
lst = random.sample(r,len(r))
l = lst[:2] + lst[2:] * 2
random.shuffle(l)
def missing_2_numbers(lst):
xor = reduce(lambda a,b : a ^ b, lst)
mask = (xor & -xor) #get the right most set bit.
@st0le
st0le / Subset Sum DFS
Created June 4, 2013 21:43
Subset Sum DFS
def subset_sum(lst,K):
lst.sort()
sz = len(lst)
def subset_sum_helper(index,s):
if index == sz or s >= K : return s == K
return subset_sum_helper(index + 1, s) or subset_sum_helper(index + 1, s + lst[index])
return subset_sum_helper(0 , 0 )