我正在尝试创建一个程序,它将创建一个10元素数组,然后为每个元素分配随机值。然后我希望程序判断数组是否平衡。通过平衡我的意思是,在数组值中的任何位置,在某个元素处,元素中的值的总和等于元素中的数组值的总和大于当前元素。
实施例
元素(1,2,3,4)值(2,1,3,0) 然后程序将显示元素1-2平衡到elemtns 3-4,因为它们都等于4。
到目前为止我已经
了import random
size = 10
mean = 0
lists = [0] * size
for i in range(size):
var = random.randint(0,4)
lists[i] = var
for i in lists:
mean += i
avg = (mean)/(size)
我认为元素可以平衡的唯一方法是,如果平均值等于2,那么我认为这是我应该如何开始。
我很欣赏正确方向的任何帮助。
答案 0 :(得分:5)
如果我理解这个问题,最简单的解决方案是这样的:
def balanced(numbers):
for pivot in range(len(numbers)):
left_total = sum(numbers[:pivot])
right_total = sum(numbers[pivot:])
if left_total == right_total:
return pivot
return None
例如:
>>> numbers = [2, 1, 3, 0]
>>> balanced(numbers)
2
>>> more_numbers = [2, 1, 3, 4]
>>> balanced(numbers)
(没有打印任何东西,因为它返回了None
,这意味着没有用于平衡周围列表的支点。)
虽然这是最简单的解决方案,但它显然不是最有效的,因为你会一遍又一遍地添加相同的数字。
如果您考虑一下,应该很容易弄清楚如何保持left_total
和right_total
的总计,只调用sum
一次。
def balanced(numbers):
left_total, right_total = 0, sum(numbers)
for pivot, value in enumerate(numbers):
if left_total == right_total:
return pivot
left_total += value
right_total -= value
return None
最后,以下是围绕它构建程序的方法:
size = 10
numbers = [random.range(4) for _ in range(size)]
pivot = balanced(numbers)
if pivot is None:
print('{} is not balanced'.format(numbers))
else:
print('{} is balanced, because elements 1-{} equal {}-{}'.format(
numbers, pivot+1, pivot+2, size+1))
答案 1 :(得分:0)
要了解此类问题的良好数据结构是具有累积和的数组。 element[j] - element[i]
是原始系列中i
到j
的总和。如果您有原始系列[1, 2, 3, 4]
,则累积系列为[0, 1, 3, 6, 10]
。原始系列中i
位置的总和为element[i] - element[0]
。对于这个问题,我们只对从0开始的总和感兴趣,所以这有点矫枉过正,但同样对其他问题更有用。
以下是制作累计总和的代码:
def cumulative_sum(series):
s = [0]
for element in series:
s.append(element + s[-1])
return s
鉴于此,我们可以使用以下代码找到支点:
def find_pivot(series):
cs = cumulative_sum(series)
total = cs[-1]
even_total = not (total & 1)
if even_total:
target = total // 2
for i, element in enumerate(cs[1:]):
if element == target:
return i + 1
return -1
请注意,如果我们知道序列总和为奇数,则没有必要尝试划分系列:那么就不能有一个支点。
或者,您可以像这样写find_pivot
:
def find_pivot(series):
cs = cumulative_sum(series)
total = cs[-1]
even_total = not (total & 1)
if even_total:
target = total // 2
try:
return cs.index(target)
except ValueError:
return -1
return -1
它的优点是循环不是在python中显式完成的,而是在标准库中的C代码中完成的。
尝试输出代码:
def test():
for i in range(1, 30):
test_values = range(i)
j = find_pivot(test_values)
if j >= 0:
print "{0} == {1}".format(test_values[:j], test_values[j:])
我们得到了这个输出:
[0] == []
[0, 1, 2] == [3]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] == [15, 16, 17, 18, 19, 20]