Finding largest distance (on number scale) between n pairs of numbers
<p>I am trying to write an algorithm that finds the largest distance between two numbers, given n pairs of numbers.</p>

<p>Here is what I have so far.</p>

<p>Wire ints is my example numbers, with first pair being <code>1,10</code> and second pair being <code>1,10</code> and third pair being <code>7,7</code>.</p>

<pre><code>wire_ints = [10, 1, 1, 10, 7, 7]
longest_cases = {}
largest_length = 0
q = 0
y = 0
leftcounter = 0
rightcounter = 1

while q &lt; len(wire_ints):
    left_port = wire_ints[leftcounter]
    right_port = wire_ints[rightcounter]
    length_wire = left_port - right_port
    wire_length = abs(length_wire)
    leftcounter = leftcounter + 2
    rightcounter = rightcounter + 2
    q = q + 2
    y = y + 1
    if not longest_cases:
        largest_length = wire_length
        longest_cases[wire_length] = y
    elif wire_length == largest_length:
        longest_cases[wire_length] = y
    elif wire_length &gt; largest_length:
        largest_length = wire_length
        longest_cases.clear()
        longest_cases[wire_length] = y

print(longest_cases)
</code></pre>

<p>This currently outputs <code>{9:2}</code>, and its not wrong. <code>9</code> is the greatest distance between any of these pairs of numbers. BUT, I want it to print <code>{9:1, 9:2}</code>.</p>

<p>The key in the dictionary refers to greatest length, and the value refers to the number of the pair in the original array. With the first 2 integers being pair 1, then the second pair 2, etc.</p>

<p>So, as the array has two pairs with the same length, it should output BOTH pairs in the dictionary.</p>

<p>I cannot figure this out.
Help!</p>

<p>What you are missing is not keeping distance indexes in a list. You can provide it by the code below:</p>

<pre><code>longest_cases = {}
wire_ints = [10, 1, 1, 10, 7, 7]

leftIndex = 0
rightIndex = 1

largest_length = 0
y = 1

while leftIndex &lt; len(wire_ints):
    cur_length = abs(wire_ints[leftIndex] - wire_ints[rightIndex])

    if cur_length in longest_cases:
        currentList = longest_cases[cur_length]
    else:
        currentList = []

    currentList.append(y)
    longest_cases[cur_length] = currentList

    leftIndex = leftIndex + 2
    rightIndex = rightIndex + 2
    y = y + 1

resultList = []
resultCase = 0
for k in longest_cases:
    if k &gt; largest_length:
        resultList = longest_cases[k]
        resultCase = k

for index in resultList:
    print resultCase, ":", index
</code></pre>

<p>Here is <a href="http://ideone.com/qX65xi" rel="nofollow">the link</a> that you can see the result.</p>

python