在Python中查找字符串中按数字顺序排列的最长字符串

在Python中查找字符串中按数字顺序排列的最长字符串,python,algorithm,python-2.7,Python,Algorithm,Python 2.7,我有一个包含数千个数字的字符串。我需要遍历字符串,找到按数字顺序排列的最长字符集。例如: string = '1223123341223455' 该字符串中以数字顺序排列的最长字符串为1223455,长度为7个字符。以下是我目前的一个例子: r=r2='' a=b=0 while a < len(string)+1: if string[a] <= string[b]: r += string[a] else: if len(r)

我有一个包含数千个数字的字符串。我需要遍历字符串,找到按数字顺序排列的最长字符集。例如:

string = '1223123341223455'
该字符串中以数字顺序排列的最长字符串为1223455,长度为7个字符。以下是我目前的一个例子:

r=r2=''
a=b=0
 while a < len(string)+1:
    if string[a] <= string[b]:
        r += string[a]
    else:
        if len(r) < len(r2):
            r = r2
    a += 1
    b += 1
r=r2=''
a=b=0
而a如果字符串[a]在0处索引字符串。因此,如果您尝试访问
some_str[len(some_str)]
您将得到一个
索引器,因为该字符串的最高索引是
len(some_str)-1
。将
while
条件更改为:
while a
。另外,您不应该使用
string
作为变量,因为它可能会掩盖python
string
模块名称。

字符串索引为0。因此,如果您尝试访问
some_str[len(some_str)]
您将得到一个
索引器,因为该字符串的最高索引是
len(some_str)-1
。将
while
条件更改为:
while a
。另外,您不应该使用
string
作为变量,因为它可能会掩盖python
string
模块名称。

问题是您将
a
增加了太多次。因此,当a等于字符串长度(
a=16
)时,程序将中断。将第三行更改为
,而
应该可以修复它

另外,我不太确定你在用变量做什么。您声明了从未使用过的r1,使用r2时没有声明它。这个问题比您的方法更容易解决-以下代码似乎可以满足您的要求:

>>> r=longest=''
>>> for a in range(1:len(string)):
        if (string[a-1] <= string[a]) or len(r)==0:
            r += string[a]
        else:
        r = string[a]       // We need to reset r if the string is not in numerical order
        if len(r) > len(longest):
            longest = r
        a += 1
>>> longest
'1223455'
>r=longest=''
>>>对于范围内的(1:len(字符串)):
如果(字符串[a-1]长度(最长):
最长=r
a+=1
>>>最长的
'1223455'

问题是您将
a
增加了太多次。因此,当a等于字符串长度(
a=16
)时,程序会中断。将第三行更改为
,而a
应该可以修复它

另外,我也不太确定你在用变量做什么。你声明了从未使用过的r1,而你使用了r2却没有声明它。这个问题比你的方法更容易解决-下面的代码似乎满足了你的要求:

>>> r=longest=''
>>> for a in range(1:len(string)):
        if (string[a-1] <= string[a]) or len(r)==0:
            r += string[a]
        else:
        r = string[a]       // We need to reset r if the string is not in numerical order
        if len(r) > len(longest):
            longest = r
        a += 1
>>> longest
'1223455'
>r=longest=''
>>>对于范围内的(1:len(字符串)):
如果(字符串[a-1]长度(最长):
最长=r
a+=1
>>>最长的
'1223455'

首先确保您有几件事情要测试,以及预期的结果,包括边界情况

strings = {
    '1223123341223455': '1223455',  # at the end
    '1': '1',                       # just one
    '12321': '123',                 # at the start
    '212321': '123',                # in the middle
    '': '',                         # empty
    '123234': '123',                # two of same length, take the first
    '12231233412234552': '1223455', # at the end -1 testing the try 
}

然后搜索最长的字符串,而不将到目前为止找到的实际字符追加到某个临时字符串。这是低效的。您只需要知道最长字符串的起始索引及其长度:

def longest(s):
    max_start = 0
    this_start = 0
    max_length_minus_one = 0
    for x in range(len(s)-1):
        if s[x] > s[x+1]:
            length_found = x - this_start
            if length_found > max_length_minus_one:
                max_length_minus_one = length_found
                max_start = this_start
            this_start = x + 1
    try:
        # test the final string position
        length_found = x + 1 - this_start
        if length_found > max_length_minus_one:
            max_length_minus_one = length_found
            max_start = this_start
    except UnboundLocalError:
        pass # empty string throws this exception
    return s[max_start:max_start+max_length_minus_one+1]
现在在测试用例上运行此命令并检查输出:

for s, check in strings.iteritems():
    res = longest(s)
    print repr(s), repr(res), 'OK' if res == check else '<<<<< ERROR'
对于s,签入strings.iteritems():
res=最长(s)

打印repr(s),repr(res),'OK'如果res==check else'首先确保您有几个东西要测试,以及预期的结果,包括边界情况

strings = {
    '1223123341223455': '1223455',  # at the end
    '1': '1',                       # just one
    '12321': '123',                 # at the start
    '212321': '123',                # in the middle
    '': '',                         # empty
    '123234': '123',                # two of same length, take the first
    '12231233412234552': '1223455', # at the end -1 testing the try 
}

然后搜索最长的字符串,而不将到目前为止找到的实际字符追加到某个临时字符串。这是低效的。您只需要知道最长字符串的起始索引及其长度:

def longest(s):
    max_start = 0
    this_start = 0
    max_length_minus_one = 0
    for x in range(len(s)-1):
        if s[x] > s[x+1]:
            length_found = x - this_start
            if length_found > max_length_minus_one:
                max_length_minus_one = length_found
                max_start = this_start
            this_start = x + 1
    try:
        # test the final string position
        length_found = x + 1 - this_start
        if length_found > max_length_minus_one:
            max_length_minus_one = length_found
            max_start = this_start
    except UnboundLocalError:
        pass # empty string throws this exception
    return s[max_start:max_start+max_length_minus_one+1]
现在在测试用例上运行此命令并检查输出:

for s, check in strings.iteritems():
    res = longest(s)
    print repr(s), repr(res), 'OK' if res == check else '<<<<< ERROR'
对于s,签入strings.iteritems():
res=最长(s)

print repr(s),repr(res),'OK'if res==check else'他不仅迭代了太多次…他还比较了相同的数字:)a=0和b=0,并且在末尾都递增。他不仅迭代了太多次…他还比较了相同的数字:)a=0和b=0,两者都在末尾递增。
字符串
模块现在很少使用,而且,无论如何,拥有该名称的变量不会阻止您从模块导入。只有对内置名称(如
str
)进行阴影处理时才会出现问题。
string
模块现在很少使用,而且,使用该名称的变量不会阻止您从模块导入。只有对内置名称(如
str
)进行阴影处理时,才会出现问题。