문제

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
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top