在给定字符串中按字母顺序查找最长的字母子串 [英] finding the longest substring of letters in alphabetical order in a given string
问题描述
当为给定的任务执行我的代码时,我不断得到最长的字符串加上迭代中的下一个字母.例如,如果我使用
s = 'azcbobobegghakl'
当答案应该是 "beggh"
时,我会得到 "beggha"
作为最长的字符串.我尝试过的所有随机字母字符串都会发生同样的错误.
我发现在result += letters"语句之后添加了额外的字母,但我不知道如何修复它.这是我的代码:
s = 'azcbobobegghakl'结果 = []最终 = []对于 s 中的字母:结果 += 字母如果 result == sorted(result) 和 len(result) >= len(final):最终=结果elif 结果 != 排序(结果):结果 = [结果[len(result)-1]]打印 "".join(final)
这里的问题是 result
和 final
指向同一个列表.您可能认为 +=
会在您发出 result += 字母
时创建一个新列表,但它不会:
但是,当您使用 x = x + [3]
时:
有关此行为的说明,请参阅此问题.当 letters
是字符串中的最后一个 a
字符时,这就是 for
循环(原始代码的)中发生的情况:>
- 开头,
final
和result
都指向['b', 'e', 'g', 'g', 'h']
. - after
result += 'a'
final
和result
都指向['b', 'e', 'g', 'g', 'h', 'a']
. - 现在输入了
elif
块,结果将指向一个新列表['a']
,而final
仍然指向 <代码>['b', 'e', 'g', 'g', 'h', 'a']. 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:
- at the beginning,
final
andresult
both point to['b', 'e', 'g', 'g', 'h']
. - after
result += 'a'
final
andresult
both point to['b', 'e', 'g', 'g', 'h', 'a']
. - now the
elif
block is entered and result will point to a new list['a']
, whilefinal
still points to['b', 'e', 'g', 'g', 'h', 'a']
. 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屋!