Quite a beginner in python. Wrote this cycle:

count = 0 a = [1,5,3,4,2] k = 2 for i in range(len(a) - 1): for j in range(i + 1, len(a)): if a[i] - a[j] == k: count += 1 print("i = " + str(i)) print("j = " + str(j)) print ("count = " + str(count)) 

Result:

 i = 1 j = 2 i = 3 j = 4 

Why such a result? I expected:

 i = 0 j = 1 j = 2 ... i = 1 j = 2 

    1 answer 1

    Because your code displays only those indices for which the difference is equal to k.

    To display all indexes (though why?), You can do this:

    http://ideone.com/Y7U40T

     count = 0 a = [1,5,3,4,2] k = 2 for i in range(len(a) - 1): print("i = " + str(i)) for j in range(i + 1, len(a)): if a[i] - a[j] == k: count += 1 print("j = " + str(j)) print ("count = " + str(count)) 
    • Yes, really, I'm stupid. Used to brackets. Here they are not - van9petryk