我是靠谱客的博主 平淡蜜粉,最近开发中收集的这篇文章主要介绍Python练习题答案: 最长的字母串【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战答案1:答案2:答案3:答案4:答案5:答案6:答案7:答案8:答案9:答案10:,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

最长的字母串【难度:2级】:

答案1:

import re

reg = re.compile('a*b*c*d*e*f*g*h*i*j*k*l*m*n*o*p*q*r*s*t*u*v*w*x*y*z*')

def longest(s):
    return max(reg.findall(s), key=len)

答案2:

def longest(string):
    start = 0
    longest = string[0:1]
    length = len(string)
    for i in range(1, length):
        if string[i] < string[i - 1] or i == length - 1:
            if string[i] < string[i - 1]:
                last = string[start:i]
                start = i
            else:
                last = string[start:]
            if len(last) > len(longest):
                longest = last
    return longest​

答案3:

import re

def longest(s):
    regex = 'a*b*c*d*e*f*g*h*i*j*k*l*m*n*o*p*q*r*s*t*u*v*w*x*y*z*'
    group = lambda x: x.group()
    return max(map(group, re.finditer(regex, s)), key=len)

答案4:

def longest(s):
    max_substring = ""
    substring = ""
    for ch in s:
        if substring == "" or ord(ch) >= ord(substring[-1]):
            substring += ch
        elif ord(ch) < ord(substring[-1]):
            substring = "".join(ch)
        if len(substring) > len(max_substring):
            max_substring = substring
    return max_substring​

答案5:

longest=lambda s: max(''.join(c+' ' if c>d else c for c,d in (zip(s,s[1:]+' '))).split(), key=len)

答案6:

def longest(s):
    chunks = []
    for c in s:
        if chunks and chunks[-1][-1] <= c:
            chunks[-1] += c
        else:
            chunks.append(c)
    return max(chunks, key=len)

答案7:

def longest(s):
    substr = s[0]
    longest = s[0]
    for c in s[1:]:
        if substr[-1] <= c:
            substr += c
        else:
            if len(substr) > len(longest):
                longest = substr
            substr = c
    return max(longest, substr, key=len)

答案8:

import re
def longest(s):
    return max(re.findall(r'a*b*c*d*e*f*g*h*i*j*k*l*m*n*o*p*q*r*s*t*u*v*w*x*y*z*',s),key=len)

答案9:

def longest(s):
    substr = '' 
    for letter in range(len(s)):
        count = letter
        while (s[count] <= s[count+1:count+2]):
            count += 1
        if len(s[letter:count+1]) > len(substr):
            substr = s[letter:count+1]
    return substr​

答案10:

### MIT 6.001x FTW!!!!
def longest(s):
    current = longest_string = s[0]
    for i in range(1, len(s)):
        if s[i] >= current[-1]:
            current+= s[i]
            if len(current) > len(longest_string):
                longest_string = current
        else:
            current = s[i]
    return longest_string​




Python基础训练营景越Python基础训练营QQ群

在这里插入图片描述
欢迎各位同学加群讨论,一起学习,共同成长!

最后

以上就是平淡蜜粉为你收集整理的Python练习题答案: 最长的字母串【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战答案1:答案2:答案3:答案4:答案5:答案6:答案7:答案8:答案9:答案10:的全部内容,希望文章能够帮你解决Python练习题答案: 最长的字母串【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战答案1:答案2:答案3:答案4:答案5:答案6:答案7:答案8:答案9:答案10:所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部