在哪些测试案例中,这可能无法提供正确的输出以及可能的解决方案是什么?

时间:2017-01-29 05:47:36

标签: python python-2.7 testcase

我需要获取一个整数列表并将其元素相乘,以便获得最大数量。这个号码,我需要输出一个字符串。 例如:

(int list) givenList = [2, 0, 2, 2, 0]
Output: 8  
//because (2)*(2)*(2)=8

(int list) givenList= [-2, -3, 4, -5]
Output:60  
//because (-3)*(-5)*(4)=60

这是我的代码:

import operator
xs = [0,0]
if  xs.count(0) == len(xs) :
    print ("0")
elif xs.count(0) == len(xs)-1:
    print ("0")
else:
    negatives =[]
    legits = []
    listLength=len(xs)
    counter = 0
    while counter < listLength:
        if xs[counter] <0:
            negatives.append(xs[counter])
        if xs[counter] > 0:
            legits.append(xs[counter])
        counter = counter +1

    if len(negatives)%2 !=0:
        negatives[negatives.index(max([n for n in negatives if n<0]))] = 1

    newList = negatives + legits
    print str((reduce(operator.mul, newList)))

0 个答案:

没有答案