Py学习  »  Python

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

kuro-san • 4 年前 • 797 次点击  

使用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
 
797 次点击  
文章 [ 4 ]  |  最新文章 4 年前
blhsing
Reply   •   1 楼
blhsing    5 年前

如果当前字符比最后一个字符大一个序数,则可以迭代字符串并与最后一个字符进行比较,并附加到可能最长的字符串:

def longest_substring(s):
    last = None
    current = longest = ''
    for c in s:
        if not last or ord(c) - ord(last) == 1:
            current += c
        else:
            if len(current) > len(longest):
                longest = current
            current = c
        last = c
    if len(current) > len(longest):
        longest = current
    return longest

以便:

print(longest_substring('asdefvbrrfqrstuvwxffvd'))

将输出:

qrstuvwx
Onyambu
Reply   •   2 楼
Onyambu    5 年前
def sub_strings(string):
    substring = ''
    string +='\n'
    i = 0
    string_dict ={}
    while i < len(string)-1:
        substring += string[i]
        if ord(substring[-1])+1 != ord(string[i+1]):
            string_dict[substring] = len(substring)
            substring = ''
        i+=1
    return string_dict   

 s='abcbcd'

 sub_strings(s)
  {'abc': 3, 'bcd': 3} 

找到你能做的最长的 max(sub_strings(s))

那么,你想把哪一个作为最长的呢??现在这是你需要解决的问题

Jonas
Reply   •   3 楼
Jonas    5 年前
s = 'abcbcdiawuhdawpdijsamksndaadhlmwmdnaowdihasoooandalw'
longest = ''
current = ''
for idx, item in enumerate(s):
    if idx == 0 or item > s[idx-1]:
        current = current + item
    if idx > 0 and item <= s[idx-1]:
        current = ''
    if len(current)>len(longest):
        longest = current
print(longest)

输出:

dhlmw

为了你的理解 'b'>'a' True , 'a'>'b' False

编辑:

对于最长连续子串:

s = 'asdefvbrrfqrstuvwxffvd'
abc = 'abcdefghijklmnopqrstuvwxyz'
longest = ''
current = ''
for idx, item in enumerate(s):
    if idx == 0 or abc.index(item) - abc.index(s[idx-1]) == 1:
        current = current + item
    else:
        current = item
    if len(current)>len(longest):
        longest = current
print(longest)

输出:

qrstuvwx
Poolka
Reply   •   4 楼
Poolka    5 年前

要避免无限循环,请添加 x += 1 在你的while循环的最后。因此,您的代码工作正常,但在一般情况下工作错误。

它工作错误的原因是您只使用一个变量 g 存储结果。使用至少两个变量比较以前找到的子字符串和新找到的子字符串,或使用list记住所有子字符串,然后选择最长的子字符串。