ant=['1']
round = 30
while round:
ant += '!'
next = []
start = 0
for current in range(len(ant)):
if ant[current] != ant[start]:
next.append(str(current-start)+ant[start])
start = current
ant = "".join(next)
round-=1
print len(ant)
I got this source in a blog and tried to run this on 3.2.
(It’s about making the ant sequence. [1,11,12,1121,&c]
But at line 10, ‘IndexError : string index out of range’ pops and I hardly understand why.
your code is run fine on my computer. My python version is 2.7
the
len(ant)is 5808But I think you python code is not very clear and not pythonic. You can read this link and this
for example, use this
instead of
for current in range(len(ant)):And don’t use
ant +='!'. It should beant.append('!')Good Luck