[Edu-sig] more mathematics for the digital age....

kirby urner kirby.urner at gmail.com
Wed Oct 29 17:20:27 CET 2014

(c) MIT License by K. Urner

More playing around with NKS for a Gnu Math class using Python
NKS = new kind of science (Wolfram) i.e. cellular automata (CA)

def make_rule(n):
    """takes 0-255 and returns rule dict"""
    the_rule = {}
    values = "{:08b}".format(n)
    for x in range(8):
        key = "{:03b}".format(x)
        the_rule[key] = values[x]
    return the_rule

def transcribe(s, r):
    """takes a row s and returns a row based on rule r"""
    n = 0
    max = len(s)
    out = ''
    while n < max-2:
        triple = s[n:n+3]
        out += r[triple]
        n += 1
    return out

def generations(s, r, n=1):
    """applies rule r to transcribed output, starting with s, n times"""
    for _ in range(n):
        row = transcribe(s, r)
        s = row
        yield row

rule130 = make_rule(130)
top = '000000000000010000000000000'

the_gen = generations(top, rule130, 10)

while True:
    dots += 1
        print("."*dots, next(the_gen), sep="")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20141029/e4365666/attachment.html>

More information about the Edu-sig mailing list