[PHP] Re: Generate every possible combination
Evan Nemerson wrote: I need to generate every possible combination of the the values in an array. For example, if... $array = Array(A, B, C); I really have no idea where to begin. The best lead I can think of is that there are going to be n! elements in the output array, where n is the size of the input array. Correct, this also implies that using more than say 20 elements (20!) will take *loads* of CPU time and memory. Anyway, I did such a program once to test the performance of VB versus Java but alas I dont keep it around. It was recursive tho. A quick check on google came up with some code here: http://www.delphiforfun.org/Programs/Permutes_1.htm and som theory here: http://www.theory-of-evolution.org/Main/chap4/permutations_7.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Generate every possible combination
Evan Nemerson wrote: I need to generate every possible combination of the the values in an array. For example, if... $array = Array(A, B, C); I want to be able to do something like print_r(magic_function($array)); which would output Array ( [0] = ABC [1] = ACB [2] = BAC [3] = BCA [4] = CAB [5] = CBA ) I really have no idea where to begin. The best lead I can think of is that there are going to be n! elements in the output array, where n is the size of the input array. Any help would be greatly appreciated. Evan for ($x=ord(A);$x=ord(Z);$x++) { for($y=ord(A);$y=ord(Z);$y++) { for($z=ord(A);$z=ord(Z);$z++) { $array[]=chr($x).chr($y).char($z); } } } is a quick suggestion. Haven't tested it. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Generate every possible combination
Austin Marshall wrote: Evan Nemerson wrote: I need to generate every possible combination of the the values in an array. For example, if... $array = Array(A, B, C); I want to be able to do something like print_r(magic_function($array)); which would output Array ( [0] = ABC [1] = ACB [2] = BAC [3] = BCA [4] = CAB [5] = CBA ) I really have no idea where to begin. The best lead I can think of is that there are going to be n! elements in the output array, where n is the size of the input array. Any help would be greatly appreciated. Evan for ($x=ord(A);$x=ord(Z);$x++) { for($y=ord(A);$y=ord(Z);$y++) { for($z=ord(A);$z=ord(Z);$z++) { $array[]=chr($x).chr($y).char($z); } } } is a quick suggestion. Haven't tested it. never mind, wasn't paying attention. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php