首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >设置游程长度编码长度的最小限制

设置游程长度编码长度的最小限制
EN

Stack Overflow用户
提问于 2019-11-28 15:50:47
回答 1查看 66关注 0票数 0
代码语言:javascript
运行
复制
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)

这是我当前的代码。对于那些了解游程长度编码的人来说,它可以使文件变得更大,因为一个值变成了两个值。我正在尝试将最小长度设置为3,这样它实际上就会压缩。任何关于代码更正的帮助,建议都是非常感谢的。

EN

Stack Overflow用户

发布于 2019-12-01 21:46:06

你在这里犯了很多错误,下面是一个小列表。

代码语言:javascript
运行
复制
1  def encode (plainText):
2      res=''
3      a=''
4      for i in plainText:
5          if a.count(i)>0:
6             a+=i
7          else:
8              if len(a)>3:
9                  res+="/" + str(len(a)) + a[0][:1]
10             else:
11                 res+=a
12                 a=i
13     return(res)

计数行3、5:将字母存储在一个字符串中,并重复调用

  • 。简单地存储最后一个字符并添加新的变量作为计数器会更容易(也更快)。
  • 第8行:只要遇到重复的字符,就添加(正确的)编码字符串。但是,您从不更新a。因此,一旦您第一次到达此行代码,每个下一个字符都将添加相同的编码字符串。解决方案很简单,将第12行的缩进移动一次,这样在两种情况下都会分配新字符。
  • 第13行:输出时不添加最后一个字符。迭代编码对前一个字符起作用。这意味着字符串中的最后一个字符必须在循环结束后处理。

  • 和最后但同样重要的是,因为您使用/作为特殊字符,所以当它显示为不重复的代码时,您应该以某种方式处理它。例如,明文/12a将被编码为/12a,然后被解码为12 as的序列。

下面是一些(希望)工作示例:

代码语言:javascript
运行
复制
def encode (plainText):
    ## Lazy solution for some of the edge cases
    if plainText is None or len(plainText) == 0:
        return plainText

    ## We can join this together
    ## and have faster arbitrary 
    ## str length addition
    res=[]
    ## We only care about the last
    ## character, no need to save all
    prev_char=''
    ## And count it ourselves, its
    ## faster then calling count
    count=0
    for i in plainText:
        if i == prev_char:
            ## If its the same char
            ## increment count
            count += 1
            ## and then continue with next
            ## cycle. Avoid unneccasary indent.
            continue

        ## else
        if count > 2 or prev_char == '/':
            ## If more then 2 occurances
            ## we build the encoding.
            ## for 3 occurances the length is the same.
            ## '/' is a special character so we
            ## always encode it
            res.append(
                f"/{count}{prev_char}"
            )
        else:
            ## Otherwise just append the symbols
            res.append(prev_char*count)
        ## We observed at least 1 instance of i 
        count = 1
        ## Store for next comparison
        prev_char = i

    ## Now deal with last character.
    ## Without this your string would miss it.
    if count > 2 or prev_char == '/':
        res.append(
            f"/{count}{prev_char}"
        )
    else:
        res.append(prev_char*count)

    ## And build our string
    return ''.join(res)
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59084005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档