So let's look at what the basic idea of the sequence is (if I understand correctly):
- Break the sequence into contiguous blocks of identical values.
- Figure out the lengths of those blocks.
- Create a new sequence with the length of each block followed by the value of that block.
Step 1 seems to be the problem here. The key issue is to me is that you are rolling your own solution when python provides something to do step 1 for you: itertools.groupby.
So consider your last number:
>>> from itertools import groupby
>>> inp = "1113122113"
>>> print([(k, list(g)) for k, g in groupby(inp)])
[('1', ['1', '1', '1']), ('3', ['3']), ('1', ['1']), ('2', ['2', '2']), ('1', ['1', '1']), ('3', ['3'])]
This gives you an iterator of tuples, where the first tuple is the value of the group and the second is the group itself. You can use the value of the group for second element of each number pair and length of the group as the first element. Then you just have to put them together into a new string.
So the algorithm can be reduced to:
new = ''.join(str(sum(1 for _ in g))+k for k, g in groupby(inp))
- Your multi-run is done in the root of the script. It would be better to give it its own function.
- You are printing in the same function that generates the sequence. This assumes you will always want to print. Better to just return the results and let whatever calls the function determine what to do with those results.
- You should put all the automatically-run code in
if __name__ = '__main__': so you can use the functions elsewhere.
- You should use
from __future__ import print_function to make your code python3 compatible.
_ in place of
i for loop indices you don't care about.
- You can use default function arguments to make the code a bit simpler to run
So here is my complete solution:
from __future__ import print_function
return ''.join(str(sum(1 for _ in g))+k for k, g in groupby(num))
def multiiter(n, inp='1'):
for _ in range(n):
inp = iterate(inp)
if __name__ == '__main__':
Using a version that commented out the
print took 5.53 seconds to run 50 answers.
Edit: Included Copperfield's