SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    960
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Generating all possible combinations of English Alphabet

    Hello,

    i want to generate all possible combinations of English alphabets in one click.

    is it possible to do this

  2. #2
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,516
    Mentioned
    51 Post(s)
    Tagged
    1 Thread(s)
    What exactly are you trying to accomplish?

    Are you trying to validate form submission input data?
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  3. #3
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    960
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Force Flow View Post
    What exactly are you trying to accomplish?

    Are you trying to validate form submission input data?
    actually telling,
    my boss wanted this.
    he sad i want to generate a list of combinations if i give for example c, x, m, t, y, then it should show all combinations.
    but the length is not limited to.
    no idea what he going to do with this.

  4. #4
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    960
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    after google for some time i came a cross this and seems i could get it to work

    http://pyrus.sourceforge.net/Math_Combinatorics.html

  5. #5
    SitePoint Evangelist bronze trophy
    Join Date
    Feb 2013
    Posts
    504
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    I found an example at stackoverflow.com
    I added the trim section at the top,building a new array $letters to remove extra space from each letter.
    Assuming $string is a comma separated list from POST or another source.

    PHP Code:
    <?php
    $string 
    "c, x, m, t, y";

    $l explode(",",$string);
    $letters = array();
    foreach(
    $l as $letter){
        
    $letters[] = trim($letter);
    }

    function 
    everyCombination($array) {
        
    $arrayCount      count($array);
        
    $maxCombinations pow($arrayCount$arrayCount);
        
    $returnArray     = array();
        
    $conversionArray = array();
            foreach (
    $array as $key => $value) {
                
    $conversionArray[base_convert($key10$arrayCount)] = $value;
            }
            for (
    $i 0$i $maxCombinations$i++) {
                
    $combination base_convert($i10$arrayCount);
                
    $combination str_pad($combination$arrayCount"0"STR_PAD_LEFT);
                
    $returnArray[] = strtr($combination$conversionArray);
            }
        return 
    $returnArray;
    }

    echo 
    "<pre>";
    print_r(everyCombination($letters));
    echo 
    "</pre>";
    ?>
    Your "c, x, m, t, y" example comes up with 3125 combinations.

  6. #6
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    960
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Drummin View Post
    I found an example at stackoverflow.com
    I added the trim section at the top,building a new array $letters to remove extra space from each letter.
    Assuming $string is a comma separated list from POST or another source.

    PHP Code:
    <?php
    $string 
    "c, x, m, t, y";

    $l explode(",",$string);
    $letters = array();
    foreach(
    $l as $letter){
        
    $letters[] = trim($letter);
    }

    function 
    everyCombination($array) {
        
    $arrayCount      count($array);
        
    $maxCombinations pow($arrayCount$arrayCount);
        
    $returnArray     = array();
        
    $conversionArray = array();
            foreach (
    $array as $key => $value) {
                
    $conversionArray[base_convert($key10$arrayCount)] = $value;
            }
            for (
    $i 0$i $maxCombinations$i++) {
                
    $combination base_convert($i10$arrayCount);
                
    $combination str_pad($combination$arrayCount"0"STR_PAD_LEFT);
                
    $returnArray[] = strtr($combination$conversionArray);
            }
        return 
    $returnArray;
    }

    echo 
    "<pre>";
    print_r(everyCombination($letters));
    echo 
    "</pre>";
    ?>
    Your "c, x, m, t, y" example comes up with 3125 combinations.
    Thank you!!! was really helpful.


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
  •