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>As far as you can have only unique keys in dictionary, you should either use pair number as a key or use a list:</p>

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

longest_dict = []
longest_so_far = 0

for i in range(len(wire_ints)//2):
  j = i*2
  a, b = wire_ints[j:j+2]
  dist = abs(a - b)
  pair = [dist, i + 1]
  if dist &gt; longest_so_far:
    longest_so_far = dist
    longest_dict = [pair]
  elif dist == longest_so_far:
    longest_dict.append(pair)

print(longest_dict)
#=&gt; [[9, 1], [9, 2]]
</code></pre>

python