繁体   English   中英

序列中相邻元素的比较

[英]Comparison between adjacent elements in sequences

我需要一个序列中元素之间的比较索引。 该索引是序列中相邻元素之间所有绝对比较的总和与具有其长度的序列可具有的最高值之​​间的商。

例如,序列s1 = [0, 1, 2]s2 = [0, 2, 1]具有绝对比较[1, 1][2, 1] 没有长度为3的序列的其他组合,绝对比较和的值高于3.因此,对于s1和s2,比较指数应为2/3和3/3。

这些序列总是具有从0length - 1整数,并且可以具有非相邻的重复元素,例如[0, 1, 0, 1, 0] 这些序列在其较低和最高元素值之间具有所有整数。

我需要一个函数来计算绝对比较的最高值和给定长度的序列可以有的。 我写的函数(最高)返回错误的结果。 我写了这段代码:

    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))

生成列表的最简单方法是:

def comparisons(seq):
    return [abs(a-b) for a, b in zip(seq, seq[1:])]

至于你的比较,最高值总是最大值,然后是最小值,重复。 例如:长度为4:

[3, 0, 3, 0]

因为这将每次产生最大差异。 对于比较字符串( length-1 ),每个项目将存在这些最大差异之一( length-1 )。 因此最大值将是(length-1)**2

但是,你似乎暗示长度3的最大值是3 ,那么为什么[0, 2, 0]无效(产生[2, 2] ,总和为4 )?

您提到必须包含从0length-1所有整数,但这会使您的一些示例(例如: [0, 1, 0] )无效。 这也与任何元素都可以重复的想法相冲突(如果长度为n的列表必须包含0到n-1,则它不能重复)。

如果这种情况属实,那么您的问题就会变得与创建抖动矩阵的问题类似。

在排序范围从0到len-1的情况下,为了产生最大差异,最佳算法是从0开始,从len-1开始,将低值加到列表的最高“侧”。 ,反之亦然:

from collections import deque
from itertools import permutations
from operator import itemgetter

def comparisons(seq):
    return [abs(a-b) for a, b in zip(seq, seq[1:])]

def best_order(n):
    temp = deque([0, n-1])
    low = 1
    high = n-2
    while low < high:
        left = temp[0]
        right = temp[-1]
        if left < right:
            temp.append(low)
            temp.appendleft(high)
        else:
            temp.append(high)
            temp.appendleft(low)
        low += 1
        high -= 1
    if len(temp) < n:
        temp.append(low)
    return list(temp)

def brute_force(n):
    getcomp = itemgetter(2)
    return max([(list(a), comparisons(a), sum(comparisons(a))) for a in permutations(range(n))], key=getcomp)

for i in range(2, 6):
    print("Algorithmic:", best_order(i), comparisons(best_order(i)), sum(comparisons(best_order(i))))
    print("Brute Force:", *brute_force(i))

这给了我们:

Algorithmic: [0, 1] [1] 1
Brute Force: [0, 1] [1] 1
Algorithmic: [0, 2, 1] [2, 1] 3
Brute Force: [0, 2, 1] [2, 1] 3
Algorithmic: [2, 0, 3, 1] [2, 3, 2] 7
Brute Force: [1, 3, 0, 2] [2, 3, 2] 7
Algorithmic: [3, 0, 4, 1, 2] [3, 4, 3, 1] 11
Brute Force: [1, 3, 0, 4, 2] [2, 3, 4, 2] 11

表明该算法与蛮力方法相匹配,可以产生最佳结果。

以下是一个更通用的解决方案:

from collections import deque

def comparisons(seq):
    return [abs(a-b) for a, b in zip(seq, seq[1:])]

def best_order(seq):
    pool = deque(sorted(seq))
    temp = deque([pool.popleft(), pool.pop()])
    try:
        while pool:
            if temp[0] < temp[-1]:
                temp.append(pool.popleft())
                temp.appendleft(pool.pop())
            else:
                temp.append(pool.pop())
                temp.appendleft(pool.popleft())
    except IndexError:
        pass
    return list(temp)

i = [0, 1, 2, 3, 4, 5, 6, 0, 0, 1, 1, 2, 2]
print("Algorithmic:", best_order(i), comparisons(best_order(i)), sum(comparisons(best_order(i))))
for n in range(2, 6):
    i = list(range(n))
    print("Algorithmic:", best_order(i), comparisons(best_order(i)), sum(comparisons(best_order(i))))

这使:

Algorithmic: [2, 1, 3, 0, 5, 0, 6, 0, 4, 1, 2, 1, 2] [1, 2, 3, 5, 5, 6, 6, 4, 3, 1, 1, 1] 38
Algorithmic: [0, 1] [1] 1
Algorithmic: [0, 2, 1] [2, 1] 3
Algorithmic: [2, 0, 3, 1] [2, 3, 2] 7
Algorithmic: [3, 0, 4, 1, 2] [3, 4, 3, 1] 11

这与之前的结果相符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM