Python 首次编程练习

Python 首次编程练习,python,string,substring,Python,String,Substring,有史以来第一次编程。。。我正试图做这个练习来 编写一个程序,打印字母按字母顺序排列的s的最长子字符串。例如,如果s='azcbobobegbegghakl',那么您的程序应该打印 按字母顺序排列的最长子字符串是:beggh 我在这里…在开始发疯之前: s = 'abcdezcbobobegghakl' n = len(s) x = 0 x += 1 lengh = s[x-1] if s[x] >= s[x-1]: lengh = lengh + s[x] if s[x+

有史以来第一次编程。。。我正试图做这个练习来

编写一个程序,打印字母按字母顺序排列的s的最长子字符串。例如,如果s='azcbobobegbegghakl',那么您的程序应该打印

按字母顺序排列的最长子字符串是:beggh

我在这里…在开始发疯之前:

s = 'abcdezcbobobegghakl'
n = len(s) 
x = 0


x += 1
lengh = s[x-1]
if s[x] >= s[x-1]:
    lengh = lengh + s[x]


if s[x+1] < s[x]:
    n = len(lengh)
if x > n:
    break 

print('Longest substring in alphabetical order is: ' + str(lengh)) 
s='abcdezcbobobeghakl'
n=长(s)
x=0
x+=1
长度=s[x-1]
如果s[x]>=s[x-1]:
长度=长度+s[x]
如果s[x+1]n:
打破
print('按字母顺序排列的最长子字符串为:'+str(长度))

我知道这段代码很糟糕。我正在尝试按字母顺序查找子字符串,以某种方式保留最长的子字符串!我知道这可能很正常,因为我以前从未编程过,但我感到非常沮丧……有什么好主意/帮助吗???

首先尝试将问题分解为小问题(不要优化!直到问题解决为止),如果您已经了解了函数,那么它们是将执行流分解为可读且可理解的代码段的好方法

一个开始的例子是:

def get_sequence_size(my_string, start):
   # Your code here
   return size_of_sequence

current_position = 0
while current_position < len(my_string):
   # Your code here using get_sequence_size() function
def get_sequence_size(我的字符串,开始):
#你的代码在这里
返回\u序列的大小\u
当前位置=0
当前位置
def find_longest_substr(my_str):
#字符串变量来保存结果
res=“”
#最长子字符串的候选项
候选者=“”
#对于字符串中的每个字符
对于my_str中的字符:
#如果候选字符为空,只需向其添加第一个字符
如果不是候选人:
候选字符+=字符
#如果候选字符中的最后一个字符“低于”当前字符,则将字符添加到候选字符中
elif候选人[-1]len(res):
res=候选人
候选字符
#重置候选字符并向其添加当前字符
其他:
候选字符
#最后一个候选项是最长的更新结果
如果len(候选人)>len(res):
res=候选人
返回res
def main():
str1=“azcbobegbegghaklbeggh”
最长=查找最长子字符串(str1)
打印最长
如果名称=“\uuuuu main\uuuuuuuu”:
main()

以下代码使用
reduce
方法解决问题:

solution = ''

def check(substr, char):
    global solution
    last_char = substr[-1]
    substr = (substr + char) if char >= last_char else char
    if len(substr) > len(solution):
        solution = substr
    return substr

def get_largest(s):
    global solution
    solution = ''
    reduce(check, list(s))
    return solution

这些都是假设您有一个字符串,并且需要按字母顺序查找最长的子字符串

方案A

test = s[0]      # seed with first letter in string s
best = ''        # empty var for keeping track of longest sequence  

for n in range(1, len(s)):    # have s[0] so compare to s[1]
    if len(test) > len(best):
        best = test
    if s[n] >= s[n-1]:
        test = test + s[n]    # add s[1] to s[0] if greater or equal
    else:                     # if not, do one of these options 
        test = s[n]

print "Longest substring in alphabetical order is:", best
方案B

maxSub, currentSub, previousChar = '', '', ''
for char in s:
    if char >= previousChar:
        currentSub = currentSub + char
        if len(currentSub) > len(maxSub):
            maxSub = currentSub
    else: currentSub = char
    previousChar = char
print maxSub
备选案文C

matches = []
current = [s[0]]
for index, character in enumerate(s[1:]):
    if character >= s[index]: current.append(character)
    else:
        matches.append(current)
        current = [character]
print "".join(max(matches, key=len))
方案D

def longest_ascending(s):
    matches = []
    current = [s[0]]
    for index, character in enumerate(s[1:]):
        if character >= s[index]:
            current.append(character)
        else:
            matches.append(current)
            current = [character]
    matches.append(current)
    return "".join(max(matches, key=len))
print(longest_ascending(s))
def最长:
buff=“”
最长=“”
s+=chr(255)
对于范围内的i(透镜-1):
buff+=s[i]
如果不是s[i]len(最长):
最长=浅黄色
buff=“”
如果len(buff)>len(最长):
最长=浅黄色
回程最长

您使用什么编程语言?我认不出来。你用什么语言编程?你学过循环吗?他们会在这方面有所帮助。我有一种感觉,我不会用这种方法去任何地方。有什么想法吗?两天内三次问同一个问题@尼科:帮你自己一个忙,好好学习这门语言:)整洁的代码。但是,当最长的子字符串位于字符串的开头或结尾时,它不会返回正确的结果。另外,使用
[-1://code>进行切片有点多余,您可以简单地使用
[-1]
。正确。很好的观察。我一路都没想过。只是想给尼科一个好的开始。感谢像你这样的人,我们让这个社区可靠。
def longest_ascending(s):
    matches = []
    current = [s[0]]
    for index, character in enumerate(s[1:]):
        if character >= s[index]:
            current.append(character)
        else:
            matches.append(current)
            current = [character]
    matches.append(current)
    return "".join(max(matches, key=len))
print(longest_ascending(s))
def longest(s):
    buff = ''
    longest = ''

    s += chr(255)
    for i in range(len(s)-1):
        buff += s[i]
        if not s[i] < s[i+1]:
            if len(buff) > len(longest):
                longest = buff
            buff = ''
    if len(buff) > len(longest):
        longest = buff

    return longest