SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to solve this (probably recursively)?

    Say you have an array: array('a', array('b', 'c'), 'd', array('e', 'f');

    How can I get all possible combinations:

    abde
    abdf
    acde
    acdf

    Anyone know the solution?
    Thanks in advance.

    Edit: I should add that it needs to work with any possible array length.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    On PHP5?

    PHP Code:
    <?php

    class Permutations implements Iterator {

        protected 
    $a null;
        protected 
    $s null;
        protected 
    $pos 0;

        public function 
    __construct ($s) {

            if (
    is_array($s)) {

                
    $this->array_values($s);
                
    $this->range(0count($s) - 1);

            } else {

                
    $this->= (string)$s;
                
    $this->range(0strlen($this->s) - 1);

            }

        }

        public function 
    key() {

            return 
    $this->pos;

        }

        public function 
    current() {

            
    $r = array();

            foreach (
    $this->as $k) {
                
    $r[] = $this->s[$k];
            }

            return 
    is_array($this->s) ? $r implode(''$r);

        }

        public function 
    next() {

            if (
    $this->_next()) {
                
    $this->pos++;
            } else {
                
    $this->pos = -1;
            }

        }

        public function 
    rewind() {

            
    sort($this->a);
            
    $this->pos 0;

        }

        public function 
    valid() {

            return 
    $this->pos >= 0;

        }

        protected function 
    _next() {

            
    $n count($this->a);

            for (
    $i $n 2$this->a[$i] > $this->a[$i 1]; $i--) {
                if (!
    $i) {
                    return 
    false;
                }
            }

            for(
    $j $n 1$this->a[$i] > $this->a[$j]; $j--);

            
    $q $this->a[$j];
            
    $this->a[$j] = $this->a[$i];
            
    $this->a[$i] = $q;

            while (++
    $i < --$n) {
                
    $q $this->a[$n];
                
    $this->a[$n] = $this->a[$i];
                
    $this->a[$i] = $q;
            }

            return 
    true;

        }

    }
    Use:
    PHP Code:
    foreach (new Permutations('qwerty') as $k => $v) {
        echo 
    $v" <br> \n";

    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, PHP4. But this class doesn't do exactly what I wanted; If you look at my input, it's an array:

    array('a', array('b', 'c'), 'd', array('e', 'f');

    'a' and 'd' never change.
    It's only array('b', 'c') and array('e', 'f') that can change.

    Thanks for the help

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bartb View Post
    But this class doesn't do exactly what I wanted; If you look at my input...
    That is the part for you to figure out. I do not give every answer otherwise what are you learning?

    http://www.tagarga.com/blok/on/070816
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not exactly permutations.

    I don't want edba etc.


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
  •