Every permutation of the alphabet up to 29 characters?

后端 未结 13 1346
攒了一身酷
攒了一身酷 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 .', ';
    

提交回复
热议问题