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 / 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 / pascal.clj
Last active August 29, 2015 13:58
Gets the nth row of Pascal
(defn pascal
"Gets the nth row of a pascal triangle."
[n]
(nth
(iterate #(vec (map + (conj % 0) (cons 0 %))) [1N])
(dec n)))
; user=> (pascal 10)
; [1 9 36 84 126 126 84 36 9 1]
@landau
landau / gcd.clj
Last active August 29, 2015 13:56
;; recursive greatest common divisor
(defn gcd [a b]
(if (zero? b)
a
(gcd b (mod a b))))
(= 2 (gcd 2 4)) ;; true