Skip to content

Instantly share code, notes, and snippets.

View joelburton's full-sized avatar

Joel Burton joelburton

View GitHub Profile
@joelburton
joelburton / poly.py
Created September 25, 2018 22:08
polymorphism
# Imagine with have some classes for animals:
class Animal:
"""Base class for all animals."""
def __init__(self, name):
self.name = name
class Dog(Animal):
@joelburton
joelburton / markov.js
Created April 2, 2018 18:38
Simple textual markov chain generator
// Textual markov chain generator - joel@joelburton.com
const fs = require('fs');
const util = require('util');
const process = require('process');
const preadFile = util.promisify(fs.readFile);
// findPivot:
// find index of "pivot" in rotated sorted nums
function findPivot(nums, firstVal, low, high) {
if (high < low) return 0; // no pivot
const mid = Math.floor((low + high) / 2);
if (nums[mid] < firstVal) {
// we're at the pivot point *or* it's further left
"""Simple example of auto-setting attribs from __init__ and using super()"""
class AutoAttr():
"""Class that sets arbitrary instance attributes from init keywords."""
def __init__(self, **kwargs):
# A useful little hack: allows you to specify arbitrary keywords
# and sets all of them on the newly-instantiated object
@joelburton
joelburton / spatula.py
Created August 15, 2017 05:19
Simple example of decorator-based reg/dispatcher
"""Example of a decorator-based registry/dispatcher for handling files by filename match."""
import re
class Spatula:
"""A registry/dispatcher."""
def __init__(self):
self.loaders = []
@joelburton
joelburton / basic_decorator.py
Created August 15, 2017 05:18
Simple use of debugging-style decorator
import time
def debug(fn):
def inner():
_start = time.time()
print(f"running {fn}")
out = fn()
print(f"done {fn}, took {time.time() - _start}")
return out
@joelburton
joelburton / traversell.py
Created May 5, 2017 17:40
Practice recursion problems for Hackbright warmups
"""Demonstrate different recursive patterns."""
class LLNode(object):
def __init__(self, value, next=None):
self.data = value
self.next = next
llist = LLNode(10, LLNode(20, LLNode(30, LLNode(40, LLNode(50)))))

Binary Search Trees

Definitions

Tree

A structure made up of nodes, where a node has 0 or 1 parent (the node with no parents is a "root"). Each node can have 0 or more children. Nodes without

@joelburton
joelburton / tsearch.c
Last active May 2, 2017 16:51
Example of using glibc tsearch
/** Demonstration of tsearch. */
#include <search.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node {
char *name;
@joelburton
joelburton / lizards.py
Created April 22, 2017 01:23
Snake arcade game, Python-style!
"""Looping Lizards snake arcade game.
You can call this like:
$ python lizards.py [debug] [speed]
Both arguments are optional:
debug
If the string debug is provided, logs to /tmp/log