面试的时候,碰到一笔试题。
已知:t = ‘aaabbcccda’ ,输出 ‘a3b2c3d1a1’ 的格式
t = 'aaabbcccda'
cnt = 0
cha = t[0]
rv = []
for c in t:
if c == cha:
cnt +=1
else:
rv.append( (cha,cnt) )
cnt = 1
cha = c
rv.append( (cha,cnt) )
Sstr = ''
for charac,nr in rv:
Sstr = Sstr + charac + str(nr)