Skip to content

Instantly share code, notes, and snippets.

@topher200
topher200 / genetic_hello_world.py
Created March 11, 2011 23:48
Gist version of genetic_hello_world.py, so I can embed it on my blog. From https://github.com/topher200/genetic-hello-world-python, as of commit 5c58bdb584ccf086fe7d
#!/usr/bin/python
import string
import random
LETTERS = string.ascii_letters + ' ' + '!' + '?'
class GeneticHelloWorld(object):
def __init__(self,
target = "Hello Python World!", # Target string we're going for
@topher200
topher200 / exmaple_from_genetic_hello_world.py
Created March 13, 2011 06:53
the main loop from my python example for my blog, source available: https://github.com/topher200/genetic-hello-world-python
def run(self):
# Create a random sample of chromos
sample = self.generate_random_chromosomes()
# Main loop: each generation select a subset of the sample and breed from
# them.
generation = -1
while self.fitness(sample[0]) != 0:
generation += 1
# Generate the selected group from sample- take the top 1% of samples
(defn generate-selected
"The selected group is generated by taking the best 10 chromos (elitism),
then repeated calling tourny-select on the sample to generate 90 more."
[sample]
(concat
(take 10 sample)
(take 90 (repeatedly #(tourny-select-chromo sample)))))
(defn generate-solution
"Generates the solution set by repeatedly selecting two chromos (at random)
@topher200
topher200 / ImageMagick_command.sh
Created March 18, 2011 06:36
Used to find all instances of one image in another. PNG output
compare -subimage-search floor-11.png crystal.png result-11-%d.png
@topher200
topher200 / imagemagick_to_coordinates.sh
Created March 18, 2011 06:49
Used to turn ImageMagick 'compare' output into the coordinates of all matches
cat result-11-1.txt | grep white | awk -F"[,| |:]" '{print "["$1 " " $2 "]"}'
@topher200
topher200 / gist:880804
Created March 22, 2011 05:06
In Inno Setup, figure out if we're running an upgrade or a fresh install
// Used to determine if we are running a new install or an upgrade by checking
// for the AppID in the registry.
function IsNewInstall(): Boolean;
var
AppID: String;
UninstallPath: String;
begin
AppID := '{enter-AppID-here}'
UninstallPath := 'Software\Microsoft\Windows\CurrentVersion\Uninstall\' + AppID + '_is1';
Result := not (RegKeyExists(HKEY_CURRENT_USER, UninstallPath)
@topher200
topher200 / gist:885476
Created March 24, 2011 17:27
Output from ImageMagick compare -subimage-search
...
768,419: (133,133,133) #858585 rgb(133,133,133)
769,419: (131,131,131) #838383 rgb(131,131,131)
770,419: (129,129,129) #818181 rgb(129,129,129)
771,419: (129,129,129) #818181 rgb(129,129,129)
772,419: (127,127,127) #7F7F7F rgb(127,127,127)
773,419: (126,126,126) #7E7E7E rgb(126,126,126)
774,419: (126,126,126) #7E7E7E rgb(126,126,126)
775,419: (137,137,137) #898989 rgb(137,137,137)
776,419: (255,255,255) #FFFFFF white
@topher200
topher200 / gist:885581
Created March 24, 2011 18:31
Cleaned coordinate output from ImageMagick
[776 419]
[714 421]
[645 422]
[643 474]
[777 477]
[640 540]
[778 540]
[707 543]
@topher200
topher200 / gist:923787
Created April 17, 2011 05:48
find-prime using Java's BigInteger in Clojure
(defn find-prime [bit-length]
(. BigInteger (probablePrime bit-length (new java.util.Random))))
@staticmethod
def _hash_function(string):
# Covert each char of string to an ordinal number and sum them. Mod this
# by the number of buckets.
return sum(map(ord, string)) % num_buckets