python - 用最合适的 "blocks"填充一个区域

标签 python algorithm

我有一些代码可以根据用户的选择生成文本 block 。这些文本 block 的高度因用户选择的项目数而异。我要做的是确保这些 block 以最有效的方式排列在页面上。

例如,第 1 部分高 250 点,第 2 部分高 650 点。如果用户选择:

表格a部分的内容值(value)400分
b部分内容200分
c部分内容250分
d部分内容50分

如何确保 b 部分和 d 部分进入第 1 部分,而 a 和 c 部分进入第 2 部分?

到目前为止,这是我的代码:

section1_height = 250
section2_height = 650

#list1 and list2 are the variables that contain the user selections
Column1 = DWIMBLOCK([list1], (18, 430), (LEFT, TOP_BASELINE), (250, 250))
Column2 = DWIMBLOCK([list2], (275, 430), (LEFT, TOP_BASELINE), (250, 650))

columns = [Column1, Column2]
sec1_columns = []
sec2_columns = []

for column in columns:
 if column.height <= 250:
  sec1_columns.append(column)

for shorts in sec1_columns:
 if #This is where I am stuck

如您所见,我已将我的专栏划分为高度小于 250 点的专栏,但现在我无法尝试按照 if sum of any number of blocks <= 250, assign those blocks to a new list 的方式做一些事情。我应该怎么做呢?谢谢!

更新:

这里是布局的粗略轮廓,只是为了让你看得更清楚。

____________________
|#########**********|
|# image #*        *|
|#########*        *|
|**********        *|
|*       **        *|
|*sec. 1 **        *|
|*       **sec. 2  *|
|**********        *|
|#########*        *|
|#       #*        *|
|# image #*        *|
|#       #*        *|
|#########**********|
____________________

两张图片总是在同一个地方,大小都一样。
还应该注意的是,这是用于 PDF 制作,而不是网络使用,因此 CSS 和 Javascript 不是选项。我使用的环境仅允许 Python 代码。

最佳答案

基本上,这是一个接一个地解决每个部分的背包问题(长度既是值又是权重)。我将为此使用蛮力,但您可以查找它并找到其他在速度方面更有效的方法,但可能无法提供最佳解决方案 - 这个可以。

2^b(b 是 block 的数量)组合来填充第一部分,因为对于每个 block ,您可以将它放在那里或不放在那里把它放在那里。只有其中的一部分是可行的。您选择填充最多的组合。然后重复下一部分的剩余项目。

这应该让您知道如何去做:

from itertools import combinations, chain

unassigned_blocks = {
    ('a', 400),
    ('b', 200),
    ('c', 250),
    ('d',  50),
    # ...
}

sections_and_assigned_blocks = {
    ('1', 250): {},
    ('2', 650): {},
    # ...
}

for section in sorted(sections_and_assigned_blocks.keys()):
    best, best_length = {}, 0
    for combination in chain(*[combinations(unassigned_blocks, n)
                               for n in xrange(1, len(unassigned_blocks)+1)]):
        combination = set(combination)
        length = sum(block[1] for block in combination)
        if best_length < length <= section[1]:
            best, best_length = combination, length
    sections_and_assigned_blocks[section] = best
    unassigned_blocks -= best

from pprint import pprint
pprint(sections_and_assigned_blocks)
# {('1', 250): set([('c', 250)]),
#  ('2', 650): set([('a', 400), ('b', 200), ('d', 50)])}

时间复杂度是 O(s*2^b)(s 是部分的数量)。在最坏的情况下,第 1-3 节太小而无法包含任何内容,将有 4 * 2^15 = 131072 次迭代。在如此小的规模上,蛮力通常不是问题。但是,增加 block 的数量会对性能产生巨大影响!

关于python - 用最合适的 "blocks"填充一个区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16743317/

相关文章:

string - 寻求基本字符串差异算法的建议

python - Django:如何直接在每个原始帖子对象下对帖子的评论进行排序?

algorithm - 实现智能列表

algorithm - 将任意 GUID 编码为可读的 ASCII (33-127) 的最有效方法是什么?

python - pd.read_csv 忽略没有标题的列

algorithm - 为什么 SHA2 有 384 位版本?

algorithm - 如何计算 GPX 轨道中的 "corners"?

python - 使用Python在Mysql数据库中插入一个值

python - 如何通过代码使用 Python Alembic 运行迁移?

python - SWIG 和 Windows 导出宏