Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

python - 设置游程编码长度的最小限制(setting a minimum limit on run length encoding length)

def encode (plainText):
    res=''
    a=''
    for i in plainText:
        if a.count(i)>0:
           a+=i
        else:
            if len(a)>3:
                res+="/" + str(len(a)) + a[0][:1]
            else:
                res+=a
                a=i
    return(res)

this is my current code.

(这是我当前的代码。)

for those of you who know about run length encoding, it can make files larger because a single value becomes two.

(对于那些了解游程长度编码的人来说,它可以使文件变大,因为单个值变为两个。)

I am trying to set a minimum length of 3, so that it would actually compress.

(我正在尝试将最小长度设置为3,以使其实际压缩。)

any help with code corrections, suggestions are greatly appreciated.

(任何有关代码更正的帮助,建议将不胜感激。)

  ask by tre rossi translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This should work:

(这应该工作:)

plainText = "Hellow world"

def encode (plainText):
    count = 1
    last_sym = ""
    rle = ""

    for i in plainText:

        if i == last_sym:
            count = count + 1

        elif i != last_sym:
            if count > 2:
                if count < 10:
                    n = str("0") + str(count)
                    rle = rle + n + i

                else:
                    rle = rle + str(count) + i

            else:
                rle = rle + i
            count = 1
            last_sym = i

    return rle









rle = encode(plainText)
print(rle)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...