【问题标题】:Python Algorithm To Maximize Number of Equal Elements in ListPython算法最大化列表中相等元素的数量
【发布时间】:2015-02-15 06:16:28
【问题描述】:

我正在尝试在 Python 中创建一个算法,该算法将采用从 0 到 1,000,000 的随机数列表,长度不超过 100 个元素,并且会尽可能地将该数组平均化,从而为我提供最大数量的相等元素.这是我目前所拥有的:

def answer(x):
    diff = max(x) - min(x)
    while diff > 1:
        x[x.index(max(x))] = x[x.index(max(x))] - (diff / 2)
        x[x.index(min(x))] = x[x.index(min(x))] + (diff / 2)
        diff = max(x) - min(x)
    return count(x)

def count(x):
    from collections import Counter
    c = Counter(x)
    return max(c.values())

这将获取一个数组,例如 [0,50] 并创建一个数组 [25,25] 并返回整数 2,因为数组中有两个相等的元素。我知道这个算法在大多数情况下都有效,但它根本不起作用。 谁能指出任何不会产生正确答案的整数数组?谢谢

编辑:

对于那些不想阅读 while 循环的人来说,代码查找整个列表的范围。将范围分成两半,将一半加到最小值,然后从最大值减去一半。它试图均衡整个列表,同时保持相同的总和

[1,4,1] = [2,3,1] = [2,2,2] =(相等元素的数量)3 [2,1,4,9] = [2,5,4,5] = [3,4,4,5] = [4,4,4,4] = (相等元素的数量) all4

【问题讨论】:

  • 如果您已经知道它并不总是有效,为什么还需要我们告诉您何时它并不总是有效?
  • 我只知道在一些我不知道数组是什么的测试中,它失败了我不知道哪些条件使它失败了,这就是我所知道的
  • 您想要 0-1m 范围内的 100 个随机数并希望找到最大相等的数?
  • 我不明白。你试图用数组的平均值替换数组中的所有数字? (这就是 [0,50] -> [25,25] 对我的建议)
  • 是的,如果您阅读了 while 循环中明显的信息

标签: python arrays algorithm list


【解决方案1】:

这个怎么样?

l = [1, 2, 5, 10]

# "best" possible case
l_m = [sum(l) / len(l)] * len(l)

# see if lists fit (division can cause rounding errors)

if sum(l_m) != sum(l):
    # if they don't this means we can only have len(l) - 1 similar items
    print len(l) - 1
else:
    # if sums fit the first list can be spread like this
    print len(l)

【讨论】:

  • 不错,也可以这样做:l = [1, 2, 5, 10]; s = sum(l); ave = s / len(l); print len(l) - (s != ave * len(l))
【解决方案2】:

我可以想象你正在尝试使数组中的元素尽可能多,同时保持它们的总和,并保持元素整数

对于N 元素,您可以让N - 1 元素相等,如果运气好的话,所有N 都相等。

这是给你的一些伪代码:

average = sum(elements) / length(elements)  # a float
best_approximation = trunc(average)  # round() would also work
discrepancy = sum(elements) - best_approximation * length(elements)
discrepant_value = best_approximation + discrepancy
result = [discrepant_value] + the rest of list having best_approximation value

通过构造,您将获得相等值的length(elements) - 1 和一个discrepant_value

【讨论】:

  • 是的,对于 N 个元素,我可以让 n-1 相等,并且某些列表 N 相等。例如 [1,2,3,4,5,7] 的列表将返回 [4,3,3,4,4,4]。现在要实际最大化列表相等性,我需要将最小值增加 1,并将新的最小值减少 1。这将给我 n-1 个元素相等我认为如果我正确实现它应该可以工作
  • 您根本不需要关心最小值和最大值。您丢弃原始值。您只关心它们的总和和平均值,以及由discrepancy 表示的舍入误差。对于[1 2 3 4 5 7],您可以获得[6 3 3 3 3 3][2 4 4 4 4 4]
【解决方案3】:

在将输入标准化为整数平均值并将余数分配到结果中时,您真正在做什么。

L = [1,2,3,4,5,7]

# Calc the integer average
avg = sum(L)/len(L)

# Find the remainder
mod = sum(L)%len(L)

# Create a new list length of original
# populating it first with the average
L2 = [avg] * len(L)

# Then add 1 to each element for as many
# as the remainder
for n in range(mod): L2[n] += 1

def count(x):
    from collections import Counter
    c = Counter(x)
    return max(c.values())
count(L2)
4

您不需要修改原始列表或创建一个新列表(不需要您的import):

L = [1,2,3,4,5,7]

# Don't even need to figure the average if you
# find the remainder of the sum of your list
# divided by the length of your list
mod = sum(L)%len(L)

result = mod if mod >= len(L)/2 else len(L) - mod
print result
4

【讨论】:

  • 虽然 Python 可读性很好,但我认为这个答案可以通过对过程的实际解释来改进。
【解决方案4】:

这是我得出的最终解决方案。

在将整个数组的范围最小化到不大于 1 之后,它会检查数组中相等数字的数量是否与长度相同,这意味着数组看起来像这样:[4,4,4,4] then立即吐出相等数字的数量(4)。如果列表中大多数相等数字的数量小于长度,则它使列表相等。因此,如果列表类似于[4,4,3,3],则最好将其转换为[4,4,4,2]。这就是均衡函数可以做的。

def answer(x):
    diff = max(x) - min(x)
    while diff > 1:
        x[x.index(max(x))] = x[x.index(max(x))] - (diff / 2)
        x[x.index(min(x))] = x[x.index(min(x))] + (diff / 2)
        diff = max(x) - min(x)
    print(x)
    if count(x) == len(x):
        return count(x)
    return equalize(x)    

def equalize(x):
    from collections import Counter
    eq = Counter(x)
    eq = min(eq.values())
    operations = eq - 1
    for i in range(0,operations):
        x[x.index(min(x))] = x[x.index(min(x))] + 1
    return count(x)

def count(x):
    from collections import Counter
    c = Counter(x)
    return max(c.values())

http://repl.it/6bA/1

【讨论】:

    猜你喜欢
    • 2012-11-09
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多