首页 > 试题广场 >

使用任意您熟悉的语言(CC++、Java、Python或者

[问答题]

使用任意您熟悉的语言(C/C++JavaPython或者其他),实现以下功能:

在字符串中找出连续最长的递增英文字母串(26个英文字母,不区分大小写),把这个字母串的长度作为函数值返回。

如字符串“dgefgfg455cd556bcd45”,可以得出最长的连续递增字母是efg、bcd,所以返回3。
def test_func(s):
    s = s.lower()
    ans = 0
    word = ''
    for char in s[1:]:
        if not char.isalpha():
            word = ''
            continue
        if word and word[-1] < char:
            word += char
        else:
            word = char
        ans = max(ans, len(word))
    return ans

发表于 2020-10-17 23:32:11 回复(0)
  1. testStr=input() ##输入一个字符串
  2. ls=[] ##定义一个空列表用于将非字母的部分转换为'.',然后存到这个列表中
  3. numLs=[] ##用于存放连续字母的长度的空列表
  4. for i in range(len(testStr)): ## 遍历输入字符串的索引
  5. if testStr[i].isalpha(): ## 如果字符是字母
  6. ls.append(testStr[i]) ## 将其添加到列表ls中
  7. else: ## 否则
  8. ls.append('.') ## 添加一个'.'到列表中
  9. tempStr = ''.join(ls).lower() ## 将列表ls的值聚合成一个字符串
  10. tempLs = tempStr.split('.') ## 将此字符串以'.'进行分割并添加到列表tempLs中
  11. tempLs = [i for i in tempLs if i != ''] ## 过滤出不为空的值到列表tempLs中
  12. for i in tempLs: ## 遍历列表tempLs
  13. count = 1 ## 定义一个计数器
  14. for j in range(len(i)-1): ## 遍历列表中的字符串的索引值减1
  15. if ord(i[j])+1 == ord(i[j+1]): ## 如果该字母的ASCII码等于后一个字母的ASCII码加1,也就是前一个字母跟后一个字母是否连续
  16. count += 1 ## 计数器加1
  17. numLs.append(count) ## 将计数器的值添加的列表numLs中
  18. else: ## 否则
  19. count = 1 ## 计数器恢复为1
  20. print(max(numLs)) ## 输出最终结果
发表于 2021-01-21 19:41:27 回复(0)
test_str="dgefgfg455cd556bcd45"
list_str=[]
curr=[]
for i in range(len(test_str)):
    if test_str[i].isalpha():
         if ord(test_str[i])+1==ord(test_str[i+1]):
             if i<len(test_str)-1:
                 curr.append(test_str[i])
             else:
                 curr.append(test_str[i]+test_str[i+1])
         else:
             curr.append(test_str[i])
             list_str.append(curr)
             curr=[]
print(list_str)
print(max(len(n) for n in list_str))

发表于 2019-08-05 21:45:54 回复(1)
aa = "dgefgfg455cd556bcd45"
cc=''
dd=0
for i in range(1,len(aa)):
    if aa[i].isalpha() and (ord(aa[i])+1==ord(aa[i+1]) or ord(aa[i])-1==ord(aa[i-1])):
        cc+= aa[i] if ord(aa[i]) > dd else ';'+aa[i]
        dd=ord(aa[i])
print(cc.split(';'))
print(max([len(u) for u in cc.split(' ')]))
发表于 2019-07-11 00:16:20 回复(0)
str = "dgefgfg455cd556bcd45"
for i in range(len(str1)): if i< len(str1)-1: if ord(str1[i])+1 ==ord(str1[i+1]):
            n = n +1  list2.append(i) else:
            n=0  list1.append(n)

print(max(list1))


发表于 2019-04-28 20:19:57 回复(0)