Py学习  »  Python

我想创建一个程序,使用python和basics按字母顺序查找最长的子字符串

kuro-san • 5 年前 • 1677 次点击  

使用python按字母顺序查找最长子字符串的代码

我所说的按字母顺序排列的最长子字符串是什么意思? 如果输入为“asdefvbrfqrstuvwxffvd”,则输出为“qrstuvwx”。

#we well use the strings as arrays so don't be confused
s='abcbcd'
#give spaces which will be our deadlines
h=s+'    (many spaces)                                                                      '
#creat outputs
g=''
g2=''
#list of alphapets
abc='abcdefghijklmnopqrstuvwxyz'

#create the location of x"the character the we examine"  and its limit 
limit=len(s)
#start from 1 becouse we substract one in the rest of the code
x=1
while (x<limit):
    #y is the curser that we will move the abc array on it
    y=0
    #putting our break condition first
    if ((h[x]==' ') or (h[x-1]==' ')):
        break
    for y in range(0,26):
        #for the second character x=1
        if ((h[x]==abc[y]) and (h[x-1]==abc[y-1]) and (x==1)):
            g=g+abc[y-1]+abc[y]

            x+=1
        #for the third to the last character x>1
        if ((h[x]==abc[y]) and (h[x-1]==abc[y-1]) and (x!=1)):
            g=g+abc[y]
            x+=1
        if (h[x]==' '):
            break
print ("Longest substring in alphabetical order is:" +g )

它不会结束,就像是在无限循环中 我该怎么办? 我是初学者,所以我想要一些for循环,而不是库中的函数 提前谢谢

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38077
 
1677 次点击  
文章 [ 4 ]  |  最新文章 5 年前