鉴于此输入:
[2,2,2,1], [4,3,0,0]
输出: 转换列表的步骤
Move 2 from C_to_A
Move 1 from D_to_B
为了方便起见,可以这种方式引用列表:
[A, B, C, D] #Just some means to reference the to and from of the move.
到目前为止我所拥有的:
locations = {
0: "A",
1: "B",
2: "C",
3: "D"
}
def get_steps_for_distribution(c, d):
print 'current: {} desired: {}'.format(c, d)
for i in range(0, len(c)):
diff = c[i] - d[i]
while diff > 0:
for j in range(1, len(c)):
next_index = (i + j) % len(c)
importer_diff = c[next_index] - d[next_index]
if importer_diff < 0:
number_to_move = min(diff, (0 - importer_diff))
diff -= number_to_move
importer_diff += number_to_move
c[i] -= number_to_move
d[i] += number_to_move
print_move_line(number_to_move, i, next_index)
例如,给出了一个问题:
get_steps([2,2,2,1], [4,3,0,0])
目前它提供了将所有内容转移到位置A的步骤,从而产生了这一点,这不是所提供的第二个列表的准确表示。:
>>>Move 2 from C_to_A
>>>Move 0 from C_to_B
>>>Move 1 from D_to_A
>>>Move 0 from D_to_B
答案 0 :(得分:1)
我找到了。问题是您没有更新需求(c)和资源(d)的主列表。在代码的底部,添加几行来实现这些更新:
diff -= number_to_move
importer_diff += number_to_move
c[i] -= number_to_move
d[i] += number_to_move
这会逐渐更新资源列表,直到它等于需求列表。
注意:一旦你开始工作,你可以改进一些事情:
使用模数:
简化 next_index 计算next_index =(i + j)%len(c)
您无需更新 importer_diff ;无论如何,你在下一个循环中重新计算它。
我的解决方案
这会产生所需的输出:
locations = {
0: "A",
1: "B",
2: "C",
3: "D"
}
def print_move_line(number_to_move, exporter, importer):
line = "Move " + str(number_to_move) + " from " + locations.get(exporter, "bad location") + "_to_" + locations.get(importer, "bad location")
print line
def get_steps(have, want):
for exporter_index in range(0, len(have)):
diff = have[exporter_index] - want[exporter_index]
# Move 'diff' units from bin 'exporter_index' to others;
# offer valid only while supplies last.
while diff > 0:
for j in range(1, len(have)):
# Start next to the exporter and wrap around.
importer_index = (exporter_index + j) % len(have)
print(" DEBUG: have", have, "want", want, "\t", exporter_index, "to", importer_index)
importer_diff = have[importer_index] - want[importer_index]
# If this bin needs units, move what we have.
if importer_diff < 0:
number_to_move = min(diff, (-importer_diff))
print(" DEBUG: bin", importer_index, "needs", importer_diff, "donor has", diff)
diff -= number_to_move
have[exporter_index] -= number_to_move
importer_diff -= number_to_move
have[importer_index] += number_to_move
print_move_line(number_to_move, exporter_index, importer_index)
get_steps([2, 2, 2, 1], [4, 3, 0, 0])
输出:
(' DEBUG: have', [2, 2, 2, 1], 'want', [4, 3, 0, 0], '\t', 2, 'to', 3)
(' DEBUG: have', [2, 2, 2, 1], 'want', [4, 3, 0, 0], '\t', 2, 'to', 0)
(' DEBUG: bin', 0, 'needs', -2, 'donor has', 2)
Move 2 from C_to_A
(' DEBUG: have', [2, 2, 4, 1], 'want', [2, 3, 0, 0], '\t', 2, 'to', 1)
(' DEBUG: bin', 1, 'needs', -1, 'donor has', 0)
Move 0 from C_to_B
(' DEBUG: have', [2, 2, 4, 1], 'want', [2, 3, 0, 0], '\t', 3, 'to', 0)
(' DEBUG: have', [2, 2, 4, 1], 'want', [2, 3, 0, 0], '\t', 3, 'to', 1)
(' DEBUG: bin', 1, 'needs', -1, 'donor has', 1)
Move 1 from D_to_B
(' DEBUG: have', [2, 2, 4, 2], 'want', [2, 2, 0, 0], '\t', 3, 'to', 2)
[wdwickar@wdwickar-ws pyside]$ python so.py
(' DEBUG: have', [2, 2, 2, 1], 'want', [4, 3, 0, 0], '\t', 2, 'to', 3)
(' DEBUG: have', [2, 2, 2, 1], 'want', [4, 3, 0, 0], '\t', 2, 'to', 0)
(' DEBUG: bin', 0, 'needs', -2, 'donor has', 2)
Move 2 from C_to_A
(' DEBUG: have', [4, 2, 0, 1], 'want', [4, 3, 0, 0], '\t', 2, 'to', 1)
(' DEBUG: bin', 1, 'needs', -1, 'donor has', 0)
Move 0 from C_to_B
(' DEBUG: have', [4, 2, 0, 1], 'want', [4, 3, 0, 0], '\t', 3, 'to', 0)
(' DEBUG: have', [4, 2, 0, 1], 'want', [4, 3, 0, 0], '\t', 3, 'to', 1)
(' DEBUG: bin', 1, 'needs', -1, 'donor has', 1)
Move 1 from D_to_B
(' DEBUG: have', [4, 3, 0, 0], 'want', [4, 3, 0, 0], '\t', 3, 'to', 2)