Reputation: 315
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.
Upvotes: 0
Views: 132
Reputation: 1931
your code is run fine on my computer. My python version is 2.7
the len(ant)
is 5808
But I think you python code is not very clear and not pythonic. You can read this link and this
for example, use this
for index, x in enumerate(ant):
instead of for current in range(len(ant)):
And don't use ant +='!'
. It should be ant.append('!')
Good Luck
Upvotes: 1
Reputation: 526573
Your code runs fine for me.
test.py:
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)
$ python test.py
5808
Upvotes: 1