Comment detail

全ての組み合わせ (Nested Flatten)

やはり再起処理で解くのが スタンダードでしょうか。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def crossProduct( params ){

    def result = []

    switch(params.size()){
    case 1:
        // 何もしない
        break;
    case 2:
        params[0].each{ item1 ->
            params[1].each{ item2 ->
                result << ([] + item1) + ([] + item2)
            }
        }
        break;
    default:
        result = crossProduct( [crossProduct(params[0..1])] + params[2..-1])
        break;
    }

    result
}


crossProduct([[1,2,3,4], ["a", "b", "c"], ["あ", "い"]]).each{
    println it
}
crossProduct([[1,2,3,4], "abc"]).each{
    println it
}

Index

Feed

Other

Link

Pathtraq

loading...