methods.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:compresso 作者: VCG 项目源码 文件源码
def decompress(data, *args, **kwargs):
        from cStringIO import StringIO

        data = list(data)

        dict_size = 256
        dictionary = dict((i, chr(i)) for i in xrange(dict_size))

        result = StringIO()
        w = chr(data.pop(0))
        result.write(w)

        for k in data:
            if k in dictionary:
                entry = dictionary[k]
            elif k == dict_size:
                entry = w + w[0]
            else:
                raise ValueError('Bad compressed k: %s' % k)
            result.write(entry)

            # Add w+entry[0] to the dictionary
            dictionary[dict_size] = str(w + entry[0])
            dict_size += 1

            w = entry

        return result.getvalue()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号