In [24]:
def transform(s):
t = s + '\\'
say_list = ''
counter = 1
for i in range(len(s)):
if t[i] == t[i + 1]:
counter += 1
else:
say_list += str(counter) + t[i]
counter = 1
return say_list
In [25]:
def test():
assert(transform('1') == '11')
assert(transform('11') == '21')
assert(transform('21') == '1211')
assert(transform('1211') == '111221')
assert(transform('111221') == '312211')
test()
In [29]:
def iterate_transform(s, n):
for _ in range(n):
s = transform(s)
return len(s)
In [30]:
iterate_transform('3113322113', 40)
Out[30]:
In [31]:
%%time
iterate_transform('3113322113', 50)
Out[31]: