title: Udacity CS101: Elementary Cellular Automaton in Python
body: <p>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!</p>
  
  <pre><code>def cellular_automaton(s,p,n):
      p = bin(p+256)[3:]
      s=s.replace('x', '1').replace('.', '0')
      while n&gt;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
  </code></pre>