栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

KenKen拼图附加项:REDUX A(已更正)非递归算法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

KenKen拼图附加项:REDUX A(已更正)非递归算法

首先,我将使用含义很深的变量名,以便使代码易于理解。然后,在我理解了这个问题之后,这显然是一个递归问题,因为一旦您选择了一个数字,寻找其余平方的可能值的问题就完全一样,只是其中的值不同。

所以我会这样:

from __future__ import divisionfrom math import ceildef make_combos(max_val,target_sum,n_cells):    combos = []    # The highest possible value of the next cell is whatever is     # largest of the max_val, or the target_sum minus the number     # of remaining cells (as you can't enter 0).    highest = min(max_val, target_sum - n_cells + 1)    # The lowest is the lowest number you can have that will add upp to     # target_sum if you multiply it with n_cells.    lowest = int(ceil(target_sum/n_cells))    for x in range(highest, lowest-1, -1):        if n_cells == 1: # This is the last cell, no more recursion. combos.append((x,)) break        # Recurse to get the next cell:        # Set the max to x (or we'll get duplicates like        # (6,3,2,1) and (6,2,3,1), which is pointless.        # Reduce the target_sum with x to keep the sum correct.        # Reduce the number of cells with 1.        for combo in make_combos(x, target_sum-x, n_cells-1): combos.append((x,)+combo)    return combosif __name__ == '__main__':    import pprint    # And by using pprint the output gets easier to read    pprint.pprint(make_combos( 6,12,4))

我还注意到您的解决方案似乎仍然有问题。例如,对于这些值,

max_val=8, target_sum=20 andn_cells=5
您的代码找不到解决方案
(8,6,4,1,1,)
。我不确定这是否意味着我错过了一条规则,但是据我了解,这些规则应该是有效的选择。

这是一个使用生成器的版本,如果值真的很大,它会节省几行代码和内存,但是作为递归,生成器“获取”可能很棘手。

from __future__ import divisionfrom math import ceildef make_combos(max_val,target_sum,n_cells):    highest = min(max_val, target_sum - n_cells + 1)    lowest = int(ceil(target_sum/n_cells))    for x in xrange(highest, lowest-1, -1):        if n_cells == 1: yield (x,) break        for combo in make_combos(x, target_sum-x, n_cells-1): yield (x,)+comboif __name__ == '__main__':    import pprint    pprint.pprint(list(make_combos( 6,12,4)))


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569141.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号