Skip to content

Instantly share code, notes, and snippets.

@davidkatz
Created November 12, 2012 12:46
Show Gist options
  • Save davidkatz/4059212 to your computer and use it in GitHub Desktop.
Save davidkatz/4059212 to your computer and use it in GitHub Desktop.
# takes an organism and returns a copy that may be mutated
def copy_and_mutate(organism):
# first, make a copy
new_organism = list(organism);
# to start the mutation action, define events and their string constants
letter_change = 'LETTER_CHANGE'
letter_deletion = 'LETTER_DELETION'
insert_space = 'INSERT_SPACE'
# define probabilities for mutation events
event_probabilities = {
letter_change: config.LETTER_CHANGE_CHANCE,
letter_deletion: config.LETTER_DELETION_CHANCE,
insert_space: config.INSERT_SPACE_CHANCE
}
# iterate over the organism, allow mutations to happen
for i,c in enumerate(new_organism):
event = roll_weighted_die(event_probabilities)
if event == letter_change:
#print event
new_organism[i] = random.choice(config.POSSIBLE_CHARACTERS)
if event == insert_space:
#print event
new_organism.insert(i,' ')
if event == letter_deletion:
#print event
del new_organism[i]
return new_organism
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment