小编Jea*_*amp的帖子

Python 单词计数器

我正在学校学习 Python 2.7 课程,他们告诉我们创建以下程序:

假设 s 是一串小写字符。

编写一个程序,打印 s 中字母按字母顺序出现的最长子串。

例如,如果 s = azcbobobegghakl ,那么您的程序应该打印

按字母顺序排列的最长子串是:beggh

如果是平局,则打印第一个子字符串。

例如,如果 s = 'abbcbcd',那么你的程序应该打印

按字母顺序排列的最长子串是:abc

我写了以下代码:

s = 'czqriqfsqteavw'

string = ''

tempIndex = 0
prev = ''
curr = ''

index = 0
while index < len(s):
    curr = s[index]
    if index != 0:
        if curr < prev:
            if len(s[tempIndex:index]) > len(string):
               string = s[tempIndex:index]
            tempIndex=index
        elif index == len(s)-1:
            if len(s[tempIndex:index]) > len(string):
               string = s[tempIndex:index+1]
    prev = curr
    index += …
Run Code Online (Sandbox Code Playgroud)

python string python-2.7

5
推荐指数
1
解决办法
1601
查看次数

标签 统计

python ×1

python-2.7 ×1

string ×1