Почему значение в списке [0, 0] == []?
def count_positives_sum_negatives(arr):
list1 = []
list2 = []
for i in arr:
if i > 0:
list1.append(i)
else:
list2.append(i)
list3 = list([len(list1), sum(list2)])
if list3:
return list3
Валидатор не засчитывает решение, ошибкой считает [0, 0] should equal [] и наоборот [] should equal [0, 0]. Как решить эту проблему?
Given an array of integers.
Return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers. 0 is neither positive nor negative. If the input is an empty array or is null, return an empty array.
Example: For input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15], you should return [10, -65].
Ответы (1 шт):
Вы нарушили условие. Написано же: "If the input is an empty array or is null, return an empty array." Т.е. при [] на входе, он же и на выходе должен быть. Проверьте код ниже на валидаторе, я добавил условие проверки (при None вообще ваш код давал ошибку).
def count_positives_sum_negatives(arr):
if arr == [] or arr == None:
return []
list1 = []
list2 = []
for i in arr:
if i > 0:
list1.append(i)
else:
list2.append(i)
list3 = list([len(list1), sum(list2)])
if list3:
return list3