Skip to content

Instantly share code, notes, and snippets.

View cabiad's full-sized avatar

Chris Abiad cabiad

View GitHub Profile
@cabiad
cabiad / graph_bfs.py
Created March 23, 2013 14:25
Python2. Simple BFS graph traversal. Given a generic graph as a Guido-style adjacency list (dict-of-lists), traverse the nodes, determining if there is a path from start to dest.
##############################################################
# #
# Copyright 2013 Christopher Abiad. All rights reserved. #
# #
##############################################################
__author__ = 'Chris Abiad <cabiad@oanda.com>'
"""
Directed graph
@cabiad
cabiad / ll.py
Created March 23, 2013 13:28
A linked list implementation in Python2 with support for Python-style iteration and a surprisingly elegant (though simple) implementation of Floyd's cycle finding algorithm.
#########################################################
# #
# Copyright 2013 Christopher Abiad All rights reserved. #
# #
#########################################################
__author__ = 'Chris Abiad <chris@chrisabiad.com>'
import collections
@cabiad
cabiad / coro.py
Created March 23, 2013 12:54
A tiny coroutine example in python
#########################################################
# #
# Copyright 2013 Christopher Abiad All rights reserved. #
# #
#########################################################
__author__ = 'Chris Abiad <chris@chrisabiad.com>'
def f1():
while True:
@cabiad
cabiad / find_missing_value.py
Created February 28, 2013 19:49
Solution to following problem: Array A contains all integers from 0 to n, except one is missing. In this problem we cannot access an entire integer in A in a single op. The elements of A are represented in binary, and the only op we can use is “fetch the jth bit of A[i],” which takes constant time. Write code to find the missing integer. Can you…
####################################
# Copyright Christopher Abiad, 2013
# All Rights Reserved
####################################
__author__ = 'Christopher Abiad'
def find_missing_one(bit_index, A):
# there is only one item
if bit_index == 0:
@cabiad
cabiad / toh.py
Last active December 14, 2015 00:49
Towers of Hanoi: Python2 implementation both recursively and iteratively using a Python list as our stack. Note that the well-known iterative solution with O(1) space requirements is not included here.
####################################
# Copyright Christopher Abiad, 2013
# All Rights Reserved
####################################
__author__ = 'Christopher Abiad'
def toh(num, src, dest, other):
if num <= 0:
@cabiad
cabiad / qsort.py
Created February 18, 2013 02:02
A simple and fun experiment in generating a Python2 quicksort method that performs the sorting in-place. Plenty of comments and a few tests at the bottom might actually make this code helpful for someone studying Python and the quicksort algorithm. See the following excellent resources for more info on quick-sort: http://www.sorting-algorithms.c…
####################################
# Copyright Christopher Abiad, 2013
# All Rights Reserved
####################################
"""In-place quicksorting."""
__author__ = 'Christopher Abiad'
from random import randrange
@cabiad
cabiad / heap.py
Last active December 13, 2015 20:19
A simple Python2 binary min-heap with a simple heapsort implementation. Also, some fuzz testing and a bunch of unit tests for the heap and its helper methods.
####################################
# Copyright Christopher Abiad, 2013
# All Rights Reserved
####################################
"""Binary min-heap with a simple heapsort implementation.
Heap is stored entirely in an array (Python list) using the property that
for any node at position i, its two children can be stored at 2i and
2i + 1 without any collisions with other nodes.
"""
@cabiad
cabiad / mergesort.py
Created February 10, 2013 00:41
Quick code kata, a merge sort in Python. Completed first on whiteboard, manually tested there, then re-tested by typing and running it through the interpreter.
####################################
# Copyright Christopher Abiad, 2012
# All Rights Reserved
####################################
__author__ = 'Christopher Abiad'
def mergesort(l):
n = len(l)
if n <= 1: