Domanda

Can anyone tell me what the issue with my code is (the line with "num" it seems). I'm getting string index out of range, but in an almost identical chunk of code it seems to work. If there's some code I can look at in python I'd love to see links to it as well. Thanks!

def cellular_automaton(s,p,n):
    p = bin(p+256)[3:]
    s=s.replace('x', '1').replace('.', '0')
    while n>0:
        N = len(s)
        r=''
        for i in range(N):
            num = int(s[(i - 1) % N] + s[i] + s[(i + 1) % N], 2)
            r += p[-1 - num]
            s = r
        n-=1
    s=s.replace('x', '1').replace('.', '0')
    return s
È stato utile?

Soluzione

Sorry for the trouble!

The problem seems to come from improper indentation of the line

s=r

I hope that this is useful to someone! Also I'd love to see suggestions for improving this method as well.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top