SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: Trying all combinations
-
Aug 15, 2007, 05:07 #1
- Join Date
- May 2005
- Posts
- 23
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Trying all combinations
Hi, this is really a general programming question, I have come across it a few times in my work and not really known how to handle it. I have normally ended up just coding it manually (putting in all the combinations), but I guess there must be a better way.
Say I have an array, (0, 1, 2, 3, 4, 5) and I want to put them into a function called do_something($a, $b, $c, $d, $e, $f) but I want this function to be called for every combination of numbers so do_something(0, 1, 2, 3, 4, 5), do_something(0, 1, 2, 3, 5, 4) etc.
I guess you could do it with for loops but when dealing with say 9 variables, it would be slow and I guess not the most efficient way to do it.
Any ideas on how to go about this?
Many thanks
-
Aug 15, 2007, 05:52 #2
- Join Date
- Mar 2006
- Location
- Gold Coast, Australia
- Posts
- 1,369
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
well... slow in the context of.... 0.003 seconds? all depends on what your function actually does.... but if your question is, can I put an undefined number of variables into a function, then to my knowledge the answer is no... you have two options - make each function variable required, or give it a default value where you don't want to necessarily provide a unique value every time.
perhaps if you copy your function in then someone could give a better answer but I can't really think how to word it any better....Studiotime - Time Management for Web Developers
to-do's, messages, invoicing, reporting - 30 day free trial!
Thomas Multimedia Web Development
-
Aug 15, 2007, 06:24 #3
- Join Date
- May 2005
- Posts
- 23
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
well, say I wanted to generate all the 6 digit pandigital numbers (eg 251364, 561342 etc) , then i have been previously been doing this with 6 for loops, this takes at least half a minute to run, and it's not very nice to code either - very messy.
If I could have an array of (1, 2, 3, 4, 5, 6) and then just something which could make all the different combinations of these.
so to put this into some kind of code as an example:
Code PHP:<? $numbers = array(1, 2, 3, 4, 5, 6); //some kind of loop to do each combination echo $next_combination . ", "; ?>
the above should then echo "123456, 123465, etc" or something along those lines.
Sorry if I'm not explaining very well.
Thanks again
-
Aug 15, 2007, 07:32 #4
- Join Date
- Aug 2005
- Posts
- 453
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Aug 15, 2007, 16:46 #5
- Join Date
- Mar 2006
- Location
- Gold Coast, Australia
- Posts
- 1,369
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Studiotime - Time Management for Web Developers
to-do's, messages, invoicing, reporting - 30 day free trial!
Thomas Multimedia Web Development
-
Aug 15, 2007, 17:57 #6
- Join Date
- Sep 2004
- Location
- Norway
- Posts
- 1,198
- Mentioned
- 4 Post(s)
- Tagged
- 1 Thread(s)
Originally Posted by austinplatt
In this case I guess I would just have created a recursive function, which would stop when it reached the set goal.
Actually, you can pass along a changing number of arguments to an function if you want. These functions allows you to do that func_num_args(), func_get_arg(), func_get_args()
-
Aug 16, 2007, 04:01 #7
- Join Date
- Apr 2004
- Location
- germany
- Posts
- 4,324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Aug 16, 2007, 04:23 #8
- Join Date
- Aug 2002
- Location
- Perth, Western Australia
- Posts
- 759
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This sounds similar to that of entering the numbers of a phone number in order to find what words it could form.
PHP shuffle function might help.
http://au2.php.net/manual/hk/function.shuffle.php
and range http://au.php.net/manual/hk/function.range.php
This is the shuffle example in action http://www.lionslair.net.au/test/shuffle.php
Some information on permutations
http://www.mathsrevision.net/alevel/...mbinations.php
Bookmarks