The question here is a detail in the statement, this:
Only the size of each of the two vectors is 5 positions. So each time one of the two vectors fill up, you should print the entire vector and use it again for the next numbers you read
Note that you are not doing this. First is to save everything and only in the end is it shows in blocks of 5. Not to mention that shows from 0 to 5 and then 5 to forward, which means that if you receive 15 pairs as input shows from 0 to 5 and after 5 to 15 what is wrong.
You can test with this entry that you see the error:
10
20
30
40
50
-10
-20
-30
-40
-50
12
14
16
18
20
Expected VS score:
par[0] = 10 VS par[0] = 10
par[1] = 20 VS par[1] = 20
par[2] = 30 VS par[2] = 30
par[3] = 40 VS par[3] = 40
par[4] = 50 VS par[4] = 50
par[0] = -10 VS par[0] = -10
par[1] = -20 VS par[1] = -20
par[2] = -30 VS par[2] = -30
par[3] = -40 VS par[3] = -40
par[4] = -50 VS par[4] = -50
par[5] = 12 VS par[0] = 12
par[6] = 14 VS par[1] = 14
par[7] = 16 VS par[2] = 16
par[8] = 18 VS par[3] = 18
par[9] = 20 VS par[4] = 20
Notice that the positions of the last 5 pairs are different, since the last 10 pairs are written in a single for
.
The solution is even simpler than the code it has. Just create a function to show the array with a certain text, the par
or impar
, and each time it reaches 5 elements you show and clear the array:
def mostra_array(array, texto):
for posicao, num in enumerate(array):
print("{}[{}] = {}".format(texto, posicao, num))
pares= []
impares= []
for i in range(15):
n= int(input())
if n%2==0:
pares.append(n)
if len(pares) == 5: # mal chega aos 5 mostra e limpa
mostra_array(pares, "par")
pares = []
if n%2!=0:
impares.append(n)
if len(impares) == 5: # mal chega aos 5 mostra e limpa
mostra_array(impares, "impar")
impares = []
# no fim os que sobraram são escritos, começando pelo impar
mostra_array(impares, "impar")
mostra_array(pares, "par")
See this solution running with the test I put up on Ideone