Print
Category: Series
Hits: 491

Series25. An integer N and a set of N integers containing at least two zeros are given. Output the sum of numbers from the given set, located between the first and last zero (if the first and last zeros are consecutive, then output 0).

Solution in Python 3:

import random

N = random.randrange(5,15)
Z = random.randrange(2,6)

L1 = [random.randrange(1,5) for i in range(0,N)]
L2 = [0 for i in range(0,Z)]
L = L1 + L2

print(L1)
print(L2)
random.shuffle(L)
print(L)

s = 0
Sum = 0
flag = True
for x in L:
if x == 0:
if flag:
flag = False
else:
Sum += s
s = 0
else:
s += x

print("Sum = ",Sum)