按字母顺序找到最长的子字符串。我的代码有什么问题

时间:2018-07-06 21:22:42

标签: python python-3.x alphabetical longest-substring

到目前为止,我已经知道了:

s = 'azcbobobegghakl'
i = 0
j = 1 

temp = ''  #temporary variable I use to change longest
longest = ''   
for b in range(len(s)):
    if s[i] <= s[j]: #checks if it's in alphabetical order
        temp+=s[i] 
        if len(longest) < len(temp):
           longest  = temp       
    else: 
        temp = ''    #reset temp after every substring  
    if len(longest) < len(temp):
        longest  += temp      #update longest if the new substring is longer
    i +=1
    if j < len(s) - 1 : # because otherwise i get an IndexError
       j +=1


print('Longest substring in alphabetical order is:', longest)

现在我的问题是,我总是得到正确的字符串,但没有最后一个字母。 (例如,这里不是“ beggh”,而是“ begg”)。

我也必须这样做,如果是平局,则第一个算为最长(如果s = abcbcd,则abc应该是最长的)

最后,我知道这个问题已经在这里被问过几次了,但是我想尽可能地解决我想出的代码,而不仅仅是完全使用别人的代码。

5 个答案:

答案 0 :(得分:2)

您正在将s[i]s[i+1]进行比较,但仅将s[i]添加到temp中。相反,总是以temp中的当前字母开头,并在比较后将s[i+1]添加到temp

s = 'azcbobobegghakl'
i = 0

temp = s[0]   #temporary variable I use to change longest
longest = ''   
for i in range(len(s)-1):
    if s[i] <= s[i+1]:  #checks if it's in alphabetical order
        temp+=s[i+1]
        if len(longest) < len(temp):
           longest  = temp       
    else: 
        temp = s[i+1]     #reset temp after every substring
    if len(longest) < len(temp):
        longest  += temp    #update longest if the new substring is longer
    i +=1



print('Longest substring in alphabetical order is:', longest)

# Longest substring in alphabetical order is: beggh

答案 1 :(得分:1)

如果您发现下一个字符不是按字母顺序排列,则不会考虑当前字符,这就是为什么最后缺少一个字符的原因。

相反,如果下一个字符顺序不正确,则应始终先处理当前字符,然后再重置temp

s = 'azcbobobegghakl'
i = 0

temp = ''   #temporary variable I use to change longest
longest = ''   
for i in range(len(s)-1):
    temp += s[i]
    if len(longest) < len(temp):
        longest = temp
    if s[i] > s[i+1]:  #checks if it's in alphabetical order
        temp = ''     #reset temp after every substring
    if len(longest) < len(temp):
        longest  += temp    #update longest if the new substring is longer  
    i +=1



print('Longest substring in alphabetical order is:', longest)

这将输出:

Longest substring in alphabetical order is: beggh

答案 2 :(得分:0)

s = 'azcbobobegghakl'
i = 0
j = 1 

temp = ''  #temporary variable I use to change longest
longest = '' 


for b in range(0, len(s) -1):
    if s[b] <= s[b + 1]: #checks if it's in alphabetical order
        if len(temp) == 0:
            temp+= s[b]
            temp+= s[b+1]
        else:
            temp+= s[b+1]

        if len(longest) < len(temp):
           longest  = temp       
    else: 
        temp = ''    #reset temp after every substring  
    if len(longest) < len(temp):
        longest  += temp      #update longest if the new substring is longer
    i +=1
    if j < len(s) - 1 : # because otherwise i get an IndexError
       j +=1


print('Longest substring in alphabetical order is:', longest)

答案 3 :(得分:0)

尽管我在其他答案中提到了如何修复您的代码,但我也想说,直接的方法是使用itertools.accumulate

list(accumulate(s, lambda s, c: s+c if s[-1]<=c else c) )
# ['a', 'az', 'c', 'b', 'bo', 'b', 'bo', 'b', 'be', 'beg', 'begg', 'beggh', 'a', 'ak', 'akl']

因此,按字母顺序查找最长子串的方法是

sorted(accumulate(s, lambda s, c: s+c if s[-1]<=c else c), key=len)[-1]
# 'beggh'

答案 4 :(得分:0)

要在最后解决问题,您需要在代码到达最终索引时进行一些中断。此外,如果您到达字符串的末尾并且仍然按字母顺序排列(请参阅下文),则需要添加一些代码以使循环复位。最后,对于最长字符串只有一个字符的情况,您需要在s [0}处为longest_word和word定义起始变量。只要坚持下去,最终您就会得到它。 pythontutor.com是调试和故障排除的绝佳工具。

s = 'abcdefghijklmnopqrstuvwxyz'
a = 0
b = 1
longest_word = s[0]
word = s[0]
for i in range(len(s)):
    a = 0
    b = 1
    if i == len(s) - 2 or b > len(s)-1:
        i += 1
        print('Longest substring in alphabetical order is:', longest_word)
        break
    while (b < len(s)-i) and (s[i+a] <= s[i+b]):
        word = s[i:i+b+1]
        a += 1
        b += 1
    if len(word) > len(longest_word):
        longest_word = s[i:i+b]
    else: 
        i += 1