- 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Imagine with have some classes for animals: | |
class Animal: | |
"""Base class for all animals.""" | |
def __init__(self, name): | |
self.name = name | |
class Dog(Animal): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Demonstration of tsearch. */ | |
#include <search.h> | |
#include <stdbool.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
typedef struct Node { | |
char *name; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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 |