Every permutation of the alphabet up to 29 characters?

后端 未结 13 1320
攒了一身酷
攒了一身酷 2021-02-11 01:23

I\'m attempting to write a program that will generate a text file with every possible permutation of the alphabet from one character up to twenty-nine characters. I\'ve chosen 2

相关标签:
13条回答
  • 2021-02-11 02:00
    function p($length, $partial)
    {
          if ($length == 0) return $partial;
          $ans = array();
          foreach (range('a', 'z') as $i)
          {
              $ans[] = p($length -1, $partial . $i);
          }
          return $ans;  
    }
    
    $top = 3;
    //$f = fopen('out.txt');
    for ($l = 1; $l < $top+1; $l++)
    {
         print_r(p($l), '');
         //fwrite($p($l), '');
    }
    

    If you want to set $top to 29 and give it a try go ahead. I'm not going to.

    EDIT - print_r(p($l), ''); ---> print_r(p($l, ''));

    PHP keeps impressing me with its tolerance for mistakes. Missing a 'required' argument to my p? no problem itll just be empty string somehow (or zero, or false, situation depending). Second '' argument to print_r? no difference, gets treated like the default false anyway

    EDIT

    I don't know what the hell I was doing here. The different return types of p are quite odd, and will return a compound array with a weird structure.

    This is a far better solution anyway

    $lengthDesired = 29;
    for($i='a'; $i != str_pad('',$lengthDesired+1,'a'); $i++)
        echo $i .', ';
    
    0 讨论(0)
  • 2021-02-11 02:00
    public class hii {  
    
    public static void main(String[] args){
    
        String[] database = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
    
        for(int i=1; i<=database.length; i++){
            String[] result = getAllLists(database, i);
            for(int j=0; j<result.length; j++){
                System.out.println(result[j]);
            }
        }
    
    
    
    }
    
    
        public static String[] getAllLists(String[] elements, int lengthOfList)
        {
            //initialize our returned list with the number of elements calculated above
            String[] allLists = new String[(int)Math.pow(elements.length, lengthOfList)];
    
            //lists of length 1 are just the original elements
            if(lengthOfList == 1) return elements; 
            else {
                //the recursion--get all lists of length 3, length 2, all the way up to 1
                String[] allSublists = getAllLists(elements, lengthOfList - 1);
    
                //append the sublists to each element
                int arrayIndex = 0;
    
                for(int i = 0; i < elements.length; i++){
                    for(int j = 0; j < allSublists.length; j++){
                        //add the newly appended combination to the list
                        allLists[arrayIndex] = elements[i] + allSublists[j];
                        arrayIndex++;
                    }
                }
                return allLists;
            }
        }
    
    
    
    
    
    
    }
    
    0 讨论(0)
  • 2021-02-11 02:01

    Using PHP's Perl-style character incrementing.

    set_time_limit(0);
    
    $perm = 'A';
    $endTest = str_repeat('Z',28).'A';
    while ($perm != $endTest) {
        echo $perm++,"\n";
    }
    

    Run the script from the command line so you don't hit a webserver timeout; then sit back and wait a few years for it to complete

    0 讨论(0)
  • 2021-02-11 02:03

    just off the top of my head (PHP).

    $index = 0;
    
    while(1) {
       $output_string = '';
       $base_26 = (string)base_convert($index, 10, 26);
       if (strlen($base_26) > 29) break;
       for ($i = 0; $i < strlen($base_26); $i++) {
          $output_string .= chr(65 + base_convert($base_26[$i], 26, 10));
       }
       $index++;
       echo $output_string;
    }
    
    0 讨论(0)
  • 2021-02-11 02:08
    void out_perms(std::string word) {
        std::vector<int> indexes(word.size());
        for (size_t i = 0; i < indexes.size(); ++i)
            indexes[i] = i;
        do {
            for (size_t i = 0; i < indexes.size(); ++i)
                std::cout << word[indexes[i]];
            std::cout << std::endl;
        } while (std::next_permutation(indexes.begin(), indexes.end()));
    }
    
    int main(int, char**) {
        out_perms("asdfg");
    }
    

    See http://codepad.org/6lQTPQrG for example output

    0 讨论(0)
  • 2021-02-11 02:11

    The word "permutation" usually means that each letter appears exactly once, so it would be impossible to generate any permutation with more than 26 letters. Anyway, since the number of generated strings is too big, you can use random strings instead (the following is C code):

    char s[30];
    int p;
    for (;;) // repeat forever: you cannot use a realistic iteration limit anyway
    {
        for (p = 0; p < 29; ++p)
            s[p] = 'a' + rand() % 26;
        s[29] = '\0';
        puts(s);
    }
    
    0 讨论(0)
提交回复
热议问题