Sum of Pairs: Codewars
<p>I recently solved a problem as stated below in Codewars(<strong>NOT</strong> asking for a solution, already solved it) and though it is not the optimized solution I came across a very interesting problem which I could not figure out an answer to.</p>

<p><strong>I have NO intention to give out the solution, but just understand what happened that resulted in this anomaly</strong> </p>

<p>The problem ran as - Given a list of integers and a single sum value, return the first two values (parse from the left please) in order of appearance that add up to form the sum.</p>

<p>Pretty simple problem and a straightforward if not optimum solution that I came up with -</p>

<pre><code>def sum_pairs(ints, s):
    indices = []
    for i in ints:
        if (s-i) in ints[ints.index(i)+1:]:
            print(ints.index(i))
            print(ints[ints.index(i)+1:])
            print(i,s-i)
            indices.append([ints.index(i),ints[ints.index(i)+1:].index(s-i)+len(ints[:ints.index(i)])+1])
    if(len(indices) == 0):
        return None
    print(indices)
    indices.sort(key=lambda x: x[1])
    print(indices)
    return [ints[indices[0][0]], ints[indices[0][1]]]
</code></pre>

<p>I passed all the test-cases.</p>

<p>I made use of the print statements for debugging and figuring out what was going on. Sample test cases and their output which stumped me are given below.</p>

<pre><code>sum_pairs([1, 2, 3, 4, 1, 0], 2)

0
[2, 3, 4, 1, 0]
(1, 1)
1
[3, 4, 1, 0]
(2, 0)
0
[2, 3, 4, 1, 0]
(1, 1)
[[0, 4], [1, 5], [0, 4]]
[[0, 4], [0, 4], [1, 5]]

sum_pairs([10, 5, 2, 3, 7, 5])
1
[2, 3, 7, 5]
(5, 5)
3
[7, 5]
(3, 7)
1
[2, 3, 7, 5]
(5, 5)
[[1, 5], [3, 4], [1, 5]]
[[3, 4], [1, 5], [1, 5]]
</code></pre>

<p>So now to the part that I could not figure out, observing the output one can see that in test case 1, 0 is printed twice and the ints[ints.index(i)+1:] is [2, 3, 4, 1, 0] for both 1's at position 0 and 4 and the entry [0,4] is appended twice.
A similar pattern is observed in test case 2.
The if condition which says </p>

<pre><code>if (s-i) in ints[ints.index(i)+1:]
</code></pre>

<p>should not be evaluated to true the second time as ints[ints.index(i)+1:] is there to ensure that the new list starts from the item next to the occurrence of i.</p>

<p>It seems not that important as I got the solution but it would be great if someone can shed light on what actually happened.</p>

<p><strong>NOTE</strong>: I have had 2 bad experiences where people downvoted my question without giving a reason to do so in a predefined ratio. It would be really useful if I could be given feedback for any such downvote, also would appreciate if anyone could check those questions and give feedback if any improvements can be made.</p>

<p><code>ints.index(i)</code> doesn't give you the index of the particular occurrence of <code>i</code> you're working with. If you want that, you want <code>enumerate</code>, not <code>index</code>.</p>

<p><code>ints.index(i)</code> means "go through <code>ints</code> and find the index of the <em>first</em> element of <code>ints</code> that equals <code>i</code>". <code>i</code> holds no information about where in <code>ints</code> it came from, so there's no way for <code>index</code> to tell that you were thinking of any particular occurrence of <code>i</code>. <code>enumerate</code> avoids this problem by keeping its own counter and incrementing it every time it produces an element, so it always knows what index it's at.</p>

python