Skip to content

Instantly share code, notes, and snippets.

@hillscottc
hillscottc / 0_reuse_code.js
Created May 23, 2014 16:02
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@hillscottc
hillscottc / graph_traversals.py
Last active August 29, 2015 14:01
Graph traversal.
""" Graph traversal functions. Breadth-first search (BFS) and Depth-first search (DFS).
Adapted from: http://code.activestate.com/recipes/576723-dfs-and-bfs-graph-traversal
"""
import pprint
GRAPH_PIC = """
+---- A
| / |
| B--D--C
@hillscottc
hillscottc / fibo_memo_basic.py
Created May 30, 2014 15:52
Fibonacci, basic memoization.
fib_vals = {1:1, 2:1}
def fib(n):
if n <= 2:
return 1
if n in fib_vals:
return fib_vals[n]
else:
fib_vals[n] = fib(n - 1) + fib(n - 2)
return fib_vals[n]
@hillscottc
hillscottc / memo_decorator.py
Created May 30, 2014 15:56
Decorator for memoization via 'cache' dictionary.
def memoize(func):
"""Decorator for memoization via 'cache' dictionary."""
cache = {}
def memoed_func(*args):
if args not in cache:
cache[args] = func(*args)
return cache[args]
memoed_func.cache = cache
@hillscottc
hillscottc / bin_tree_ops.py
Created May 30, 2014 16:27
Binary tree traversal operations.
tree_walk(x):
if x:
tree_walk(x:left)
print x:key
tree_walk(x:right)
tree_search(x, k):
"""Recursive search."""
if not x or k == x.key:
@hillscottc
hillscottc / bal_brackets.py
Created May 31, 2014 15:08
Stack to balance brackets.
from pythonds.basic.stack import Stack
def parChecker(symbolString):
s = Stack()
balanced = True
index = 0
while index < len(symbolString) and balanced:
symbol = symbolString[index]
if symbol in "([{":
s.push(symbol)
@hillscottc
hillscottc / palchecker.py
Created May 31, 2014 15:48
A Palindrome Checker Using Deque (palchecker)
from pythonds.basic.deque import Deque
def palchecker(aString):
chardeque = Deque()
for ch in aString:
chardeque.addRear(ch)
stillEqual = True
@hillscottc
hillscottc / recsum.py
Created May 31, 2014 16:07
The basic recursive function. Sum of a list of numbers.
"""Recursive sum of a list of numbers. The basic recursive case.
Define the base case.
If base case, return it.
Else: listSum(numList) = first(numList) + listSum(rest(numList))
"""
def listsum(numList):
if len(numList) == 1:
return numList[0]
@hillscottc
hillscottc / bin_search.py
Created May 31, 2014 23:38
Binary search of a string.
def binarySearch(alist, item):
first = 0
last = len(alist)-1
found = False
while first<=last and not found:
midpoint = (first + last)//2
if alist[midpoint] == item:
found = True
else:
@hillscottc
hillscottc / dijkstra.py
Last active August 29, 2015 14:02
Dijkstras Algorithm for shortest path.
"""
Dijkstra's algorithm for shortest paths.
http://code.activestate.com/recipes/577343-dijkstras-algorithm-for-shortest-paths/)
- dijkstra(G, s) finds all shortest paths from s to each other vertex.
- shortest_path(G, s, t) uses dijkstra to find the shortest path from s to t.
The input graph G is assumed to have the following representation:
- A vertex can be any object that can be used as an index into a dictionary.