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>This takes your initial input and converts it to a list of tuples. Then it calculates the absolute difference between the tuple members and puts that into a new list. Then your final output is created as index_list.</p>

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

new_list = [(x,y) for (x,y) in zip(wire_ints[::2], wire_ints[1::2])]
diff_list = [abs(x[0] - x[1]) for x in new_list]
index_list = [(x, index) for (index, x) in enumerate(diff_list) if x == max(diff_list)] 

print index_list
</code></pre>

<p>Just realised this could be compressed further</p>

<pre><code>new_list = [abs(x-y) for (x,y) in zip(wire_ints[::2], wire_ints[1::2])]
index_list = [(x, index) for (index, x) in enumerate(diff_list) if x == max(diff_list)] 
</code></pre>

<p>If you desperately want a dictionary, the best approach would probably be storing the max value as a key, and the pair numbers in a list or tuple as the value.</p>

python