考慮到字符串數組獲得的數組項的所有組合,同時保留序列 - 紅寶石
["the" "cat" "sat" "on" "the" "mat"]
我希望得到的序列項目的所有組合,從任何初始位置,例如
["the"]
["the" "cat"]
["the" "cat" "sat"]
...
["cat" "sat" "on" "the" "mat"]
["sat" "on" "the" "mat"]
["on" "the" "mat"]
...
["sat" "on"]
["sat" "on" "the"]
不允許原始序列或缺失元素的組合,例如,
["sat" "mat"] # missing "on"
["the" "on"] # reverse order
我也想知道這個操作是否有一個特定的名字,或者如果有一個更好的方式來描述它。
謝謝。
我把編輯範圍的自由使用'的...''而不是-1'。另請注意,Ruby 1.9.2引入了'flat_map',它基本上等價於map {}。flatten(1)'。也可用'backports' :-) – 2010-08-11 14:30:36