代码之家  ›  专栏  ›  技术社区  ›  Yevhen_Radchenko

为[0,0]和返回空数组时失败

  •  -3
  • Yevhen_Radchenko  · 技术社区  · 7 年前

    我试着用那个测试来测试功能,顺便说一句,它总是失败的:

    [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]
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   Prune    7 年前

    if len(arr) == 0
        return []
    
        2
  •  0
  •   Thomas Weller    7 年前