import itertools houses = [1, 2, 3, 4, 5] orderings = list(itertools.permutation(houses)) for (red, green, ivory, yellow, blue) in orderings: for (Englishman, Spaniard, Ukranian, Japanese, Norwegian) in orderings: for (dog, snails, fox, horse, ZEBRA) in orderings: for (coffee, tea, milk, oj, WATER) in orderings: for (oldGold, kools, Chesterfields, LuckyStrike, Parliaments) in orderings:
def zebra_puzzle(): houses = first, _, middle, _, _ = [1, 2, 3, 4, 5] orderings = list(itertools.permutations(houses)) return next((WATER, ZEBRA)
import time def t(): t0 = time.clock() zebra_puzzle() t1 = time.clock() return t1-t0 print t() def timecall(fn, *args): t0 = time.clock() result = fn(*args) t1 = time.clock() return t1-t0
def timecall(fn, *args): t0 = time.clock() result = fn(*args) t1 = time.clock() return t1-t0 def timedcalls(n, fn, *args): times = [timedcall(fn, *args)[0] for _ in range(n)] return min(times), average(times), max(times) def average(numbers): return sum(numbers) / float(len(numbers))
def instrument_fn(fn, *args): c.starts, c.items = 0, 0 result = fn(*args) print '%s got %s with %5d iters over %7d items' % ( fn.__name__, result, c.starts, c.items)
def ints(start, end):
i = start
while i <= end
yield i
i = i + 1
[/python]
[python]
def all_ints():
"Generate integers in the order 0, +1, -1, +2, -2, +3, -3,..."
yield 0
for i in ints(1):
yield +i
yield -i
[/python]
[python]
import string, re
def valid(f):
try:
return not re.search(r'\b0[0-9]', f) and eval(f) is True
except ArithmeticError:
return False
[/python]
[python]
import string, re
def solve(formula):
def valid(f):
try:
return not re.search(r'\b0[0-9]', f) and eval(f) is True
except ArithmeticError:
return False
[/python]