大神!!求用python写一个程序,找出给定字符串s中按字母表顺序排列的最...
发布网友
发布时间:2022-04-23 16:38
我来回答
共3个回答
热心网友
时间:2022-04-18 07:09
from string import lowercase
s = 'babcbcasdfweljlkjlefghisfsdfsd'
cont = []
sub = []
for i in s:
if len(sub) >= 1 and lowercase.index(sub[-1]) + 1 != lowercase.index(i):
cont.append(''.join(sub))
sub = []
sub.append(i)
cont = sorted(cont, key = len, reverse=True)
print cont[0]
望采纳!
http://www.pythoner.cn/labs/在线运行连python内置函数都用不了,还是不要在这上面学习了,自己本地安装一个python环境也不费劲。
热心网友
时间:2022-04-18 08:27
import re
def fun(s):
r = 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?'
m = re.findall(r, s, re.I)
m = sorted(m, key = len, reverse=True)
return m[0]
print(fun('abcbcd'))
热心网友
时间:2022-04-18 10:02
p = 0
ans = ''
while True :
ans1 = ''
com = ''
for i in (s[p:]):
if str(i) > str(com):
com = i
ans1 = ans1 + i
else:
p = p + 1
break
if len(ans)< len(ans1):
ans = ans1
elif p+1 == len(s):
break
print('Longest substring in alphabetical order is:'+ str(ans))