0

I have alphabet array 24 character: "A B C D E F G H I J K L M N O P Q R S T U V W X"

I want collect all case with: 3 unique characters.

First case: ABC, DEF, GHI, JKL, MNO, PQR, STU, VWX

B11002
  • 95
  • 1
  • 2
  • 5
  • You want to get all separations into subarrays each containing exactly 3 elements? – phimuemue Jul 10 '10 at 15:07
  • 1
    Does the order matter. For instance, is DEF, ABC, GHI, JKL, MNO, PQR, STU, VWX the same as what you give or not? (it must be different for existing 24! such set lists) – Artefacto Jul 10 '10 at 15:07
  • I would suggest looking at [array_chunk](http://www.php.net/manual/en/function.array-chunk.php) and [array_unique](http://www.php.net/manual/en/function.array-unique.php). – Austin Hyde Jul 10 '10 at 15:10
  • @Jamie Wong: Yes, I want "generating 277 743 578 112 000 sets" or 4 characters or 12 characters set... – B11002 Jul 10 '10 at 15:20
  • @B11002 You realize 24! is of the order of 10^24. A petabyte are only 10^15 bytes... – Artefacto Jul 10 '10 at 15:24
  • @B11002 I delete my answer because I realized my math was, indeed, wrong – Jamie Wong Jul 10 '10 at 15:24

3 Answers3

2

This is a little late coming, but for anyone else reading over this: If you are looking to split a string into 3-character chunks, try PHP's built in str_split() function. It takes in a $string and $split_length argument. For example:

$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWX';
$grouped  = str_split($alphabet, 3);

var_export( $grouped );

This outputs the following array:

array ( 0 => 'ABC', 1 => 'DEF', 2 => 'GHI', 
        3 => 'JKL', 4 => 'MNO', 5 => 'PQR', 
        6 => 'STU', 7 => 'VWX', )

This works for the example given in the question. If you want to have every possible combination of those 24 letters, Artefacto's answer makes more sense.

Brian
  • 541
  • 3
  • 15
0
$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$c = strlen($alphabet);
$result = array();

for ($i = 0; $i < $c; ++$i) {
    $current0 = $i;
    for ($j = 0; $j < $c; ++$j) {
        if ($current0 == $j) continue;
        $current1 = $j;
        for ($k = 0; $k < $c; ++$k) {
            if (isset($current0 == $k || $current1 == $k)) continue;
            $result[] = $alphabet[$i].$alphabet[$j].$alphabet[$k];
        }
    }
}

Hope I understood your question right. This one iterates over the alphabet in three loops and always skips the characters which are already used. Then I push the result to $result.

But better try the script with only five letters ;) Using alls strlen($alphabet) (don't wanna count now...) will need incredibly much memory.

(I am sure there is some hacky version which is faster than that, but this is most straightforward I think.)

NikiC
  • 100,734
  • 37
  • 191
  • 225
  • This function not good if I want 4 characters or 12 characters set because "$alphabet[$i].$alphabet[$j].$alphabet[$k]" – B11002 Jul 10 '10 at 15:26
  • sorry, thought you wanted to have triples of unique characters – NikiC Jul 10 '10 at 15:30
0

There's a 1:1 relationship between the permutations of the letters of the alphabet and your sets lists. Basically, once you have a permutation of the alphabet, you just have to call array_chunk to get the sets.

Now, 24! of anything (that is 620448401733239439360000) will never fit in memory (be it RAM or disk), so the best you can do is to generate a number n between 1 and 24! (the permutation number) and then generate such permutation. For this last step, see for example Generation of permutations following Lehmer and Howell and the papers there cited.

Artefacto
  • 96,375
  • 17
  • 202
  • 225
  • array_chunk only for the first case. I don't care memory problem. We can reduce 24 to 6 characters. Can you explain "generate a number n between 1 and 24! (the permutation number)" in php function? – B11002 Jul 10 '10 at 15:37
  • @B11 If you can reduce to 6 characters, take a look at [`mt_rand`](http://www.php.net/mt_rand). – Artefacto Jul 10 '10 at 15:59
  • @B11 By the way, if you just want to generate all the permutations, there's are easier ways to do this... See e.g. [here](http://www.merriampark.com/perm.htm) for a listing in Java. – Artefacto Jul 10 '10 at 16:01
  • I feel like the question was actually asking about splitting the string into 3-character chunks ("ABC","DEF"), keeping the same order. Not finding every permutation those characters can take on. – Brian Feb 18 '15 at 19:32