【问题标题】:Making a program to find what the longest substring in alphabetical order制作程序以按字母顺序查找最长的子字符串
【发布时间】:2020-09-26 01:44:14
【问题描述】:

我想编写一个程序,从字符串 s 中按字母顺序打印最长的子字符串,但我不知道我做错了什么,我几天前才开始编码,这是我做过的最好的想办法。

s = 'vvrsmxxlplnawxxcmcvuxrgi'

alphabet =['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
Current_Sequence=''
Max_Sequence=''
runs=0
current_runs = ''
max_runs=''
for I in str(s)[1:]:
    runs +=1
    if alphabet.index(I) >= alphabet.index(s[runs-1]):
        Current_Sequence += str(I)
        current_runs += str(runs)
    else:
        if (len(str(Current_Sequence))) > (len(str(Max_Sequence))) :
            Max_Sequence = str(Current_Sequence)
            Current_Sequence=''
            max_runs= int(str(current_runs)[0])
            current_runs=''
        else: 
            Current_Sequence=''
            current_runs=''

if (len(Max_Sequence)) >= (len(Current_Sequence)):
    print('Longest substring in alphabetical order is: ' + s[max_runs-1] + str(Max_Sequence))

if (len(Current_Sequence)) > (len(Max_Sequence)):
    print('Longest substring in alphabetical order is: ' + s[current_runs[0]-1] + str(Current_Sequence))  

* 错误:预期为“awxx”,得到“vwxx”。 *

在另一个试验中,s = 'zyxwvutsrqponmlkjihgfedcba' 结果是 * 错误:应为“z”,得到“不支持的操作数类型”:“str”和“int”。 *

任何意见都会有所帮助,因为我还很年轻,对代码还很陌生

【问题讨论】:

  • 您好,欢迎来到 StackOverflow!您的代码没有按预期工作吗?如果没有,您能否编辑您的帖子并描述它在做什么?
  • 欢迎您!你能提供一些期望结果的例子吗?
  • 请阅读:stackoverflow.com/help/minimal-reproducible-example - 您的意见是什么?你有:for I in str(s)[1:]:,但我没有看到任何地方定义了s。你得到什么输出?你期待什么输出?展示一些测试用例,这将是一个很好的起点......
  • 正如@DillonMiller 提到的。您能否提供一些示例输入值和您期望的结果。请解释您尝试过的代码有什么问题。如果您收到错误消息,请在您的问题中也发布它。您提供给我们的信息越多,获得好的答案的机会就越大
  • 我只是按照建议做了,并在输入中添加了字符串s、预期输出和实际输出

标签: python helper edx


【解决方案1】:

你可以使用zip将字符串偏移一,累加子字符串,选择最长的。

s = 'vvrsmxxlplnawxxcmcvuxrgi'

li = []
st = ""
for c1,c2 in zip(s,s[1:]):
    if c2 >= c1:
        st += c1
    elif st != "":
        li.append(st+c1)
        st =""

print(max(li, key = len))

输出:

awxx

【讨论】:

    【解决方案2】:

    你不必改变太多。

    只有几行:

    你想从第一个字符开始

    for I in str(s)[0:]:
    

    序列重新启动后,您还希望将当前字母(打破链)添加到新序列中。因此你不再需要 max_runs

    Current_Sequence=I
    

    将运行 +=1 移动到 for 循环的末尾。

    从打印中删除 max_runs

    print('Longest substring in alphabetical order is: ' + str(Max_Sequence))
    

    完整代码:

    s = 'kjddbyydx'
    alphabet =['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    Current_Sequence=''
    Max_Sequence=''
    runs=0
    for I in str(s)[0:]:
        if alphabet.index(I) >= alphabet.index(s[runs-1]):
            Current_Sequence += str(I)
        else:
            if (len(str(Current_Sequence))) > (len(str(Max_Sequence))) :
                Max_Sequence = str(Current_Sequence)
                Current_Sequence=I
            else:
                Current_Sequence = I
        runs +=1
    
    if (len(Max_Sequence)) >= (len(Current_Sequence)):
        print('Longest substring in alphabetical order is: ' + str(Max_Sequence))
    
    if (len(Current_Sequence)) > (len(Max_Sequence)):
        print('Longest substring in alphabetical order is: ' + str(Current_Sequence))
    

    使用枚举代替 for 循环的代码:

    for runs, I in enumerate(s):
        if alphabet.index(I) >= alphabet.index(s[runs-1]):
            Current_Sequence += str(I)
        else:
            if (len(str(Current_Sequence))) > (len(str(Max_Sequence))) :
                Max_Sequence = str(Current_Sequence)
                Current_Sequence=I
            else:
                Current_Sequence = I
    
    if (len(Max_Sequence)) >= (len(Current_Sequence)):
        print('Longest substring in alphabetical order is: ' + str(Max_Sequence))
    
    if (len(Current_Sequence)) > (len(Max_Sequence)):
        print('Longest substring in alphabetical order is: ' + str(Current_Sequence))
    

    【讨论】:

    • 你能告诉我如何将它应用到我的程序中吗?我还是个新手,还有很多东西要学。
    • 你也可以去掉 current_run 。刚刚更新了代码,不需要了。
    • 对我有用,它给出了正确的输出。但是我再次更改了代码。我猜现在应该按照你的意愿去做。
    • 没关系。你的调整就像一个魅力。你能解释一下你改变了什么以及为什么它是错误的。感谢您的帮助。
    • 不知道了。但我想给你一个建议。您使用了 for 循环并在内部使用“运行”来存储您当前查看的字符的索引。尝试使用枚举,如下所示:对于运行,我在枚举中:
    【解决方案3】:

    你的程序看起来不错。也许这会帮助你找出问题所在

        def max_substr(s):
            max_s = s[0:1]
            cur_s = max_s
    
            for i in range(1, len(s)):
                ch = s[i : i + 1]
                if ch >= cur_s[-1]:
                    cur_s = cur_s + ch
                    if len(cur_s) > len(max_s):
                        max_s = cur_s
                else:
                    cur_s = ch
    
            return max_s
    
    
        print(max_substr("vvrsmxxlplnawxxcmcvuxrgi"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 1970-01-01
      相关资源
      最近更新 更多