Could anyone help me identify the error? 1179 Vector Filling IV -URI

0

Sofolks,I'mstudyingPythonduringcollegeholidaysandIcameacrossthisprobleminURI.

I'm still a beginner in language, and programming in general. Could someone tell me why the platform does not accept my code? I tried to follow everything exactly, but I do not understand why the site does not accept the code when submitted. My code below:

pares= []
impares= []

for i in range(15):

    n= int(input())
    if n%2==0:
        pares.append(n)

    if n%2!=0:
        impares.append(n)

a=0 

for i in pares[0:5]:
    print('par[{}] = {}'.format(a, i))
    a+=1

b=0

for i in impares[0:5]:
    print('impar[{}] = {}'.format(b, i))
    b+=1

if len(impares)>5:

    c=0
    for i in impares[5:]:
        print('impar[{}] = {}'.format(c, i))
        c+=1

if len(pares)>5:

    d=0
    for i in pares[5:]:
        print('par[{}] = {}'.format(d, i))
        d+=1

If anyone can help me I'll be grateful.

    
asked by anonymous 24.12.2017 / 06:34

1 answer

0

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

    
12.11.2018 / 13:54