基本字符串压缩

2015-10-19 0 535
基本字符串压缩
class Zipper:
    def zipString(self, iniString):
        nlen = len(iniString)
        rest =[]
        i = 0
        endflag = 0
        while i < (nlen-1):
            for j in range(i+1, nlen):
                if iniString[i] == iniString[j]:
                    continue
                else:
                     rest.append((iniString[i], i, j))
                     endflag = j
                     break
            i = j

        rest.append((iniString[endflag], endflag, nlen))
        resStr = ""
        for item in rest:
            resStr += (item[0] + str(item[2]-item[1]))

        if len(resStr) >= nlen:
            return iniString
        else:
            return resStr
a = Zipper()
print a.zipString('aabcccccaaa')

b = Zipper()
print b.zipString('welcometonowcoderrrrr')

c = Zipper()
bstr= c.zipString('qwertyuioplkjhgfdsAzxcvbNM')
print type(bstr)

遇见资源网 python 基本字符串压缩 http://www.ox520.com/15846.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务