第一次编程。。。我试着做这个练习是为了。。公司名称:
编写一个程序,打印s中字母按字母顺序出现的最长子串。例如,如果s='azcbobebeghakl',那么您的程序应该打印
按字母顺序最长的子串是: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))
我知道这段代码不好…我正在尝试按字母顺序查找子字符串,并以某种方式保留最长的子字符串!我知道这可能是正常的,因为我以前从未编程,但我感到非常沮丧…有什么好主意/帮助??在
以下代码使用
reduce
方法解决了该问题:首先试着把你的问题分解成小问题(不要优化!如果您了解了函数,它们是将执行流分解为可读和可理解片段的好方法。在
例如:
相关问题 更多 >
编程相关推荐