here is the task

Help to understand where the error is:

a = [] N = 2015 for i in range(0, N): a.append(int(input())) b = -1 for i in range(1, N-1): if (b == -1 or a[i] < b) and (a[i] < a[i - 1]) and (a[i] < a[i + 1]): b = a[i] if b == -1: print('0') else: print(b) 

Closed due to the fact that off-topic participants Sergey Gornostaev , 0xdb , Suvitruf , Grundy , Kirill Stoianov August 20 '18 at 13:06 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Sergey Gornostaev, 0xdb, Suvitruf, Grundy
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What's the mistake? - gil9red

3 answers 3

Line 4:

 a.append( int( input() ) ) # Здесь вы заполняете значение массива с ключём [0]. То есть a[0]. 

Line 6:

 for i in range(1, N-1): # Здесь вы запускаете цикл, в котором при первой итерации i будет равно 1. 

Line 7:

 if (b == -1 or a[i] < b) and (a[i] < a[i - 1]) and (a[i] < a[i + 1]): # Здесь вы используете массив `a`. Конкретно: его значение под ключём i. i = 1, # но у вас заполнен только нулевой ключ массива. (Строка 4) 

This is what causes your error: IndexError: list index out of range

  • There is a question, why then everything works in the program commentator? - Ivan
  • @ Ivan In the last topic I gave you advice about the fact that you first need to enter an array, and then process it. It is still relevant. - MBo
  • @ Ivan, Question, how does range (0, N-1) differ from range (1, N-1) ?)) - Don2Quixote
  • No, apparently, you did not understand my question. I enter the commentator program below, and it’s written in the same line as the array as I have, but its program works, although the entire array is not entered - Ivan
  • @ArtyomZinovyev, if you change the ange (0, N-1) in its program to range (1, N-1), then nothing changes - Ivan
 a = [] minimum = -1 N = 2015 for i in range(0, N): a.append(int(input())) for i in range(0, N-1): if a[i] < a[i-1] and a[i] < a[i + 1] and (a[i] < minimum or minimum == -1): minimum = a[i] print(minimum if minimum != -1 else 0) 

    The mistake is that you start traversing an array to calculate a hole, after each input of a new element. Including after entering the first one, when a [i - 1]) or a [i + 1] simply cannot exist. If you remove the external data entry loop and substitute the test array a = [4, 9, 2, 17, 3, 8] and N = 6 into the code, your code will work correctly.

    In general, this is not a pythonic style. If for example:

    min([mid for left, mid, right in zip(a, a[1:], a[2:]) if left > mid < right])

    • To be honest, I understood almost nothing) - Ivan
    • You actually write in C, with index arithmetic, only in Python syntax. To understand this line, read about the basic concepts of Python - bypassing lists for..in, list comprehension, unpacking lists. And Python in one expression, instead of AND, you can put several conditions. What will look better even with index access. Only instead of the named left, mid, rigth will be written a [i - 1]> a [i] <a [i + 1] - Lecron