Could someone help me identify the error? 1179 Vector IV Completion -URI

Asked

Viewed 677 times

1

1179 URI

So guys, I’m studying Python during the college holidays and I came across this problem at the URI.

inserir a descrição da imagem aqui

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 to the letter, 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’d be grateful.

  • What do you mean "the platform does not accept my code" ? For the result is the same as in the picture!

  • When I submit to the platform, returns me the message "Wrong Answer (40%)". Yes, the result is the same as the image, and so I do not understand why the code is not accepted. I believe the logic is not wrong, and if you see anything to prove otherwise, please! rs feel free to tell me- then I’ll try other ways to get the same output requested in the problem.

1 answer

1

The question here relates to a detail in the wording, this:

However, the size of each of the two vectors is 5 positions. Then, each time one of the two vectors fills up, you must print out the whole vector and use it again for the next numbers that are read

Notice you’re not doing this. First you are saving everything and only at the end it shows in blocks of 5. Not to mention it shows from 0 to 5 and after 5 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 sees the error:

10
20
30
40
50
-10
-20
-30
-40
-50
12
14
16
18
20

Expected VS Result:

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

Note that the positions of the last 5 pairs are different, as the last 10 are written in one for.

The solution is even simpler than the code you have. Simply create a function to display 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")

Watch this solution run with the test I put up on Ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.