Python 在哪些测试用例中,这可能无法给出正确的输出,哪些是潜在的解决方案?
我需要得到一个整数列表,然后乘以它的元素,这样我就得到了可能的最大数。这个数字,我需要输出为字符串。 例如: 这是我的密码:Python 在哪些测试用例中,这可能无法给出正确的输出,哪些是潜在的解决方案?,python,python-2.7,testcase,Python,Python 2.7,Testcase,我需要得到一个整数列表,然后乘以它的元素,这样我就得到了可能的最大数。这个数字,我需要输出为字符串。 例如: 这是我的密码: 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) c
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)))
导入操作符
xs=[0,0]
如果xs.count(0)=len(xs):
打印(“0”)
elif xs.count(0)=len(xs)-1:
打印(“0”)
其他:
否定=[]
合法性=[]
listLength=len(xs)
计数器=0
而计数器<列表长度:
如果xs[计数器]为0:
合法附加(xs[计数器])
计数器=计数器+1
如果len(否定)%2=0:
负片[负片.索引(最大值([n表示负片中的n,如果你的当前代码没有导致[0,5]
被报告为0
而不是5
)?列表[-1]
将被报告为1
而不是-1
@BillLynch噢,你绝对正确,我不能相信我完全忽略了这一点:(@BillLynch I显式测试[-1];报告为0我们还可以使用for
循环,而不是while
循环。
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)))