【问题标题】:extract sub sequence Python提取子序列 Python
【发布时间】:2017-04-10 03:41:23
【问题描述】:

问题:

给定一个数字序列,提取最大长度的升序子序列。

示例输入:L = [1,3,5,6,1,5,1,6,7]

输出:[1,3,5,6]

代码:

def Sequence(integers):


sequence = []
i = 0
stored = []
#newseq = []

for i in range(len (integers)-1) :

    if integers[i] <= integers[i+1]:   #i less than i+1 append to sequence
        stored.append(integers[i])
        sequence.append(integers[i])

    else:
        if integers[i] >= integers[i+1]:

            del sequence[:]

    if len(stored) > (len(sequence)):
        print('biggest subseq =',stored)
        print('small sub',sequence)

print (stored,sequence)

Sequence([1,2,3,4,5,1,2,4,5])

错误:

正在输出[1, 2, 3, 4, 1, 2, 4] [1, 2, 4]

但它应该输出:[[1, 2, 3, 4,5] [1, 2, 4, 5]

我该如何解决这个问题?

【问题讨论】:

    标签: list python-3.5 subsequence


    【解决方案1】:

    一旦这个工作,我可以显示最大的子序列。有什么想法吗?

    我的想法是行不通,你需要重写很多次。

    您首先循环遍历数字 (for i in ...),这很好,第一个 if 测试获得了一系列不断增加的数字,到目前为止还可以。但随后您将数字添加到storedsequence。为什么要向两者添加相同的内容?

    如果序列停止增加,else 就会触发。这很酷,您可以按照递增的顺序并注意它何时结束。但是您不相信这一点,并且您对另一个 if 再次进行了完全相同的测试,因为...原因?在那里,您删除sequence。 “我会跟踪所有的序列,当它们结束时,我会把它们扔掉而不使用它们,因为扔掉我正在寻找的东西就好了:)”。

    好的,从你给出的名字来看,我猜sequence 应该是“我正在处理的当前序列”。

    在那些if 测试之后,对于每个循环迭代,您检查len(stored)stored 永远不会被清除或重置,因此它只会构建原始列表中的几乎每个数字。一旦你做了那个长度测试......你什么都不做,除了打印东西。

    您打印的内容:print('biggest subseq = ', sequence) - 使名称 sequence 看起来应该是“最大”,但与您之前使用它的方式相比,这是错误的。 sequence 不是最大的,是当前的,对吧?还是不对? “我将使用无用的名称,因为我不喜欢输入长名称。为什么我的代码不起作用?”。是的,我也经常这样做。

    然后你打印出stored 是'小子'?什么是小潜艇?不管它应该是什么,stored 在这一点上没有任何有用的东西。

    而且,您跟踪号码 i &gt;= i+1 并仅在匹配时将 i 添加到序列中的方式,意味着您总是会错过每个序列中的最后一个数字。 (“下一个较小,所以我将跳过添加这个”)。

    更糟糕的是,您跟踪 range( len(integers) - 1) 的方式意味着您永远不会将原始列表中的最后一个数字检查到最终子序列中。

    是的,一个简单的修复方法对您的代码不起作用。它沿着正确的路线寻找一个可行的答案,但它完全没有做正确的事情来真正做到这一点。


    我认为您正在尝试做的是“跟踪直到序列结束并存储它。然后,找到下一个序列。如果它比上一个存储的序列长,则存储新的序列”。所以:

    1. 给自己起一个清晰的变量名,解释它们的用途。

    2. stored 中,您应该将其设置为您找到的整个序列,而不是像您看到的那样添加单个数字。

    3. 这需要发生在序列结束的地方,而不是输入列表中的每个数字。

    4. 这意味着stored 的更新需要在if len(stored) &gt; len(sequence) 内部进行。

    5. .. 需要以另一种方式测试 - 是新的比存储的长。

    6. 它需要采取行动来更新商店。

    试着写出尽可能接近你的代码,得到这个:

    def Sequence(integers):
    
      longest_sequence = []
      current_sequence = []
    
      for i in range( len(integers) ):
    
        if i < len(integers) - 1 and integers[i] <= integers[i+1]:   # sequence continues
          current_sequence.append(integers[i])
          print('current_sequence ', current_sequence)
    
        else:                           # else sequence, or input, ends now
          current_sequence.append(integers[i])   # catch this last number in sequence, too
          print('\nsubseq ended ', current_sequence)
    
          # now we've hit the end of a subsequence
          # do we replace the stored one, or not?
          if len(current_sequence) > len(longest_sequence):
            print('\nreplacing previous longest ', longest_sequence)
    
            longest_sequence = current_sequence
    
          # either way, reset the current sequence tracker
          current_sequence = []
    
      print()
      print ('Finished. Longest found: ', longest_sequence)
    
    
    Sequence([1,2,3,4,5,1,2,4,5])
    print('\n----\n')
    Sequence([1,2,4,5,1,2,3,4,5])
    

    你可以run online at repl.it here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 2015-08-25
      相关资源
      最近更新 更多