【发布时间】:2019-04-27 16:34:29
【问题描述】:
我想用python找到最长的连续字母子串。
尝试
def f(word):
'''
Recall that if c is an ascii character then ord(c) returns its ascii code.
Will be tested on nonempty strings of lowercase letters only.
>>> f('x')
The longest substring of consecutive letters has a length of 1.
The leftmost such substring is x.
>>> f('xy')
The longest substring of consecutive letters has a length of 2.
The leftmost such substring is xy.
>>> f('ababcuvwaba')
The longest substring of consecutive letters has a length of 3.
The leftmost such substring is abc.
>>> f('abbcedffghiefghiaaabbcdefgg')
The longest substring of consecutive letters has a length of 6.
The leftmost such substring is bcdefg.
>>> f('abcabccdefcdefghacdef')
The longest substring of consecutive letters has a length of 6.
The leftmost such substring is cdefgh.
'''
desired_length = 0
desired_substring = ''
print(f'The longest substring of consecutive letters has a length of {desired_length}.')
print(f'The leftmost such substring is {desired_substring}.')
if __name__ == '__main__':
import doctest
doctest.testmod()
我该如何解决这个问题?
【问题讨论】:
-
将您的标题和描述更改为
longest ALPHABETICAL substring -
"Attempt" .. 我明白你在那里做了什么。
标签: python regex python-3.x