Skip to content

Instantly share code, notes, and snippets.

View joelburton's full-sized avatar

Joel Burton joelburton

View GitHub Profile
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
</head>
<body>
Limit: <input id="num"> <button>Show</button>
<ul id="melons">
@joelburton
joelburton / tree.py
Last active August 29, 2015 14:23 — forked from anonymous/file1.py
A neat idea
class Node:
def __init__(self, data, children=None):
self.data = data
self.children = children or []
def find(self, data):
for i in self.descendants():
if i.data == data:
return i
def descendants(self):
yield self
@joelburton
joelburton / stringplay.c
Last active August 29, 2015 14:24
Demonstrate C string creation & mutability
/**
* Demonstrate types of strings & mutability & storage
*
* Joel Burton <joel@joelburton.com>
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@joelburton
joelburton / gist:275126c3df0191e4a1d0
Created July 15, 2015 06:34
Binary Tree insertion in C
void insertNode(TreeNode **root, COMPARE compare, void* data) {
TreeNode *node = (TreeNode*) malloc(sizeof(TreeNode));
node->data = data;
node->left = NULL;
node->right = NULL;
if (*root == NULL) {
*root = node;
return;
}
@joelburton
joelburton / cdecl.c
Created July 16, 2015 08:53
C Declaration Explainer
/* C Declaration explainer.
*
* Reads a C declaration from standard input and explains what it means.
*
* For example:
*
* $ echo "char *stuff[]" | this_program
* stuff is an array of pointer to char
*
* Adapted from Peter van der Linden's "Expert C Programming".
__version__ = "1.0"
class Cat(object):
def __init__(self):
print "making cat"
print "see?", type(self)
self.name = "fluffy"
def __add__(self, other):
if not type(other) == Cat:
@joelburton
joelburton / eeevil.py
Last active February 23, 2017 09:24
Hangman variant that sneakily changes the chosen-word to foil the player.
"""Evil Hangman.
Hangman, except avoid choosing a real word and, on a letter guess, reduce
the set of possible words to the set that gives the player as little
advantage as possible.
"""
import random, collections
word_len = int(input("what length word do you want (2-20) [6]") or 6)
import sys
from cStringIO import StringIO
class fake_input(object):
def __init__(self, *args):
text = "\n".join(args) + "\n"
self.stdin = sys.stdin
self.input = StringIO(text)
def __enter__(self):
sys.stdin = self.input
@joelburton
joelburton / wordguess.py
Last active March 1, 2017 21:56
Simple demonstration of using Python's stdlib "cmd" to make an interactive game.
"""Console-based guess-the-secret-word game."""
import cmd
import random
class WordGuess(cmd.Cmd):
"""REPL for a guess-the-secret-word game."""
prompt = "\nWordGuess > "
intro = "Guess the secret word! (Control-D quits)"
@joelburton
joelburton / server.py
Created March 27, 2017 05:23
AJAX movie rating snippet
@app.route("/movies/ajax/<int:movie_id>", methods=['POST'])
def movie_detail_ajax_process(movie_id):
"""Add/edit a rating."""
# Get form variables
score = int(request.form["score"])
user_id = session.get("user_id")
if not user_id: