The reason why ind_num1 and ind_num2 seemingly have no effect on index1 and index2 is because you're incrementing the former two variables within the loop, but you've assigned the values of index1 and index2 outside of the loop. Hence index1 and index2 will always remain to be 0 and 2 respectively. If you want to index1 and index2 to accurately reflect the values of ind_num1 and ind_num2, you need to update the values of index1 and index2 within the loop, with every iteration of the loop.
inp = [1, 2, 3, 2, 8, 7, 6, 9, 5]
def check(n):
count = 0
ind_num1 = 0
ind_num2 = 2
for i in n[1:-1]:
index1 = n[ind_num1]
index2 = n[ind_num2]
if i > index1 and i > index2:
count += 1
ind_num1 += 1
ind_num2 += 1
return count
print(check(inp))
If you want to also compare the first item in the list to the last item and second item, and the last item in the list to the second to last item and first item, it would be as simple as...
inp = [1, 2, 3, 2, 8, 7, 6, 9, 5]
def check(n):
count = 0
for e,i in enumerate(inp):
follIndex = e+1 if e+1 < len(n) else 0 # Changes index of following item to 0 if comparing last item in the list.
if i > inp[e-1] and i > inp[follIndex]:
count += 1
return count
print(check(inp)) # Returns and prints '3' because three values (3, 8, 9)
# are bigger than both of the adjacent values in the list.
You can keep track of the current index by enumerating the list, whereby the value of e is an integer that always represents the index of the current item.
The purpose of tracking the current index, or e, is so that you can easily access the adjacent list items via inp[e-1] and inp[e+1].
This allows you get rid of many unnecessary variables.
Since you don't want to compare the first or last items, this is what you would do:
inp = [1, 2, 3, 2, 8, 7, 6, 9, 5]
def check(n):
count = 0
for e,i in enumerate(n[1:-1]):
if i > n[e] and i > n[e+2]:
count += 1
return count
print(check(inp))
You use enumerate to quantify the iterations (i.e. 0, 1, ... 6) as e. This value (i.e. e) is used to determine the adjacent list items (i.e. n[e] and n[e+2]), which allows you to get rid of all those unnecessary variables. FWIW, i is essentially equal to e+1.
inp[0]toinp[-1]andinp[1]?