[英]Limiting the number of trials in combination Ruby
以下代碼將5C4
(5個組合4)的所有結果計算為字符串:
a = ['1', '2', '3', '4', '5']
result_array = a.combination(4).map(&:join).to_a
有沒有一種方法可以用來限制組合試驗的次數,類似於Python中的itertools
模塊?
嘗試這個:
result_array = a.combination(4).lazy.map(&:join).take(2).to_a
lazy
將確保您僅計算take
所需的任意多個值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.