-原文恕刪-
之前有答過類似問題
原文在 #1FYW0hPP (Programming)
我把原po問題分為3個步驟
1. 不同容器,相同物品 的 排列組合
2. 原PO有一個特別限制條件是翻轉一樣的要去掉
3. 每個數字(容器內物品數) <= K (原po設為2)
1)
from itertools import combinations
from numpy import diff
m,n = 9,8 # 改成9樣相同物品給8個相異箱子
c = [ (0,)+i+(m,) for i in combinations(range(1,m),n-1)]
result1 = map(lambda x: tuple(diff(x)),c)
這時result會包含像是(1,1,1,2)及(2,1,1,1)這種對稱的
2)
由於http://docs.python.org/2/library/itertools.html#itertools.combinations
有說到這邊會是 in sorted order
所以這個result直接取前半段就可以了
from math import ceil
result2 = result1[:int(ceil(len(result1)/2.0))]
3)
最後去掉大於2的數字 (optional)
result3 = filter(lambda x:False not in [i < 3 for i in x],result2)
# result3 為最終結果
寫到這邊忽然看到前面dark版友回兩篇都是重複組合
但是原PO那張圖的需求又讓我覺得怪怪的@@
因為萬一是 4個數字 總和6
像是(1,2,1,2)就不會出現在重複組合裡(因為只會出現(1,1,2,2)這樣)
所以還要再排列一次不是?
通篇廢話不知道有沒有幫到原po就是了~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.104.179.117
※ 編輯: bob123 來自: 59.104.179.117 (11/04 18:23)
※ 編輯: bob123 來自: 59.104.179.117 (11/04 18:29)
※ 編輯: bob123 來自: 59.104.179.117 (11/04 18:30)
推
11/15 22:01, , 1F
11/15 22:01, 1F
→
11/15 22:02, , 2F
11/15 22:02, 2F
→
11/15 22:04, , 3F
11/15 22:04, 3F
→
11/15 22:05, , 4F
11/15 22:05, 4F
→
11/15 22:06, , 5F
11/15 22:06, 5F
→
11/15 22:09, , 6F
11/15 22:09, 6F
→
11/15 22:09, , 7F
11/15 22:09, 7F
討論串 (同標題文章)
完整討論串 (本文為第 6 之 6 篇):
問題
0
24