just a bear whose intentions are good (two_star) wrote,
just a bear whose intentions are good
two_star

Python code for look-and-say walk

Here's the Python code I used to make the look-and-say walk.The svg.py that it uses is this recipe.

import svg
def looksay(l):
    out = []
    last = None
    while l:
        x = l.pop(0)
        if x == last:
            out[-2] += 1
        else:
            out.append(1)
            out.append(x)
        last = x
    return out

def walk(l):
    out = []
    pos = (0,0)
    dir = 0
    while l:
        x = l.pop(0)
        if x == 1:
            dir = (dir - 1) % 4
        elif x == 2:
            dir = (dir + 1) % 4
        if dir == 0:
            pos = (pos[0], pos[1] + 1)
        elif dir == 1:
            pos = (pos[0] + 1, pos[1])
        elif dir == 2:
            pos = (pos[0], pos[1] - 1)
        elif dir == 3:
            pos = (pos[0] - 1, pos[1])
        else:
            print "error"
        out.append(pos)
    return out

def plotwalk(l):
    size = 1500
    scene = svg.Scene('looksaywalk', size, size)
    scalefactor = 2
    last = l.pop(0)
    origin = (size/2, size/2)
    while l:
        curr = l.pop(0)
        scene.add(svg.Line((scalefactor * last[0] + origin[0], scalefactor * last[1] + origin[1]), 
                           (scalefactor * curr[0] + origin[0], scalefactor * curr[1] + origin[1])))
        last = curr
    scene.write_svg()
    scene.display()

l = [1]
for x in xrange(36):
    l = looksay(l)

w = walk(l)
plotwalk(w)
Tags: look-and-say, python
Subscribe

  • look, say, walk

    1, 11, 21, 1211, 111221, 312211... Those are the first few numbers in the look-and-say sequence, so called because it is formed by looking at the…

  • Swan Sovereign / The Cabin Project

    Went to a show at the Doug Fir last night. The opening band was The Cabin Project. The lead singer's vocal stylings reminded me a bit of Neko Case,…

  • Blog posts from Greater September

    Hinged Polyforms: Flexible Polyrhombs: Some Contributed Solutions:

  • Post a new comment

    Error

    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments