SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast stoopid's Avatar
    Join Date
    Sep 2004
    Location
    Vilnius, Lithuania
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    All possible combination of given letters

    Ok, so this is what i want to do and i really need your help guys, because i'm new with php...

    So lets say i have 5 letters: A; B; C; D and E. and what i want a php script to do is to generate all possible combinations of those letters with no repeats. For example:

    ABCDE
    ECDBA
    CBAED
    ...

    and so on, until theres no combinations left (5*4*3*2=120 combinations)
    so if you can, please help..

  2. #2
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what you want to do is count in a different base (ie: decimal=base 10, hex=base 16, binary=base 2). so, build a ticker similar to an analog speedometer.

    take your variables (A, B, C, D, E) and put them into an array. count the array elements and construct a string of that length, starting with 0-0-0-0-0. next build a function or class that increments the 1st digit. when it reaches 4, it increments the next digit by one and returns itself to 0. the function/class will probably need to be recursive. have it terminate at 4-4-4-4-4


    by the way: props for the creative problem. it took me quite a while to figure out the best way to think about it

  3. #3
    SitePoint Enthusiast stoopid's Avatar
    Join Date
    Sep 2004
    Location
    Vilnius, Lithuania
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, there's a problem... you see i want that all letters would appear only once per combination. so letters combination:
    A B C D E is cool, but
    A A B C E is not cool
    so making speedometer would'nt work, because there would be such comninations like:
    0-0-0-0-0
    0-0-0-0-1 and so on...

  4. #4
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a class i've used before to do this, i can't remember who the original author was but the credit goes to them anyway
    PHP Code:
    <?php

    class stringPerms
    {
      var 
    $results = array();
      var 
    $theList = array();
      var 
    $aString '';

      function 
    stringPerms($aString){
        
    $this->theList $this->make_list($aString);
      }

      function 
    make_list($aString){
        
    $x = array();
        for(
    $counter 0$counter strlen($aString); $counter++){
          
    $x[$counter] = substr($aString$counter1);
        }
        return 
    $x;
      }

      function 
    get_combinations(){
        return 
    $this->get_all($this->theList);
      }

      function 
    get_all($ar$app ''){
        if (
    count($ar) > 0){
          
    $x 0;
          while (
    $x count($ar)){
            
    $tmp array_slice($ar,0,count($ar));
            
    array_splice($tmp$x1);
            
    $sub count($this->theList) - count($ar);
            
    $app substr($app,0,$sub) . $ar[$x];
            
    $this->get_all(array_slice($tmp,0,count($tmp)), $app);
            
    $x $x 1;
          }
          if (
    count($ar) == 1){
            
    array_push($this->results$app);
            
    $app '';
          }
        }
        if (
    count($this->results) == $this->getResults()){
          return 
    $this->results;
        }
      }

      function 
    getResults(){
        return 
    $this->factorial(count($this->theList));
      }

      function 
    factorial($x){
        
    $f 1;
        while(
    $x 0){
          
    $f $f*$x;
          
    $x $x 1;
        }
        return 
    $f;
      }
      
    }


    //example useage
    $myString = new stringPerms('ABCDE');
    $perms $myString->get_combinations();
    foreach(
    $perms as $key => $value){
      echo 
    $key+.": $value<br>";
    }

    ?>

  5. #5
    SitePoint Enthusiast stoopid's Avatar
    Join Date
    Sep 2004
    Location
    Vilnius, Lithuania
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, that's IT! Thanks!

  6. #6
    Afraid I can't do that Dave Hal9k's Avatar
    Join Date
    Mar 2004
    Location
    East Anglia, England.
    Posts
    640
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On a side note, if you have a scientific calculator handy it should have an ! (exclamation mark) button which is the factorial function. It returns the number of combinations of how much discrete information you have. Like ABCDE would have 5, so that's 5 factorial (5!) which gives 120. You probably already knew that, but it might be interesting to someone else

  7. #7
    SitePoint Enthusiast stoopid's Avatar
    Join Date
    Sep 2004
    Location
    Vilnius, Lithuania
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, that's the only thing i know how to do so no problemo!


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
  •