Skip to content

Instantly share code, notes, and snippets.

@landau
landau / is_circular.py
Last active December 23, 2015 15:29
Circular Array
def is_circular(arr, start):
if not isinstance(arr, list): raise TypeError('Expected arr to be list')
if not isinstance(start, int): raise TypeError('Expected start to be int')
size = len(arr)
n = size
pos = start
# key running count to track that each index is hit
# use the index as the count, total_exp will be the
@landau
landau / min-cut.py
Created September 7, 2013 21:03
Min Cut Graph
from random import choice
from copy import deepcopy
class MinCutGraph(object):
def __init__(self, create_graph_fn):
if not hasattr(create_graph_fn, '__call__'):
raise TypError('Expected `create_graph_fn` to be a func')
self.graph = create_graph_fn()
@landau
landau / well-formed.py
Created September 3, 2013 13:25
Well formed
from sys import argv
def is_left(a):
if a == '[' or a == '(' or a == '{': return True
return False
def is_right(a):
if a == ']' or a == '}' or a == ')': return True
return False
@landau
landau / hashmap.py
Created August 17, 2013 23:39
Hashmap in python
class Hashmap(object):
"""
character holding hash map
"""
def __init__(self, hash_fn, length=100):
assert hasattr(hash_fn, '__call__'), 'You must provide a hash function'
self._buckets = [None] * length
self.hash_len = length
self.hash_fn = hash_fn
@landau
landau / merge-sort.py
Last active December 19, 2015 15:19
In place Merge Sort
from random import shuffle
from time import time
def compare(a, b):
if a > b: return 1
if a < b: return -1
return 0
@landau
landau / linkedlist.py
Created July 10, 2013 16:01
Linked List
from heapq import heappop, heappush, heapify
class Node:
def __init__(self, val):
self.val = val
self.next = None
def __str__(self):
return "<Node: %d>" % self.val
@landau
landau / maxpq.py
Last active December 19, 2015 11:49
Max Order Priority Queue
class MaxPQ(object):
def __init__(self):
# TODO initialize with array
self._N = -1
self._arr = []
pass
def _compare(self, a, b):
if a > b: return 1
if a < b: return -1
@landau
landau / tree.py
Created July 1, 2013 13:30
Successor Binary Tree
class Node(object):
def __init__(self, val):
self.val = val
self.left = None
self.right = None
self.parent = None
def has_no_kids(self):
return self.right is None and self.left is None
@landau
landau / tree.py
Created June 28, 2013 14:33
Lowest Common Ancestor in Python
class Node(object):
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def __lt__(self, val):
return self.val < val
def __gt__(self, val):