OK.
I would probably still go with a counting approach.
abc can be represented as 012, so, for all 4-character permutations of abc, you could count in base 3 from 0 to 2222(base 3), and then do zero padding and string substitutions to output as aaaa - cccc


The only problems I can think of:
converting to/from base 3 (you might be able to write a simple algorithm for this)
handling large bases > base16 could be tricky.


Also, googling for permutation algorithm comes back with lots of results. Most are in C++, but conversion to PHP may be trivial. It looks like some are using loops where one character or position is swapped with another each time. (That is basically a counting algorithm.)

you could just stick your alphabet into an array, and then do the "counting" by stepping through the array, with carry...

Do you know how to count for any base? It's not hard, but I doubt I can explain it concisely...
If you can do that, you can solve this problem easily.


if not,
see the "accepted answer" at
<http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_10352883.html>
and convert to PHP.

It looks like the STRVector is similar to a standard PHP array.

Note this is a stateful algorithm that only outputs a few results at a time; you could modify it to output all results.
Be careful how you store the output, as it will get VERY big very quickly.


Barry Gould


At 05:28 PM 4/3/2003, you wrote:
Hi guys,

I appreciate your taking interest, but as I wrote, I am looking for a permutation algorithm, not specifically for all the hex colors. That was simply an example of a usage.

Put another way, I want a function that I can do:

echo generatePerms("abc",4);

and it will return

aaaa
aaab
aaac
aaba
aabb
aabc

and so on

It is for some mathematical R&D.

Thanks,

michael


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to