I need an index of comparison between elements in a sequence. This index is the quotient between sum of all absolute comparisons between adjacent elements in a sequence, and the highest value that a sequence with its length can have.
For instance, the sequences s1 = [0, 1, 2] and s2 = [0, 2, 1] have absolute comparisons [1, 1], and [2, 1], respectively. There is no other combination of sequences with length 3 with a higher value of absolute comparisons sum than 3. Thus, comparison index should be 2/3 and 3/3 for s1 and s2.
These sequences always have integers from 0 to length - 1 and can have non-adjacent repeated elements, such as [0, 1, 0, 1, 0]. These sequences have all integers between their lower and highest elements values.
I need a function to calculate the highest value of absolute comparisons sum a sequence with a given length can have. The function I wrote (highest) returns wrong results.
I wrote this code:
def aux_pairs(seq):
n = 2
return [seq[i:i + n] for i in range(len(seq) - (n - 1))]
def comparisons_sum(seq):
return sum([abs(el[-1] - el[0]) for el in aux_pairs(seq)])
def highest(seq):
card = len(seq)
pair = [0, card - 1]
r = (pair * (card / 2))
if card & 1 == 1:
r.append(0)
return comparisons_sum(r)
def comparison_index(seq):
return comparisons_sum(seq) / float(highest(seq))
The easiest way to produce your list is to simply do:
As to your comparison, the highest value is always going to be the maximum followed by the minimum, repeated. E.g: for length 4:
As this will produce the maximum difference each time. There will be one of these maximum differences (of
length-1) for each item in the comparison string (of lengthlength-1). Hence the maximum will be(length-1)**2.However, you seemed to imply that the maximum for length 3 was
3, so why is[0, 2, 0]not valid (producing[2, 2]which sums to4)?You mentioned that all of the integers from
0tolength-1must be included, but then this makes some of your examples (e.g:[0, 1, 0]) invalid. This also conflicts with the idea any elements can be repeated (if a list of length n must contain from 0 to n-1, it cannot have repeats).If this case is true, then your question becomes somewhat similar to the problem of creating a dithering matrix.
In the case of ordering the range from 0 to len-1, to produce the maximum difference, the optimal algorithm is to work up from 0, and down from len-1, adding the low values to the highest ‘side’ of the list, and visa versa:
Which gives us:
Showing that this algorithm matches the brute force approach for producing the best result possible.
What follows is a more general solution:
Which gives:
Which matches the previous results where it can.