- 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
AJAX version: | |
<form action="HANDLED BY AJAX" id="rate-movie-form"> | |
<label>Rating: | |
<input type="number" name="score" id="score"> | |
</label> | |
<input type="submit" value="Rate"> | |
</form> | |
<script> | |
"use strict"; |
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 sys | |
from select import poll, POLLIN | |
def time_rawinput(prompt, timeout): | |
print(prompt, end="") | |
sys.stdout.flush() | |
p = poll() | |
p.register(sys.stdin, POLLIN) | |
events = p.poll(timeout * 1000) | |
if events: |
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 |
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
"""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
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
"""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
"""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
// 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 |