Skip to content

Instantly share code, notes, and snippets.

@akellehe
akellehe / Cryptographer.md
Last active December 22, 2015 18:59
Book Club - Advanced Bash Scripting, Chapters 1-10 Challenge

Cryptographer

Part 1 - Decoding to binary format.

Write a script named char_to_bin.sh that takes a single commandline input argument and returns that argument encoded to binary. For example:

>> ./char_to_bin.sh "a"
1100001
@akellehe
akellehe / connie.md
Last active August 29, 2015 14:01
Summary of matlab methods used in connie

Matlab methods

  • size(mat,dim) - Prints the size of a particular dimension of a matrix
  • length(arr) - Output the scalar size of an array
  • ones(x,y) - Create a vector/matrix of all ones
  • zeros(x,y) - Create a vector/matrix of all zeros
  • issparse(mat) - Returns 1 if the storage class is sparse
  • diag(A), diag(v) - Returns a column vector of the diagonal elements of A or a square matrix with the elements of v as the diagonal elements (zeros elsewhere).
  • sparse(A), sparse(m,n) - Creates a sparse matrix from the matrix A or an m by n sparse matrix of all zeros.
  • [find(X>2), find(X>2)=0](http://www.
@akellehe
akellehe / diffusion.py
Created May 11, 2014 01:10
Represents a diffusion of an infection on a graph. a_true is the adjacency matrix. This version is slow. Lots of optimizations to be made.
import numpy
import math
import random
class Diffusion(object):
def __init__(self, a_true, prob_model):
N = numpy.size(a_true, 0)
assert a_true.shape == (N,N), a_true.shape
seed_node_index = math.floor(random.random() * N)
@akellehe
akellehe / Optimization.md
Last active August 29, 2015 14:02
optimization

Optimization

"Premature optimization is the root of all evil" 
                            -- DonaldKnuth

When we're coding we have to make tradeoffs between performance and readability. If you're making a decision where readability really suffers you should definitely consider whether or not your optimization is premature. Below we've attempted to identify a number of cases that are "readability-neutral" and improve performance.

Preamble

@akellehe
akellehe / recursive.py
Last active August 29, 2015 14:23
The recursive version
def get_descendants(node, A, descendants=None):
if not descendants:
descendants = []
descendants.append(node)
children = A.getrow(node).indices
for child in children:
descendants += get_descendants(child, A)
@akellehe
akellehe / queue.py
Created June 15, 2015 18:51
Get descendants from a tree using a queue.
def get_descendants(node, A):
unexplored = deque([node])
descendants = set()
while unexplored:
node = unexplored.popleft()
descendants.add(node)
children = A.getrow(node).indices
for child in children:
unexplored.append(child)
@akellehe
akellehe / buffered.py
Created June 15, 2015 18:52
Using a queue and a buffer to get the descendants from a root in a tree.
def get_descendants(node, A):
unexplored = deque([node])
while unexplored:
node = unexplored.popleft()
yield node
children = A.getrow(node).indices
for child in children:
unexplored.append(child)
@akellehe
akellehe / node.py
Created June 15, 2015 18:54
A basic definition for a node in a tree.
class Node(object):
def __init__(self, name):
self.name = name
self.children = []
def add_child(self, child):
self.children.append(child)
def __repr__(self):
return '<{0}>'.format(self.name)
@akellehe
akellehe / graph.py
Created June 15, 2015 18:54
Constructing a lightweight graph.
graph = []
for name in xrange(6):
graph.append(Node(name))
graph[0].add_child(graph[1])
graph[0].add_child(graph[2])
graph[2].add_child(graph[3])
graph[2].add_child(graph[4])
graph[4].add_child(graph[5])
@akellehe
akellehe / graph_descendants.py
Created June 15, 2015 18:55
Gets the descendants using a graph structure, a queue, and a buffer.
def get_descendants(node):
unexplored = deque([node])
while unexplored:
node = unexplored.popleft()
yield node
children = node.children
for child in children:
unexplored.append(child)