Here's Peter Norvig's treatment of the zebra puzzle from his CS212 course.
This file contains hidden or 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
class A(object): | |
def hook(self, f): | |
atime = 0 | |
def intime(*args): | |
print atime | |
atime += 1 | |
return f(*args) | |
return intime | |
>>> f = A().hook(lambda b: b + 1) |
This file contains hidden or 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
#!/usr/bin/env python | |
from collections import namedtuple | |
from struct import Struct | |
class NamedStruct(Struct): | |
def __init__(self, name, fields, byteorder=None): | |
fmt = (byteorder or '') + ''.join(field[0] for field in fields) | |
super(NamedStruct, self).__init__(fmt) | |
self.namedtuple = namedtuple(name, (field[1] for field in fields)) |
This file contains hidden or 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
var fs = require('fs'), | |
path = require('path'), | |
glob = require('glob').glob; | |
exports = module.exports = function jam(options) { | |
options = options || {}; | |
var patterns = options.patterns || []; | |
var root = options.root || path.join(__dirname, 'public'); | |
var namespace = options.namespace || 'JST'; | |
var output = options.output || path.join(root, 'jst.js'); |
This file contains hidden or 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
from contextlib import contextmanager | |
@contextmanager | |
def save(obj, attrs=None): | |
"""Save attributes of an object, then restore them. | |
Example: | |
import breakfast | |
with save(breakfast, ('eggs', 'bacon')): | |
breakfast.Eggs = lambda: "Eggs" |
This file contains hidden or 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
<html><body> | |
<h1>Find a Unique Username</h1> | |
<p>With a limit of 15 characters</p> | |
<form method="get"> | |
<label>First Name <input name="first_name" /></label> | |
<label>Last Name <input name="last_name" /></label> | |
<label>Quit after <input name="end" value="111" /></label> |