1

私はこのような配列を持っています

mainArray =>
    [a] =>
        [A]
        [B]
        [C]
    [b] =>
        [D]
        [E]
        [F]
    [c] =>
        [G]
        [H]
        [I]

これらを次のような配列に入れたいと思います。

secondArray =>
    [0] => { [A], [D], [G] }
    [1] => { [A], [E], [G] }
            .
            .
            .
    [n] => { [C], [F], [I] }

{[A]、[B]、..}の特定の要素で始まる$secondArrayの適切な数の要素を取得する方法を理解するのに問題があります。たとえば、[A]で始まるものはいくつありますか。

これは私がしなければならないと思うことです:

secondArray =>
   [0] =>
        [A]

secondArray =>
   [0] =>
        [A]
        [D]

secondArray =>
   [0] =>
        [A]
        [D]
        [G]


secondArray =>
   [0] =>
        [A]
        [D]
        [G]
   [1] =>
        [A]
        [D]
        [H]


secondArray =>
   [0] =>
        [A]
        [D]
        [G]
   [1] =>
        [A]
        [D]
        [H]
   [2] =>
        [A]
        [D]
        [I]

secondArray =>
   [0] =>
        [A]
        [D]
        [G]
   [1] =>
        [A]
        [D]
        [H]
   [2] =>
        [A]
        [D]
        [I]
   [3] =>
        [A]
        [E]
        [G]
   [4] =>
        [A]
        [E]
        [H]
   [5] =>
        [A]
        [E]
        [I]

secondArray =>
   [0] =>
        [A]
        [D]
        [G]
   [1] =>
        [A]
        [D]
        [H]
   [2] =>
        [A]
        [D]
        [I]
   [3] =>
        [A]
        [E]
        [G]
   [4] =>
        [A]
        [E]
        [H]
   [5] =>
        [A]
        [E]
        [I]
   [6] =>
        [A]
        [F]
        [G]
   [7] =>
        [A]
        [F]
        [H]
   [8] =>
        [A]
        [F]
        [I]

などですが、どうやって実装するのか本当に考えられません...

どんな助けでもいただければ幸いです

4

2 に答える 2

1

要素を順番に生成できます。

イテレータを実装するには:

-remember the array of input arrays, if there exists a next element.
   and an array of indexes of the same length.
-if any input array is empty, the result set is empty and there is no next (first) element.
-initialise the array of indexes to all zeroes.

To get the next element:
-If you remember there is no next element, fail.
-compute the result:
--start with an empty result
--For each input array and its corresponding index
---Append input[index] to the result
-compute the next set of indexes:
--Iterate the indexes in reverse order. For each index
---Increment the index
---If the index now points past its corresponding input array
----Reset the index to zero
---Else
----Return the result, exiting the function.
-Remember there is no next element
-Return the result (last element).

すべての組み合わせを一度に実行したい場合は、コードを少し単純化します。

-if any input array is empty, the result set is empty.
-initialise the array of indexes to all zeroes.
-compute the result and store in the result set.
-while not done generating results:
--Iterate the indexes in reverse order. For each index
---Increment the index
---If the index now points past its corresponding input array
----Reset the index to zero
---Else
----Compute the result from the current indexes
----Add the result to the result set
----Continue generating the results
--(all indexes were reset to zero) finish generating the results.
-return the result set.
于 2012-09-29T17:56:46.013 に答える
1

Rubyの場合:

['A', 'B', 'C'].product(['D', 'E', 'F'])
# => [['A', 'D'], ['A', 'E']...

Array#product複数の配列を受け入れることができます。

于 2013-07-18T23:39:57.487 に答える