Skip to content

Instantly share code, notes, and snippets.

View maxcountryman's full-sized avatar
🦀

Max Countryman maxcountryman

🦀
View GitHub Profile
@maxcountryman
maxcountryman / weasel_redux.py
Created January 6, 2011 22:14
Weasel program redux
import string, random
CHARS = string.uppercase + ' '
PROGENY = 100
RATE = 0.05
TARGET = 'METHINKS IT IS LIKE A WEASEL'
genotype = lambda s : [random.choice(s) for x in xrange(len(TARGET))]
phenotype = ''
@maxcountryman
maxcountryman / weasel_tester.py
Created January 7, 2011 22:13
Weasel program revisited with simple timing and testing functions
import string, random, time
CHARS = string.uppercase + ' '
TEST = True
class Biosphere(object):
'''Attempts to find `target` by producing generations of random strings that
mutate by `rate` percent. Generational population is determined by
`progeny`.
'''
@maxcountryman
maxcountryman / bf_coverter_interpreter.py
Created January 10, 2011 01:59
Brainfuck converter and interpreter
class Converter(object):
'''Converts a string to Brainfuck.'''
def __init__(self, string):
self.output = self._parse(string)
def _parse(self, string):
'''Takes a string, redefines it as a list containing its ASCII values,
rounds the values down, creates loop cells using those values, minus
the first decimal, i.e. 72 becomes 70 and then 7, this is because the
@maxcountryman
maxcountryman / brainfuck_genetic_algo.py
Created January 13, 2011 17:48
This is my poor attempt at implementing a genetic algorithm that will evolve Brainfuck towards a target string.
import random, time, string
CHARS = '-+<>[].'
SEED_RANGE = 350
GAUSS_RANGE = 15
TIMEOUT = 0.0009
class Biosphere(object):
'''Attempts to find `target` by producing generations of random strings that
mutate by `self.rate` percent. Generational population is determined by
@maxcountryman
maxcountryman / shift.py
Created January 18, 2011 20:36
Another simple Caesar cipher
# foo = shift('string',3)
# shift(foo, -3)
shift = lambda s,i=3 : ''.join(map(lambda c : chr(c + i), map(ord, s)))
@maxcountryman
maxcountryman / markov_chain.py
Created January 31, 2011 00:25
Simple Markov chain generator
# big.txt here --> http://norvig.com/big.txt
from random import randrange, choice
MIN = 3
MAX = 2
class MarkovChain(object):
'''Provides the necessary functions to generate a Markov chain.
@maxcountryman
maxcountryman / add.py
Created April 18, 2011 09:24
recursive bitwise addition
add = lambda x, y : add((x & y) << 1, x ^ y) if x != 0 else y; add(13,29)
def add_again(x, y):
if x == 0:
return y
else:
return add_again((x & y) << 1, x ^ y)
@maxcountryman
maxcountryman / ringbuffer.py
Created August 5, 2011 18:22
A simple circular "ring" buffer class
class RingBuffer(object):
'''This data structure provides a circular "ring" queue, limited by
n-number of positions as `length`. Once the ring is full, i.e. the length
is exceed, the head of the queue is deleted. In this way we acheive a
first in, first out circular buffer.'''
def __init__(self, length):
self.queue = []
self.keys = []
self.length = length
@maxcountryman
maxcountryman / radioreddit.py
Created September 13, 2011 18:58
radio redit plugin for an irctk app
# radioreddit stuff
@bot.command
def status(stream):
streams = [
'rock',
'electronic',
'hiphop',
'random',
'talk',
@maxcountryman
maxcountryman / weasel.c
Created December 20, 2011 02:16
Weasel in C
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
char* ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
char* TARGET = "ME THINKS IT IS LIKE A WEASEL";
float RATE = 0.05;
int GENERATIONS = 250;
int ALPHABET_LEN = 27;