避免RLE算法中的Python一对一错误

编辑:看来,这不仅仅是一个一次性的错误。

在以下简单算法中,我遇到了一个错误,该算法应该显示沿run-length encoding行的字符串中的字母数。

我可以看到为什么不将最后一个字符添加到结果字符串中,但是如果我增加range中的i则得到index out of range的原因显而易见。

我想从算法设计的角度来了解概念上的问题,以及使我的代码正常工作。

我需要一些特殊情况的代码来处理原始字符串中的最后一项吗?还是将当前字符与previous字符进行比较也许更有意义,尽管这在算法开始时就产生了问题?

是否有一种通用的方法来将当前元素与上一个/下一个元素进行比较,从而避免索引超出范围的问题?

def encode(text):
    # stores output string
    encoding = ""
    i = 0

    while i < len(text) - 1:
        # count occurrences of character at index i
        count = 1
        while text[i] == text[i + 1]:
            count += 1
            i += 1

        # append current character and its count to the result
        encoding += text[i] + str(count) 
        i += 1

    return encoding

text = "Hello World"
print(encode(text))
# Gives H1e1l2o1 1W1o1r1l1
za80967190 回答:避免RLE算法中的Python一对一错误

是的,如果外部循环中的最后一个字符与上一个字符不同(在您的情况下为while i < len(text)),则外部循环应使用d

您的算法在全局上是好的,但是当寻找最后一个字符时,它将崩溃。此时,text[i+1]变为非法。

要解决此问题,只需在内部循环中添加安全检查:while i+1 < len(text)

def encode(text):
    # stores output string
    encoding = ""
    i = 0

    while i < len(text):
        # count occurrences of character at index i
        count = 1
        # FIX: check that we did not reach the end of the string 
        # while looking for occurences
        while i+1 < len(text) and text[i] == text[i + 1]:
            count += 1
            i += 1

        # append current character and its count to the result
        encoding += text[i] + str(count) 
        i += 1

    return encoding

text = "Hello World"
print(encode(text))
# Gives H1e1l2o1 1W1o1r1l1d1
,

如果您保留自己的策略,则必须检查i+1 < len(text)。 这给出了类似的内容:

def encode(text): 
    L = len(text) 
    start = 0 
    encoding = '' 
    while start < L: 
        c = text[start] 
        stop = start + 1 
        while stop < L and text[stop] == c: 
            stop += 1 
        encoding += c + str(stop - start) 
        start = stop 
    return encoding

做事的另一种方法是记住每次运行的开始:

def encode2(text): 
     start = 0 
     encoding = '' 
     for i,c in enumerate(text): 
         if c != text[start]: 
             encoding += text[start] + str(i-start) 
             start = i
     if text:
         encoding += text[start] + str(len(text)-start) 
     return encoding

这使您可以枚举感觉更pythonic的输入。

本文链接:https://www.f2er.com/3165528.html

大家都在问