SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Member
    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

  2. #2
    SitePoint Wizard wheeler's Avatar
    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

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wheeler View Post
    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.
    Yes, pass in your argument(s) as an array.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  4. #4
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by byron3@earthlink View Post
    Yes, pass in your argument(s) as an array.
    ah yes, good point
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  5. #5
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,198
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by austinplatt
    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.
    Sounds odd that the code should take a half minute to run. Most probably your code can be optimised.

    In this case I guess I would just have created a recursive function, which would stop when it reached the set goal.

    Quote Originally Posted by wheeler View Post
    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.
    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()

  6. #6
    SitePoint Member
    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

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by austinplatt View Post
    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.
    This is called "permutations" in maths, there are several well-known algorithms to generate them, google for "permutations alogrithms" or similar.

    If you're on the hunt for the sample code, check my sig, I've just posted some.

  8. #8
    Fully Sweet Car noddy's Avatar
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •