<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6587284544385083247</id><updated>2011-07-31T08:34:42.488+01:00</updated><category term='python'/><title type='text'>Solutions Looking For Problems</title><subtitle type='html'>Coding for the love of it...</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://randombitsofcode.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6587284544385083247/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://randombitsofcode.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Samwise</name><uri>http://www.blogger.com/profile/06570763735523250866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://farm1.static.flickr.com/163/361928484_575ee2f9d7_t.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6587284544385083247.post-1010230807334047263</id><published>2009-03-20T17:25:00.011Z</published><updated>2009-03-24T10:34:40.362Z</updated><category scheme='http://www.blogger.com/atom/ns#' term='python'/><title type='text'>Calculate π by throwing darts</title><content type='html'>We can approximate pi using nothing more than random numbers and some simple geometry: in essence, we randomly throw darts at a square board of side r; within the square we inscribe a quadrant of a circle of radius r with its centre at (0, 0). We count all of the 'throws'; if a dart lands within the quadrant, we also count a 'hit'&lt;br /&gt;&lt;br /&gt;For a large number of throws, we see that:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 204, 204);"&gt;        hits    area_of_quadrant&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 204, 204);"&gt;       ------ = ----------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 204, 204);"&gt;       throws    area_of_square&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Some half-remembered geometry tells us that:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 204, 204);"&gt;       area_of_quadrant   (1 / 4) * pi * r^2   pi&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 204, 204);"&gt;       ---------------- = ------------------ = --&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 204, 204);"&gt;        area_of_square           r^2            4&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Or:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 204, 204);"&gt;                area_of_quadrant        hits&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 204, 204);"&gt;       pi = 4 * ---------------- = 4 * ------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 204, 204);"&gt;                 area_of_square        throws&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;I first solved this problem as an undergraduate sometime in 1994 as part of a Computational Physics module. Using FORTRAN.&lt;br /&gt;&lt;br /&gt;The full explanation can be found &lt;a href="http://icanhaz.com/calculatepi"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#!/usr/bin/env python&lt;br /&gt;&lt;br /&gt;import random&lt;br /&gt;import math&lt;br /&gt;&lt;br /&gt;# class representing a single throw of a dart&lt;br /&gt;class Throw:&lt;br /&gt;    def __init__(self):&lt;br /&gt;        # generate two random coordinates and work out how far away we are from&lt;br /&gt;        # the origin&lt;br /&gt;        self.x = random.random()&lt;br /&gt;        self.y = random.random()&lt;br /&gt;        self.distance = self.distance()&lt;br /&gt;&lt;br /&gt;    def distance(self):&lt;br /&gt;        # the distance from the origin is the hypotenuse of a right-angled &lt;br /&gt;        # triangle with sides of length and x and y. Pythagoras told us that:&lt;br /&gt;        #    distance = sqrt((x^2) + (y^2))&lt;br /&gt;        # which looks like this in python&lt;br /&gt;        return math.sqrt(self.x**2 + self.y**2)&lt;br /&gt;&lt;br /&gt;    # did we land inside the quadrant?&lt;br /&gt;    def is_a_hit(self):&lt;br /&gt;        return self.distance &lt;= 1.0&lt;br /&gt;&lt;br /&gt;# main class&lt;br /&gt;class MonteCarlo:&lt;br /&gt;    def __init__(self):&lt;br /&gt;        # initialise everything&lt;br /&gt;        self.hits = 0&lt;br /&gt;        self.throws = 0&lt;br /&gt;        self.pi = 0&lt;br /&gt;&lt;br /&gt;    # this method is called on every throw&lt;br /&gt;    def increment(self, throw):&lt;br /&gt;        # we always clock up another throw&lt;br /&gt;        self.throws += 1&lt;br /&gt;        # and accumulate a hit if we scored&lt;br /&gt;        if throw.is_a_hit():&lt;br /&gt;            self.hits += 1&lt;br /&gt;        # then get a new value of pi&lt;br /&gt;        self.calculate_pi()&lt;br /&gt;&lt;br /&gt;    # explanation can be found here: http://icanhaz.com/calculatingpi&lt;br /&gt;    def calculate_pi(self):&lt;br /&gt;        self.pi = 4 * (float(self.hits) / float(self.throws))&lt;br /&gt;&lt;br /&gt;    # we use this in determining whether to print our status&lt;br /&gt;    def divides_by(self, number):&lt;br /&gt;        return float(self.throws) % number == 0&lt;br /&gt;&lt;br /&gt;    # represent the current state as a string&lt;br /&gt;    def __repr__(self):&lt;br /&gt;        return "Throws: %10d, Hits: %10d, Pi: %10f, Actual Pi: %10f" % (self.throws, self.hits, self.pi, math.pi)&lt;br /&gt;&lt;br /&gt;# if we're called on the command line&lt;br /&gt;if __name__ == '__main__':&lt;br /&gt;    # construct a new instance&lt;br /&gt;    m = MonteCarlo()&lt;br /&gt;    # loop forever&lt;br /&gt;    while 1:&lt;br /&gt;        # keep throwing darts&lt;br /&gt;        m.increment(Throw())&lt;br /&gt;        # only print on every 100000th iteration&lt;br /&gt;        if m.divides_by(100000): print m&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Get the code here: &lt;code&gt;svn co http://pikesley.homedns.org/svn/code/MonteCarlo&lt;/code&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6587284544385083247-1010230807334047263?l=randombitsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://randombitsofcode.blogspot.com/feeds/1010230807334047263/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://randombitsofcode.blogspot.com/2009/03/calculate-by-throwing-darts.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6587284544385083247/posts/default/1010230807334047263'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6587284544385083247/posts/default/1010230807334047263'/><link rel='alternate' type='text/html' href='http://randombitsofcode.blogspot.com/2009/03/calculate-by-throwing-darts.html' title='Calculate π by throwing darts'/><author><name>Samwise</name><uri>http://www.blogger.com/profile/06570763735523250866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://farm1.static.flickr.com/163/361928484_575ee2f9d7_t.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6587284544385083247.post-1956214694654670960</id><published>2009-02-28T11:18:00.015Z</published><updated>2009-06-30T22:10:06.817+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='python'/><title type='text'>Permutations</title><content type='html'>We want to work out all the possible arrangements of a list of &lt;em&gt;n&lt;/em&gt; items. Why? We don't know. But we do know how to do it: seed with a list containing a single list containing a single item: &lt;code&gt;[[&lt;span style="font-weight: bold; color: rgb(51, 204, 255);"&gt;'a'&lt;/span&gt;]]&lt;/code&gt;. Then step through each of the lists in our list, and make copies, inserting the next item (&lt;span style="color: rgb(51, 204, 255);"&gt;'b'&lt;/span&gt;) at each index, so we get &lt;code&gt;[[&lt;span style="color: rgb(51, 204, 255);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(51, 204, 255);"&gt;b&lt;/span&gt;&lt;span style="color: rgb(51, 204, 255);"&gt;'&lt;/span&gt;, 'a'], ['a', &lt;span style="color: rgb(51, 204, 255);"&gt;'&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 255);"&gt;b&lt;/span&gt;&lt;span style="color: rgb(51, 204, 255);"&gt;'&lt;/span&gt;]]&lt;/code&gt;. Then we take this list as our seed and go round again, inserting the next item (&lt;span style="color: rgb(51, 204, 255);"&gt;'c'&lt;/span&gt;), so we get &lt;code&gt;[[&lt;span style="color: rgb(51, 204, 255);"&gt;'&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 255);"&gt;c&lt;/span&gt;&lt;span style="color: rgb(51, 204, 255);"&gt;'&lt;/span&gt;, 'b', 'a'], ['b', &lt;span style="color: rgb(51, 204, 255);"&gt;'&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 255);"&gt;c&lt;/span&gt;&lt;span style="color: rgb(51, 204, 255);"&gt;'&lt;/span&gt;, 'a'], ['b', 'a', &lt;span style="color: rgb(51, 204, 255);"&gt;'&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 255);"&gt;c&lt;/span&gt;&lt;span style="color: rgb(51, 204, 255);"&gt;'&lt;/span&gt;], [&lt;span style="color: rgb(51, 204, 255);"&gt;'&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 255);"&gt;c&lt;/span&gt;&lt;span style="color: rgb(51, 204, 255);"&gt;'&lt;/span&gt;, 'a', 'b'], ['a', &lt;span style="color: rgb(51, 204, 255);"&gt;'&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 255);"&gt;c&lt;/span&gt;&lt;span style="color: rgb(51, 204, 255);"&gt;'&lt;/span&gt;, 'b'], ['a', 'b', &lt;span style="color: rgb(51, 204, 255);"&gt;'&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(51, 204, 255);"&gt;c&lt;/span&gt;&lt;span style="color: rgb(51, 204, 255);"&gt;'&lt;/span&gt;]]&lt;/code&gt;. And so on and so on, until we've permuted all of our items. These lists get pretty big pretty quickly (the length of the list is &lt;span style="font-style: italic;"&gt;n!&lt;/span&gt; so 10 items generates 3628800 combinations, and doing this may bring your computer to its knees).&lt;br /&gt;&lt;br /&gt;I first tackled this problem ~10 years ago, but I don't think my solution then was as elegant as this one.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#!/usr/bin/env python&lt;br /&gt;&lt;br /&gt;import sys&lt;br /&gt;&lt;br /&gt;# class representing a list of permutations&lt;br /&gt;class Unit(list):&lt;br /&gt;# count the total number of Units spawned&lt;br /&gt;count = 0&lt;br /&gt;def __init__(self, seed = None):&lt;br /&gt;    # if this is the first Unit created, we need to prepare the seed&lt;br /&gt;    if Unit.count == 0: seed = [[seed]]&lt;br /&gt;    Unit.count += 1&lt;br /&gt;    # call our superclass. We are just a fancy list&lt;br /&gt;    super(Unit, self).__init__(seed)&lt;br /&gt;&lt;br /&gt;# produce a new generation, splicing in the passed in value&lt;br /&gt;def spawn(self, interloper):&lt;br /&gt;    # a temporary working list&lt;br /&gt;    l = []&lt;br /&gt;    # for each of our existing lists&lt;br /&gt;    for u in self:&lt;br /&gt;        # for each index (including length + 1)&lt;br /&gt;        for i in range(0, len(u) + 1):&lt;br /&gt;            # take a copy of the list&lt;br /&gt;            a = Unit(u)&lt;br /&gt;            # insert the new value at this index&lt;br /&gt;            a.insert(i, interloper)&lt;br /&gt;            # and add the modified copy to our working list&lt;br /&gt;            l.append(a)&lt;br /&gt;    # when we're done, return a new Unit seeded with the list we just built&lt;br /&gt;    return Unit(l)&lt;br /&gt;&lt;br /&gt;# calculate and show some stats&lt;br /&gt;def stats(self):&lt;br /&gt;    self.items = len(self[0])&lt;br /&gt;    self.combinations = len(self)&lt;br /&gt;    return "%d items, %d permutations" % (self.items, self.combinations)&lt;br /&gt;&lt;br /&gt;# simple wrapper class representing the set of items we want to permute&lt;br /&gt;class Inventory(list):&lt;br /&gt;def __init__(self, chars):&lt;br /&gt;    # we are yet another variation on a list&lt;br /&gt;    super(Inventory, self).__init__(chars)&lt;br /&gt;&lt;br /&gt;# do we have more items to give?&lt;br /&gt;def has_next(self):&lt;br /&gt;    return len(self) &gt; 0&lt;br /&gt;&lt;br /&gt;# shift the first item off of the list&lt;br /&gt;def next(self):&lt;br /&gt;    return self.pop(0)&lt;br /&gt;&lt;br /&gt;# wrapper class that actually does the work. We extend the Unit class mainly&lt;br /&gt;# because we want its stats() method&lt;br /&gt;class Permutations(Unit):&lt;br /&gt;def __init__(self, items):&lt;br /&gt;    # we need an inventory&lt;br /&gt;    self.i = Inventory(items)&lt;br /&gt;    # get the first item off of the inventory&lt;br /&gt;    self.u = Unit(self.i.next())&lt;br /&gt;    # now while the inventory has more items to give us&lt;br /&gt;    while self.i.has_next():&lt;br /&gt;        # make another generation of Units fed with the next item&lt;br /&gt;        self.u = self.u.spawn(self.i.next())&lt;br /&gt;    # at the end, we become whatever the final Unit.spawn() gave us&lt;br /&gt;    self.extend(self.u)&lt;br /&gt;&lt;br /&gt;# if we're called on the command line&lt;br /&gt;if __name__ == '__main__':&lt;br /&gt;# we will permute the command line arguments&lt;br /&gt;permutables = sys.argv[1:]&lt;br /&gt;# if we got no arguments, default to this&lt;br /&gt;if not permutables:&lt;br /&gt;    permutables = ["life", "the universe", "everything"]&lt;br /&gt;# do the work&lt;br /&gt;p = Permutations(permutables)&lt;br /&gt;# sort the result (a Permutation is just a fancy list, after all)&lt;br /&gt;p.sort()&lt;br /&gt;# and print it&lt;br /&gt;print p&lt;br /&gt;print p.stats()&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Get the code here: &lt;code&gt;svn co http://pikesley.homedns.org/svn/code/Combinatrics/tags/Release-2.0&lt;/code&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6587284544385083247-1956214694654670960?l=randombitsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://randombitsofcode.blogspot.com/feeds/1956214694654670960/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://randombitsofcode.blogspot.com/2009/02/permutations.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6587284544385083247/posts/default/1956214694654670960'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6587284544385083247/posts/default/1956214694654670960'/><link rel='alternate' type='text/html' href='http://randombitsofcode.blogspot.com/2009/02/permutations.html' title='Permutations'/><author><name>Samwise</name><uri>http://www.blogger.com/profile/06570763735523250866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://farm1.static.flickr.com/163/361928484_575ee2f9d7_t.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6587284544385083247.post-8241417368509372351</id><published>2009-02-06T23:20:00.007Z</published><updated>2009-02-21T18:37:51.750Z</updated><category scheme='http://www.blogger.com/atom/ns#' term='python'/><title type='text'>Evolve some text</title><content type='html'>We start with a target string. We then generate ten random candidates each of the same length as the target, and assign each one a score based on how many correct characters it has. The highest-scoring candidate then becomes the father of the next generation: we spawn ten clones of the winner, with the correct members of each retained and the other members randomised. Then we score each of these, and go round and round again until we get to the target string.&lt;br /&gt;&lt;br /&gt;Happy Birthday Darwin. And Python rocks.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#!/usr/bin/env python&lt;br /&gt;&lt;br /&gt;import sys&lt;br /&gt;import string&lt;br /&gt;import random&lt;br /&gt;import copy&lt;br /&gt;&lt;br /&gt;# the string we will work towards&lt;br /&gt;try:&lt;br /&gt;    target = sys.argv[1]&lt;br /&gt;except:&lt;br /&gt;    target = "from so simple a beginning endless forms most beautiful and most wonderful have been, and are being, evolved."&lt;br /&gt;winningscore = len(target)&lt;br /&gt;&lt;br /&gt;# get ascii character set&lt;br /&gt;chars = []&lt;br /&gt;for i in range(32, 127):&lt;br /&gt;    chars.append(chr(i))&lt;br /&gt;chars.append("\n")&lt;br /&gt;&lt;br /&gt;# how many candidates will we spawn in each generation&lt;br /&gt;selectees = 10&lt;br /&gt;&lt;br /&gt;# a list to hold our candidates&lt;br /&gt;candidates = []&lt;br /&gt;&lt;br /&gt;# class representing a character in our candidate&lt;br /&gt;class member():&lt;br /&gt;    def __init__(self, char = ""):&lt;br /&gt;        self.character = char&lt;br /&gt;        # fixity will be set if this character is correct&lt;br /&gt;        self.fixity = False&lt;br /&gt;&lt;br /&gt;    # set to a new, random character&lt;br /&gt;    def randomize(self):&lt;br /&gt;        # unless this one has been fixed&lt;br /&gt;        if not self.fixity:&lt;br /&gt;            a = chars[int(random.random() * len(chars))]&lt;br /&gt;            self.character = a&lt;br /&gt;&lt;br /&gt;    # string representation    &lt;br /&gt;    def __str__(self):&lt;br /&gt;        return self.character&lt;br /&gt;&lt;br /&gt;# class representing a generated candidate (it's basically a list)&lt;br /&gt;class candidate(list):&lt;br /&gt;    # we start with a score of 0&lt;br /&gt;    grade = 0&lt;br /&gt;    size = 0&lt;br /&gt;&lt;br /&gt;    # create a new list&lt;br /&gt;    def __init__(self, length):&lt;br /&gt;        self.size = length&lt;br /&gt;        # and populate and score it&lt;br /&gt;        self.populate()&lt;br /&gt;        self.score()&lt;br /&gt;&lt;br /&gt;    # fill the array with randomness to start&lt;br /&gt;    def populate(self):&lt;br /&gt;        for i in range(0, self.size):&lt;br /&gt;            m = member()&lt;br /&gt;            m.randomize()&lt;br /&gt;            self.append(m)&lt;br /&gt;&lt;br /&gt;    # represent as a string (this is an ugly hack, I think)&lt;br /&gt;    def __str__(self):&lt;br /&gt;        a = []&lt;br /&gt;        for i in self:&lt;br /&gt;            a.append(i.__str__())&lt;br /&gt;        return "".join(a)&lt;br /&gt;&lt;br /&gt;    # count how many correct members we have&lt;br /&gt;    def score(self):&lt;br /&gt;        self.grade = 0&lt;br /&gt;        for i in range(0, len(self)):&lt;br /&gt;            # if its a match with the target string&lt;br /&gt;            if self[i].character == target[i]:&lt;br /&gt;                # then we fix this member in place and score 1 point&lt;br /&gt;                self[i].fixity = True&lt;br /&gt;                self.grade += 1&lt;br /&gt;&lt;br /&gt;    # randomize ourself    &lt;br /&gt;    def shuffle(self):&lt;br /&gt;        for a in self:&lt;br /&gt;            # members with fixity set will not be randomized&lt;br /&gt;            a.randomize()&lt;br /&gt;        # and work out our new score&lt;br /&gt;        self.score()&lt;br /&gt;&lt;br /&gt;# find the candidate with the best score&lt;br /&gt;def getwinner():&lt;br /&gt;    bestgrade = 0&lt;br /&gt;    # assume the first one is the winner for now&lt;br /&gt;    winner = candidates[0]&lt;br /&gt;&lt;br /&gt;    # now for each candidate&lt;br /&gt;    for c in candidates:&lt;br /&gt;        # if we're better than the best so far&lt;br /&gt;        if c.grade &gt; bestgrade:&lt;br /&gt;            # hold on to the leader&lt;br /&gt;            winner = c&lt;br /&gt;            # and raise the bar&lt;br /&gt;            bestgrade = c.grade&lt;br /&gt;    # this method will pick the first candidate from the set of possible winners&lt;br /&gt;&lt;br /&gt;    # if all of our members are correct&lt;br /&gt;    if bestgrade == winningscore:&lt;br /&gt;        # we've solved it!&lt;br /&gt;        global unsolved&lt;br /&gt;        unsolved = 0&lt;br /&gt;    # send back the winner&lt;br /&gt;    return winner&lt;br /&gt;&lt;br /&gt;# real live code starts here&lt;br /&gt;&lt;br /&gt;# generate an initial set of random candidates&lt;br /&gt;for i in range(0, selectees):&lt;br /&gt;    candidates.append(candidate(len(target)))&lt;br /&gt;&lt;br /&gt;# assume we've not solved it yet&lt;br /&gt;unsolved = True&lt;br /&gt;&lt;br /&gt;# how may turns do we take?&lt;br /&gt;iterations = 0&lt;br /&gt;&lt;br /&gt;# while we have not the answer&lt;br /&gt;while (unsolved):&lt;br /&gt;    iterations += 1&lt;br /&gt;    &lt;br /&gt;    # find our winner&lt;br /&gt;    winner = getwinner()&lt;br /&gt;    # and show it&lt;br /&gt;    print winner&lt;br /&gt;&lt;br /&gt;    # set all of our candidates to the winner&lt;br /&gt;    for i in range(0, len(candidates)):&lt;br /&gt;        candidates[i] = copy.copy(winner)&lt;br /&gt;        # then shuffle each one&lt;br /&gt;        candidates[i].shuffle()&lt;br /&gt;&lt;br /&gt;# and when we're finished, print some stats&lt;br /&gt;print "%d-character string, %d iterations" % (len(target), iterations)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;!--Get the code here: &lt;code&gt;svn co http://pikesley.homedns.org/svn/Evolve&lt;/code&gt;--&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6587284544385083247-8241417368509372351?l=randombitsofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://randombitsofcode.blogspot.com/feeds/8241417368509372351/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://randombitsofcode.blogspot.com/2009/02/evolve-some-text.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6587284544385083247/posts/default/8241417368509372351'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6587284544385083247/posts/default/8241417368509372351'/><link rel='alternate' type='text/html' href='http://randombitsofcode.blogspot.com/2009/02/evolve-some-text.html' title='Evolve some text'/><author><name>Samwise</name><uri>http://www.blogger.com/profile/06570763735523250866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://farm1.static.flickr.com/163/361928484_575ee2f9d7_t.jpg'/></author><thr:total>0</thr:total></entry></feed>
