简体   繁体   English

我哪里错了?

[英]Where am I wrong?

Question Write function mssl() (minimum sum sub-list) that takes as input a list of integers.It then computes and returns the sum of the maximum sum sub-list of the input list.问题编写函数 mssl()(最小总和子列表),将整数列表作为输入。然后计算并返回输入列表的最大总和子列表的总和。 The maximum sum sub-list is a sub-list (slice) of the input list whose sum of entries is largest.最大和子列表是输入列表的条目总和最大的子列表(切片)。 The empty sub-list is defined to have sum 0. For example, the maximum sum sub-list of the list [4, -2, -8, 5, -2, 7, 7, 2, -6, 5] is [5, -2, 7, 7, 2] and the sum of its entries is 19. l = [4, -2, -8, 5, -2, 7, 7, 2, -6, 5] mssl(l) 19 mssl([3,4,5]) 12 mssl([-2,-3,-5]) 0 In the last example, the maximum sum sub-list is the empty sub-list because all list items are negative.空子表被定义为总和为0。例如,列表[4, -2, -8, 5, -2, 7, 7, 2, -6, 5] 的最大和子表为[5, -2, 7, 7, 2] 其项的总和为 19. l = [4, -2, -8, 5, -2, 7, 7, 2, -6, 5] mssl( l) 19 mssl([3,4,5]) 12 mssl([-2,-3,-5]) 0 在最后一个例子中,最大和子列表是空子列表,因为所有列表项都是消极的。 THIS IS MY SOLUTION这是我的解决方案

 def mssl(lst):
    pos,neg,TotalList=[],[],[]
    for items in range(len(lst)):
         if(lst[items]>0):
            pos+=[lst[items]]
         else:
             neg+=[lst[items]]
    TotalPos=sum(pos)
    TotalNeg=sum(neg)

    if(len(neg)>0):
        for negatives in range(len(neg)):
         TotalList=[TotalPos+neg[negatives]]
        if(TotalList>TotalList[negatives-1]):
               print(TotalList)
    else:
        TotalList=TotalPos
    print(TotalList)

THIS IS NOT A HOMEWORK QUESTION I AM LEARNING PYTHON FOR FUN, PLEASE LET ME KNOW WHERE I AM WRONG这不是一个家庭作业问题我学习 Python 是为了好玩,请让我知道我哪里错了

It looks like you're trying to learn programming, with python as your first language.看起来您正在尝试学习编程,将 Python 作为您的第一语言。 This particular problem is a somewhat difficult one to start with.这个特殊问题是一个有点难以开始的问题。 I would advise you to take a straightforward, brute-force approach at first.我建议你一开始就采取直接的、蛮力的方法。 Evaluate the sums of all the subsequences, one after another, and keep track of which is largest.一个接一个地计算所有子序列的总和,并跟踪哪个最大。 Once you have a function that will produce the correct answer, you can look for a better (faster, more elegant, whatever) solution.一旦你有了一个能产生正确答案的函数,你就可以寻找更好(更快、更优雅,无论如何)的解决方案。

As to your code, it really has nothing to do with the question.至于你的代码,它真的与问题无关。 For example, TotalList is always a one-element list.例如,TotalList 始终是一个单元素列表。 The expression TotalList[negatives-1] doesn't make much sense;表达式TotalList[negatives-1]没有多大意义; if there's only one element in the list, you can access it as TotalList[0] .如果列表中只有一个元素,您可以将其作为TotalList[0]访问。 The expression TotalList>TotalList[negatives-1] makes no sense at all;表达式TotalList>TotalList[negatives-1]完全没有意义; you don't want to compare a list to a number.您不想将列表与数字进行比较。

This is a well-known problem, and a simple, fast solution is not at all easy to come up with, so don't be discouraged if you don't find it.这是一个众所周知的问题,一个简单、快速的解决方案并不容易想出,所以如果你没有找到它,不要气馁。 Once you get a straightforward solution, you can think about an elegant one.一旦你得到一个简单的解决方案,你就可以考虑一个优雅的解决方案。 This problem can be solved in one line of python, using list comprehensions.这个问题可以在一行 Python 中解决,使用列表推导式。 Trying to do that will lead to improvement in your python style.尝试这样做将导致您的python 风格的改进。 For example, instead of writing例如,而不是写

 for items in range(len(lst)):
         if(lst[items]>0):
            pos+=[lst[items]]
         else:
             neg+=[lst[items]] 

you can, and should write你可以,而且应该写

pos = [x for x in lst if x > 0]
neg = [x for x in lst if x < 0]

Good luck learning python.祝你学习python好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM