Such a tour exists only when the tree is a caterpiller tree which is a tree consisting of a straight path with some leafs attached to nodes on that path. Using this observation, getting a recurrence and solving it using Dynamic Programming is greatly simplified. Also, working out some examples on paper, it is easy to see that the answer is 2 * n1! * ... * nk! where n1,..,nk are the number of leafs attached to each node on the path defining the caterpiller tree. A special case is when the tree is a star, when the answer is simply (n - 1)!. See: http://en.wikipedia.org/wiki/Caterpillar_tree
# Greatest common divisor of 1 or more numbers. | |
from functools import reduce | |
def gcd(*numbers): | |
""" | |
Return the greatest common divisor of 1 or more integers | |
Examples | |
-------- |
# Sort a list of dictionary objects by a key - case sensitive | |
from operator import itemgetter | |
mylist = sorted(mylist, key=itemgetter('name')) | |
# Sort a list of dictionary objects by a key - case insensitive | |
mylist = sorted(mylist, key=lambda k: k['name'].lower()) |
'''Parens | |
Enumerate nested parenthesis. | |
Implements Algorithm P from Knuth's the Art of Computer Programming v. 4 | |
directly with minimal optimizations for python. | |
Usage: python parens.py number | |
''' | |
import sys |
# Unoptimized, and actually wrong code | |
# Written to solve problems in my textbook, and does that well | |
# Don't use, don't judge. | |
def solve(functions): | |
def solve_recurse(functions, values, count): | |
old_values = values[:] | |
for i in range(len(functions)): | |
values[i] = f(*values) | |
print values |
/* | |
Copyright (c) 2011 Andrei Mackenzie | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
Value objects are an important concept in DDD. This kata is made both to learn value objects and to learn better ways of testing. | |
Write a probability value object. It should contain the following methods: | |
Probability CombinedWith(Probability) | |
Probability InverseOf() | |
Probability Either(Probability) | |
if you forget your probability math: | |
Either:P(A) + P(B) - P(A)P(B) | |
CombinedWith: P(A)P(B) |
#!/bin/bash | |
# http://codekata.pragprog.com/2007/01/kata_four_data_.html | |
awk '{print (($7 - $9) < 0 ? -($7 - $9) : $7 - $9) " " $2}' football.dat | grep -P "[A-Za-z_]{2,}" | sort -n | head -n 1 | awk '{print $2}' |
Write a function that returns true or false depending on whether its input integer is a leap year or not. | |
A leap year is divisible by 4, but is not otherwise divisible by 100 unless it is also divisible by 400. | |
2001 is a typical common year | |
1996 is a typical leap year | |
1900 is an atypical common year | |
2000 is an atypical leap year |
Considering each permutation of the letters as a variable, we get a number of simultaneous equations which can be solved using Gaussian Elimination to compute the expected value of each permutation. However, the number of variables is very large. This can be reduced by making the observation that many states such as "abab" and "baba" are essentially the same since the letters in them are simply relabelled. Thus we can normalize each string by letting it start with an 'a', replacing the next occuring character with a 'b' and so on. For example, string "paddpa" would be normalized to "abccab". This reduces the number of variables greatly, and also helps us efficiently memoize across various test cases. Also, we should note that running one Gaussian Elimination gives us the expected values for many states (and not just one), all of which should be saved for future reference.