149 lines
4.4 KiB
Python
149 lines
4.4 KiB
Python
import random
|
||
|
||
|
||
# Посчитать сумму всех положительных чисел
|
||
def sum_positive(array):
|
||
q = 0
|
||
N = len(array)
|
||
for i in range(N):
|
||
if array[i] > 0:
|
||
q += array[i]
|
||
|
||
return q
|
||
|
||
|
||
# Посчитать сумму всех отрицательных чисел
|
||
def sum_negative(array):
|
||
q = 0
|
||
N = len(array)
|
||
for i in range(N):
|
||
if array[i] < 0:
|
||
q += array[i]
|
||
|
||
return q
|
||
|
||
|
||
# Посчитать сумму всех положительных чисел, если в массиве есть отрицательное число
|
||
# Если отрицательных чисел нет, вернуть 0
|
||
def sum_positive_if_negative(array):
|
||
q = 0
|
||
N = len(array)
|
||
for i in range(N):
|
||
if array[i] > 0:
|
||
q += array[i]
|
||
if array[i] < 0:
|
||
return 0
|
||
|
||
return q
|
||
|
||
|
||
# Посчитать сумму всех отрицательных чисел, если в массиве есть число 1
|
||
# Если числа 1 нет - посчитать сумму всех положительных чисел
|
||
def sum_negative_if_one(array):
|
||
q = int()
|
||
a = int()
|
||
for i in array:
|
||
if i == 1:
|
||
q += 1
|
||
if q > 0:
|
||
break
|
||
if q > 0:
|
||
for i in array:
|
||
if i < 0:
|
||
a += i
|
||
else:
|
||
for i in array:
|
||
if i > 0:
|
||
a += i
|
||
return a
|
||
|
||
|
||
|
||
# Посчитать сумму чисел ДО первого отрицательного числа (не включая его)
|
||
def sum_before_negative(array):
|
||
q = -1
|
||
a = int()
|
||
for i in array:
|
||
q += 1
|
||
if i < 0:
|
||
break
|
||
if q >= 0:
|
||
for i in array[:q]:
|
||
a += i
|
||
return a
|
||
|
||
|
||
# Посчитать сумму чисел ПОСЛЕ первого положительного числа (включая его)
|
||
def sum_after_positive(array):
|
||
q = -1
|
||
a = int()
|
||
for i in array:
|
||
q += 1
|
||
if i > 0:
|
||
break
|
||
if q >= 0:
|
||
for i in array[q:]:
|
||
a += i
|
||
return a
|
||
|
||
|
||
#################################################
|
||
def verifier(test_name, test_array, actual, expected):
|
||
print(test_name)
|
||
print(test_array)
|
||
if actual != expected:
|
||
print("=======> FAILED! <=======")
|
||
print(f"Got value <{actual}>")
|
||
print(f"Expected value is <{expected}>")
|
||
else:
|
||
print("PASSED")
|
||
print()
|
||
|
||
|
||
test_array10 = []
|
||
test_array100 = []
|
||
test_array1000 = []
|
||
|
||
for i in range(10):
|
||
test_array10.append(random.randint(0, 20) - 10)
|
||
test_array100.append(random.randint(0, 200) - 100)
|
||
test_array1000.append(random.randint(0, 2000) - 1000)
|
||
|
||
tests = [test_array10, test_array100, test_array1000, [], [0]]
|
||
|
||
|
||
for i in range(len(tests)):
|
||
verifier(f"sum_positive TEST {i+1}", tests[i], sum_positive(tests[i]), sum([x for x in tests[i] if x > 0]))
|
||
|
||
for i in range(len(tests)):
|
||
verifier(f"sum_negative TEST {i+1}", tests[i], sum_negative(tests[i]), sum([x for x in tests[i] if x < 0]))
|
||
|
||
for i in range(len(tests)):
|
||
is_in = len([x for x in tests[i] if x < 0]) > 0
|
||
if is_in:
|
||
verifier(f"sum_positive_if_negative TEST {i+1}", tests[i], sum_positive_if_negative(tests[i]), sum([x for x in tests[i] if x > 0]))
|
||
else:
|
||
verifier(f"sum_positive_if_negative TEST {i+1}", tests[i], sum_positive_if_negative(tests[i]), 0)
|
||
|
||
for i in range(len(tests)):
|
||
is_in = len([x for x in tests[i] if x == 1]) > 0
|
||
if is_in:
|
||
verifier(f"sum_negative_if_one TEST {i+1}", tests[i], sum_negative_if_one(tests[i]), sum([x for x in tests[i] if x < 0]))
|
||
else:
|
||
verifier(f"sum_negative_if_one TEST {i+1}", tests[i], sum_negative_if_one(tests[i]), sum([x for x in tests[i] if x > 0]))
|
||
|
||
for i in range(len(tests)):
|
||
indices = [x for x in range(len(tests[i])) if tests[i][x] < 0]
|
||
if len(indices) > 0:
|
||
verifier(f"sum_before_negative TEST {i+1}", tests[i], sum_before_negative(tests[i]), sum(tests[i][:indices[0]]))
|
||
else:
|
||
verifier(f"sum_before_negative TEST {i+1}", tests[i], sum_before_negative(tests[i]), sum(tests[i]))
|
||
|
||
for i in range(len(tests)):
|
||
indices = [x for x in range(len(tests[i])) if tests[i][x] > 0]
|
||
if len(indices) > 0:
|
||
verifier(f"sum_after_positive TEST {i+1}", tests[i], sum_after_positive(tests[i]), sum(tests[i][indices[0]:]))
|
||
else:
|
||
verifier(f"sum_after_positive TEST {i+1}", tests[i], sum_after_positive(tests[i]), 0)
|
||
|