Write a program that reads a series of lines, each of them containing a sequence of non negative integers, and prints as output the value of the sum of the first line that contains an even number of elements ending in 3. If there is no such line, the output will be -1.
For instance, for the input:
23 12 4 25
44 43 23 3 12 8
33 4 9 73 14
88 92 55
The output would be 133, because the third line contains 2 numbers ending in 3 (33 and 73), and the previous lines contained an odd number of such elements (1 and 3 respectively). Thus, the third line is the first with an even number of elements ending in 3. The sum of the element of this line is 33+4+9+73+14 = 133
So, in conclusion:
- Input is a series of lines with numbers. Each line contains at least one number.
- Output is the sum of the elements in the first line that happens to have an even number of elements ending in 3, or -1 if there is no such line.
My try far now is:
import sys
line = sys.stdin.read().split()
# even number of elements ending in 3
for i in range(len(line)): # Make str input int
line[i] = int(line[i])
print(line)
With this all integer input in different lines is stored in a single list. But how I am able to distinguish which integers are from line 1? or 2? Solution needs to be with the import sys parameter. My question is about how to treat the input. How can I store values from each line in a list for each?
UPDATE: I was trying some things, not the result for the question but maybe clears what I want.
import sys
line = sys.stdin.readline().split() # ['23', '12', '4', '25']
while line != '':
lista_numb = []
for i in line:
if i.endswith('3'):
lista_numb.append(i)
print(lista_numb)
line = sys.stdin.readline().split()
if len(lista_numb)%2 == 0: # even
print(sum(line))