0
votes

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
1
Can you please provide the full traceback of the error message and mark the line in your code where the error occurs as indicated by the message.Levon
Actually I think I just figured out what the problem was: improper indentation where s=r. But thank you!RHH

1 Answers

0
votes

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.