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 / parse_article_links.py
Created November 30, 2015 01:21
Parse article links out of wikipedia string
import mwlib.parser.nodes
import mwlib.uparser
import codecs
with codecs.open('us.dat', 'rb', encoding='utf-8') as fp:
text = fp.read()
article = mwlib.uparser.parseString(title='us', raw=text)
nodes = article.find(mwlib.parser.nodes.ArticleLink)
for n in nodes:
5 procs started on release0.13.3 @ 13:45
var downstream = function(buzz_id, root_id, limit) {
if (root_id == null){
return db.user_edge_v2.find({
"buzzid": buzz_id
}).count()
}
var limit = typeof limit != 'undefined' ? 1000 : limit;
var seen = {root_id: true};
var nodeq = [root_id];
var downstream_count = 0;
@akellehe
akellehe / should_add.py
Created June 15, 2015 18:59
Criteria for adding an edge to a tree.
def should_add(self, edge):
return all([
self.would_be_leaf(edge),
self.is_earliest_parent(edge)])
@akellehe
akellehe / online.py
Created June 15, 2015 18:58
Two methods for computing graph statistics online. Tree name and edge are the only inputs.
conn = StrictRedis()
def online_size(sender, tree_name=None, edge=None):
conn.incr('{0}.size'.format(tree_name))
def online_depth(sender, tree_name=None, edge=None):
with conn.lock('{0}.depth'.format(tree_name)):
current_depth = conn.get('{0}.depth'.format(tree_name)) or 0
if int(current_depth) < edge.generation:
conn.set('{0}.depth'.format(tree_name), edge.generation)
@akellehe
akellehe / edge.py
Created June 15, 2015 18:57
A basic edge in a tree.
class Edge(object):
def __init__(self, parent, child, created_at):
self.parent = parent
self.child = child
self.created_at = created_at
self.generation = 0
@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)
@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 / 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)