5

Possible Duplicate:
Generate all combinations of arbitrary alphabet up to arbitrary length

I'm trying to make write all possible words of 10 letters(zzzzzzzzzz) in php. How can I do that? it will look like that : http://i.imgur.com/sgUnL.png

I tried some ways to it but they are only making 10 letters randomly not increasing from 1 letter. By the way execution time and how it's big is not problem. i just need to algorithm for it, if somebody show it with code it'll be more helpful of course..

Community
  • 1
  • 1
xecute
  • 61
  • 3
  • 12
    26^10 possibilities = 141 167 095 653 376, so it might take a while. – Tim Cooper Sep 01 '11 at 12:38
  • @NullUserException it's not problem. – xecute Sep 01 '11 at 12:40
  • @xecute: What will that be good for? – Tomalak Sep 01 '11 at 12:42
  • @Tim Cooper not problem, I just wonder algorithm. I need to learn it. Maybe I'll use it with 5 letters(11million possibility).. – xecute Sep 01 '11 at 12:43
  • 3
    Re Tim's comment that it might take a while: Allowing one millisecond per string, I estimate that the program will take about 4000 years to run. (In reality, it would actually be a lot longer than that). – Spudley Sep 01 '11 at 12:46
  • 4
    Also, if you wanted to output the results to disk, allowing 10 bytes per string, you would need 12 terabytes of storage. Again, in reality it would be more because you'd need some kind of structure to the file, but I'm trying to point out the scale of the question you've asked. – Spudley Sep 01 '11 at 12:50
  • 1
    possible duplicate of [Generate all combinations of arbitrary alphabet up to arbitrary length](http://stackoverflow.com/questions/2380962/generate-all-combinations-of-arbitrary-alphabet-up-to-arbitrary-length), [Algorithm to return all combinations of k elements from n](http://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n), [Combinations, Dispositions and Permutations in PHP](http://stackoverflow.com/questions/1679605/combinations-dispositions-and-permutations-in-php) – outis Sep 01 '11 at 12:56
  • @Tomalak no it's not. I just really stuck in my mind with this question on php. – xecute Sep 01 '11 at 13:00
  • @outis: Actually, the first of your links is a quite better duplicate than the second one (which asks for something else). (The third one is only a link.) I fixed the link here. – Paŭlo Ebermann Sep 01 '11 at 14:04

3 Answers3

7
function words($length, $prefix='') {
    if ($length == 0) return;
    foreach(range('a', 'z') as $letter) {
        echo $prefix . $letter, "\n";
        words($length-1, $prefix . $letter);
    }
}

Usage:

words(10);

Try it here: http://codepad.org/zdTGLtjY (with words up to 3 letters)

Arnaud Le Blanc
  • 90,979
  • 22
  • 192
  • 188
6

Version 1:

for($s = 'a'; $s <= 'zzzzzzzzzz'; print $s++.PHP_EOL);

as noted by Paul in comments below, this will only go to zzzzzzzzyz. A bit slower (if anyone cares) but correct version would be:

//modified to include arnaud576875's method of checking exit condition
for($s = 'a'; !isset($s[10]); print $s++.PHP_EOL);
Mchl
  • 58,281
  • 9
  • 105
  • 116
  • impressive.. that's really performanceful. @arnaud576875's answer is function in function. this is really simple and performanceful answer, thank you Mchl.. – xecute Sep 01 '11 at 13:07
  • According to OP's question, `$s = 'aaaaaaaaaa'` is probably what he wants. +1 Nonetheless for the actual best answer. – netcoder Sep 01 '11 at 13:07
  • 2
    Neither of these do what you might expect. zz gives a list that goes to zyz and aaaaaaaaaa gives only a. – Paul Sep 01 '11 at 13:31
  • 2
    The second one 4 times faster with `!isset($s[10])` instead of `strlen($s) <= 10` (if anyone cares) – Arnaud Le Blanc Sep 01 '11 at 16:36
0
 <?php

function makeWord($length, $prefix='')
{
   if ($length <= 0)
   {
      echo $prefix . "\n";
      return;
   }

   foreach(range('a', 'z') as $letter)
   {
      makeWord($length - 1, $prefix . $letter);
   }
}    

// Use the function to write the words.
$minSize = 1;
$maxSize = 3;

for ($i = $minSize; $i <= $maxSize; $i++)
{
   makeWord($i);
}

?>
Paul
  • 5,603
  • 1
  • 36
  • 49