拉取max lengh子字符串,使用ASCII按字母顺序排序

2024-05-03 22:56:29 发布

您现在位置:Python中文网/ 问答频道 /正文

请帮我解决这个问题。我是一个初学者,这个练习让我发疯,因为我尝试切片,现在ASCII,这对我来说似乎更容易? 只有当字符大于字符串中的前一个字符时,我才设法提取该字符的ord值。如何从字母数组中获取最大排序的子字符串,或者至少从它们的ord值中获取?(代码应拉入'fruw',因为它是最大排序的)

s = 'gasgrerupxkgfruwgohfzl'
s2 = []
print s
for char in range(0, len(s)):
        if ord(s[char]) >= ord(s[char-1]):
            s2.append(str(ord(s[char])))
print s2

我的想法是正确的吗?我应该做哪些修改?谢谢


Tags: 字符串代码排序字母ascii切片数组字符
3条回答

这里有一个函数可以做同样的事情,对于任何需要使用你的代码的人(包括你自己)来说,阅读和理解它的作用要简单得多。

def find_lss(s):
    if not s:
        return s # empty s or None
    solutions = []
    current = None
    for char in s:
        if current is None or current[-1] > char:
            if current is not None: solutions.append(current)
            current = char
        else:
            current += char
    solutions.append(current)
    return max(solutions, key=len)

请注意,不需要使用ord(),因为直接比较字符对您的问题非常有效。

然后您可以将其用于您的示例:

^{pr2}$

更重要的是,此实现还可以处理经典的角点情况,即空字符串:

>>> find_lss('')
''

它甚至可以与None配合使用:

^{4}$

(无输出,无错误;即函数返回None。)

我已经尽可能的修改了你的代码

s = 'gasgrerupxkgfruwgohfzl'
s2 = ""                                      # a temporary string to hold value
temp = []                                    # added an extra array
print s
if s:
    for char in range(0, len(s)-1):
            if ord(s[char+1]) >= ord(s[char]):   # same as your code
                s2+=s[char]
            else:                                # if some letter is smaller, then add the previously longest str to temp array
                s2+=s[char]
                temp.append(s2)
                s2 = ""
    s2+=s[char+1]                                # As pointed out, the last letter would not have been detected as we loop up to len -1, thus we need to add this
    temp.append(s2)
    print max(temp,key = len)                    # print the longest string
else:
    print s

评论试图解释这些变化

我们先用索引的方法来完成:

def solve(s):
    max_g = [] # this will store the final answer
    curr_g = [s[0]] # this list will store the contents of current group, start with first item. 
    for i in xrange(1, len(s)):
        c = s[i]
        # Now if the current character is greater than or equal to
        # last item in curr_g then simply append the current item to
        # curr_g
        # else compare the length of current_group with max_g
        # if it's greater than length of max_g then we have found a
        # new bigger group, so time to update value of max_g
        # and lastly update curr_g to [c]
        if c >= curr_g[-1]:
            curr_g.append(c)
        else:
            if len(curr_g) > len(max_g):
                max_g = curr_g
            curr_g = [c]

    #One last check of the group
    if curr_g and len(curr_g) > len(max_g):
        return ''.join(curr_g)
    else:
        return ''.join(max_g)

s = 'gasgrerupxkgfruwgohfzl'
print solve(s)
# fruw

使用生成器函数和max的更好方法。这一次只能在内存中存储一个组:

^{pr2}$

用法:

print max(groups(s), key=len)
# fruw

更新:

当然,如果您想处理空字符串,那么在函数的顶部添加一个简单的if条件。由于您没有详细说明如何处理重复字符(事实上,您的答案中也使用了>=),但是从您的comment来判断,您想要的是一个简单的>

相关问题 更多 >