代码之家  ›  专栏  ›  技术社区  ›  exploitr

按字母顺序排列的最长子字符串提供意外输出

  •  3
  • exploitr  · 技术社区  · 6 年前

    我的问题和这个封闭的问题有些关系- Longest substring in alphabetical order ,但不正确。

    下面是我的python代码,用于打印 String s

    s = 'azcbobobegghakl'
    chow = ''
    stuff = []
    for e in s:
        if(s.find(e) < (len(s)-1)):     #fix indexoutofrange
           if(e <= s[s.find(e) + 1]):   #just checking the next char
               chow = chow + e          #sum up
           else:
               stuff.append(chow)
               chow = ''                # reset chow
        else:
            stuff.append(chow)          #edit
    
    if(len(stuff)==1):
        print(stuff[0])
    elif(len(stuff)==0):
        print('')
    else:
        print(max(stuff,key=len))
    

    我知道有人会在StackOverflow找到更好的代码。但是,我的问题是为什么我没有得到预期的代码行为?


    • “阿兹博博博格哈克尔” 乞讨 ' | “beggh”是对的
    • 'abcbcd'' ' |
    • 'afzeolnfabcdefooda'' fabcdef公司 ' | “abcdefoo”是对的

    我能看到的是,最后一个字符有时没有添加,有时,第一个字符是错误的。

    请具体回答错误区域并说明原因,而不是修复代码。

    3 回复  |  直到 6 年前
        1
  •  2
  •   AER    6 年前

    与另一个答案一样,当出现异常时也需要追加。但是,find函数只查找该字母的第一个实例。因此,您应该丢弃已经处理过的字符串(比用计数器单调地跟踪它更容易)。

    s = 'azcbobobegghakl'
    s_working = s
    chow = ''
    stuff = []
    for e in s:
        if(s_working.find(e) < (len(s_working)-1)):     #fix indexoutofrange
           if(e <= s_working[s_working.find(e) + 1]):   #just checking the next char
               chow = chow + e          #sum up
           else:
               chow = chow + e #[FIX]Add the last char before append to list
               stuff.append(chow)
               chow = ''                # reset chow
        s_working = s_working[1:]       # discards the already processed string
        else:
            chow = chow + e
            stuff.append(chow)
    
    print(max(stuff,key=len))
    

    测试了所有的例子,它的作品。现在调整为注释示例,并捕获最后一个字母(如果包括在内)。

        2
  •  3
  •   kaan bobac    6 年前

    对于最后一个字符,我认为问题是在将字符串追加到列表之前没有添加最后一个字符。此问题可以通过以下修复程序解决:

    s = 'azcbobobegghakl'
    chow = ''
    stuff = []
    for e in s:
        if(s.find(e) < (len(s)-1)):     #fix indexoutofrange
           if(e <= s[s.find(e) + 1]):   #just checking the next char
               chow = chow + e          #sum up
           else:
               chow = chow + e #[FIX]Add the last char before append to list
               stuff.append(chow)
               chow = ''                # reset chow
    
    print(max(stuff,key=len))
    
        3
  •  1
  •   Melih Taşdizen    6 年前

    也许这不是你想要的答案,但我不久前写了smiler代码。它可以找到按字母顺序连续的字符组。找到最长的一个可以很容易地实现。

    base_string = "afzeolnfabcdefoooda"
    tmp = []
    _group = []
    
    for _index, _char in enumerate(base_string):
        try:
            _next_char =  base_string[_index + 1]
    
            if  _char <= _next_char:
                # add chars to list if they are alphabetically ordered
                _group.append(_char)
            else:
                # if they are not, add the char the current group, add group to list, and create an empty one
                _group.append(_char)
                tmp.append(_group)
                _group = []
    
        except IndexError:
            # end of the string there is no _next_char,
            # add the _char to current group and `break` the loop
            _group.append(_char)
            tmp.append(_group)
            break
    

    结果:

    [['a', 'f', 'z'], ['e', 'o'], ['l', 'n'], ['f'], ['a', 'b', 'c', 'd', 'e', 'f', 'o', 'o', 'o'], ['d'], ['a']]