【发布时间】:2011-07-07 20:39:23
【问题描述】:
上周我问了一个关于 C++ 中排列的问题 (List of combinations of N balls in M boxes in C++)。
答案对我帮助很大,但我的问题现在已经改变。 我想做的是从这个 python 函数到 C++ 的翻译,在结果中保持相同的顺序:
def combinations_with_replacement_counts(n, r): #(n-boxes, r-balls)
size = n + r - 1
for indices in itertools.combinations(range(size), n-1):
#print indices
starts = [0] + [index+1 for index in indices]
stops = indices + (size,)
yield tuple(map(operator.sub, stops, starts))
我没有 python 技能,尽管我阅读了文档,但我不明白这个函数。
【问题讨论】:
-
具体哪些部分你不明白?
-
我不明白 itertools.combinations() 算法和“stops = indices + (size,)”的语法。关于map(),它是否分别减去starts和stop的元素,例如stops[i] - starts[i]或stops[1] -stops[2]...和starts[1] -starts[2]?跨度>
标签: c++ python permutation