生成列表的最简单方法是简单地执行以下操作:
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
)?
您提到必须包含从0
to的所有整数length-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
在可能的情况下,它与以前的结果相匹配。