在给定字符串中按字母顺序查找最长的字母子串 [英] finding the longest substring of letters in alphabetical order in a given string

查看:52
本文介绍了在给定字符串中按字母顺序查找最长的字母子串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当为给定的任务执行我的代码时,我不断得到最长的字符串加上迭代中的下一个字母.例如,如果我使用

s = 'azcbobobegghakl'

当答案应该是 "beggh" 时,我会得到 "beggha" 作为最长的字符串.我尝试过的所有随机字母字符串都会发生同样的错误.

我发现在result += letters"语句之后添加了额外的字母,但我不知道如何修复它.这是我的代码:

s = 'azcbobobegghakl'结果 = []最终 = []对于 s 中的字母:结果 += 字母如果 result == sorted(result) 和 len(result) >= len(final):最终=结果elif 结果 != 排序(结果):结果 = [结果[len(result)-1]]打印 "".join(final)

解决方案

这里的问题是 resultfinal 指向同一个列表.您可能认为 += 会在您发出 result += 字母 时创建一个新列表,但它不会:

<预><代码>>>>x = [1,2]>>>y = x>>>x += [3]>>>X[1, 2, 3]>>>是[1, 2, 3]>>>x 是 y真的

但是,当您使用 x = x + [3] 时:

<预><代码>>>>x = [1,2]>>>y = x>>>x = x + [3]>>>X[1, 2, 3]>>>是[1, 2]>>>x 是 y错误的

有关此行为的说明,请参阅问题.当 letters 是字符串中的最后一个 a 字符时,这就是 for 循环(原始代码的)中发生的情况:

  1. 开头,finalresult 都指向 ['b', 'e', 'g', 'g', 'h'].
  2. after result += 'a' finalresult 都指向 ['b', 'e', 'g', 'g', 'h', 'a'].
  3. 现在输入了 elif 块,结果将指向一个新列表 ['a'],而 final 仍然指向 <代码>['b', 'e', 'g', 'g', 'h', 'a'].
  4. final 在此之后将不再更新

因此,您的原始代码(在编辑之前)可以通过更改来修复

result += 字母

result = result + [字母]:

s = 'azcbobobegghakl'结果 = []最终 = []对于 s 中的字母:结果 = 结果 + [字母]如果 result == sorted(result) 和 len(result) >= len(final):最终=结果elif 结果 != 排序(结果):结果 = [结果[len(result)-1]]打印(最终)

When executing my code for the given task, I keep getting the longest string plus the next letter in the iteration. For example, if I use

s = 'azcbobobegghakl' 

I will get "beggha" as the longest string, when the answer is supposed to be "beggh". This same bug occurs for all strings of random letters I tried.

I have figured out that the extra letter gets added on after the "result += letters" statement, but I'm not sure how to fix it. Here is my code:

s = 'azcbobobegghakl'
result = []
final = []
for letters in s:
    result += letters
    if result == sorted(result) and len(result) >= len(final):
        final=result
    elif result != sorted(result): 
        result = [result[len(result)-1]]
print "".join(final)

解决方案

The problem here is that result and final point to the same list. You are probably thinking that += will create a new list when you issue result += letters, but it won't:

>>> x = [1,2]
>>> y = x
>>> x += [3]
>>> x
[1, 2, 3]
>>> y
[1, 2, 3]
>>> x is y
True

However, when you use x = x + [3]:

>>> x = [1,2]
>>> y = x
>>> x = x + [3]
>>> x
[1, 2, 3]
>>> y
[1, 2]
>>> x is y
False

For an explanation of this behavior, see this question. This is what's happening in your for loop (edit: of your original code) when letters is the last a character in your string:

  1. at the beginning, final and result both point to ['b', 'e', 'g', 'g', 'h'].
  2. after result += 'a' final and result both point to ['b', 'e', 'g', 'g', 'h', 'a'].
  3. now the elif block is entered and result will point to a new list ['a'], while final still points to ['b', 'e', 'g', 'g', 'h', 'a'].
  4. final will never be updated again after this

Hence, your original code (before you edited it) can be fixed by changing

result += letters

to

result = result + [letters]:

s = 'azcbobobegghakl'
result = []
final = []
for letters in s:
    result = result + [letters]        
    if result == sorted(result) and len(result) >= len(final):
        final=result            
    elif result != sorted(result):
        result = [result[len(result)-1]]        

print(final)

这篇关于在给定字符串中按字母顺序查找最长的字母子串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆