我正在尝试使用该测试来测试功能,但是它总是失败并显示以下内容:
[0, 0] should equal []
有人可以告诉我我的错误在哪里吗?
测试:
Test.describe("Basic tests")
Test.assert_equals(count_positives_sum_negatives([1, 2, 3, 4, 5, 6, 7,
8, 9, 10, -11, -12, -13, -14, -15]),[10,-65])
Test.assert_equals(count_positives_sum_negatives([0, 2, 3, 0, 5, 6, 7,
8, 9, 10, -11, -12, -13, -14]),[8,-50])
Test.assert_equals(count_positives_sum_negatives([1]),[1,0])
Test.assert_equals(count_positives_sum_negatives([-1]),[0,-1])
Test.assert_equals(count_positives_sum_negatives([0,0,0,0,0,0,0,0,0]),[0,0])
Test.assert_equals(count_positives_sum_negatives([]),[])
我的代码:
def count_positives_sum_negatives(arr):
positive_count = 0
negative_sum = 0
for n in arr:
if n > 0:
positive_count += 1
elif n < 0:
negative_sum += n
return [positive_count, negative_sum]
答案 0 :(得分:2)
您似乎还没有处理过简并的情况,因为输入列表为空,因此没有任何报告。在函数顶部添加一行:
if len(arr) == 0
return []
答案 1 :(得分:0)
如果测试失败,则需要执行以下过程: