Skip to content

Instantly share code, notes, and snippets.

import Foundation
func emptyGenerator<T>() -> AnyGenerator<T> {
return anyGenerator{
return nil
}
}
func generateOnce<T>(g: () -> T) -> AnyGenerator<T> {
var first = true
import Foundation
public protocol KDPoint {
func distance(other: Self) -> Double
func dimension() -> Int
func lessThan(other: Self, dim: Int) -> Bool
}
public enum BinaryTreeVisitOrder {
case InOrder
from graph import *
def kosaraju(g):
def dfs(g, start, s, visited=set()):
for n in g.vertex_out[start]:
if n in visited: continue
visited.add(n)
dfs(g, n, s, visited)
s.append(start)
from random import sample
@fferri
fferri / graph.py
Last active August 29, 2015 14:26
class DirectedGraph:
def __init__(self, edges={}):
# copy/cast constructor
if isinstance(edges, DirectedGraph): edges = edges.label
self.label = {}
self.vertex_in = {}
self.vertex_out = {}
for edge, label in edges.items():
self[edge] = label