0

How to list all possible letter combinations (from an alphabet: "qwertyuiopasdfghjklzxcvbnm"), like:

q
w
e
r
t
y
u
o
p
s
d
f
g
h
j
k
l
x
c
v
b
n
m
qq
qw
qe
qr
qt
qu

up to 64 bit long strings? So the last one would be String(8):

mmmmmmmm
bijou
  • 49
  • 1
  • 1
  • 6
  • What have you got so far? (post your code) – Michael Robinson Feb 02 '11 at 12:06
  • 4
    What's wrong with your yesterdays code? – mario Feb 02 '11 at 12:07
  • I think you forgot the empty word `''`. – Ishtar Feb 02 '11 at 12:07
  • You do realise that there are 217,180,147,158 of them? – borrible Feb 02 '11 at 12:09
  • Oh, you mean 64 bit string, or presumably, 8 characters. – Kevin A. Naudé Feb 02 '11 at 12:10
  • look on this http://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n – Haim Evgi Feb 02 '11 at 12:14
  • 1
    Wait, how is this "Not a Real Question" ?? – shamittomar Feb 02 '11 at 12:16
  • It's not a real question, because it cannot be serious. This is a case of start your program running, then bequeath the result to your great grand children (who might still be alive before it finishes). – Mark Baker Feb 02 '11 at 12:30
  • Assuming 64 characters rather than 64 bits (else adjust the 65 to 9) $i = 'a'; while (strlen($i) < 65) { echo $i++,"\n"; } ... best run from the command line so you don't need to worry about browser timeout. If you want the qwerty sort, then build an array (rather than echo) and sort it when complete.... but make sure you have lots and lots and lots and lots of memory – Mark Baker Feb 02 '11 at 12:32
  • I have really fast machine available... – bijou Feb 02 '11 at 12:33
  • memory is not an issue, every 100 iterations results are saved & flushed – bijou Feb 02 '11 at 12:34
  • @bijou - I hope you have a quantum machine, otherwise don't hold your breath waiting else you'll asphyxiate before you have children, let alone great grand children – Mark Baker Feb 02 '11 at 12:34
  • who said I need the whole list? – bijou Feb 02 '11 at 12:40
  • 1
    If you can generate one per microsecond, generating 217,180,147,159 combinations (don't forget the empty string) would only take two and a half days to complete. If it took as long as a millisecond each, it would take a bit less than seven years. Hardly "bequeath to your great-grandchildren". – Joe White Feb 02 '11 at 13:25
  • 1
    @bijou: well, you did say you wanted to list all possible combinations. You can't do that without the whole list... – Chris Feb 02 '11 at 16:52
  • @Mark Baker, with all respect, I do agree that it will take days to execute it, but that does not mean that it's not a real question. This is correctly tagged as [[algorithm]] where similar things are discussed. – shamittomar Feb 02 '11 at 18:37
  • @shamittomar - If anybody understands what they are asking, and believes that this is a correct approach to their problem, then they don't really understand their problem... so they're not actually asking the right question. The only "valid" version of this question I've seen was an artist wanting to generate a book with all the possible permutations, as a piece of "concept art". – Mark Baker Feb 02 '11 at 18:43

1 Answers1

1
for($a=97; $a<123; $a++){
  for($b=97; $b<123; $a++){
    for($c=97; $c<123; $b++){
      for($d=97; $d<123; $a++){
        for($e=97; $e<123; $a++){
          for($f=97; $f<123; $a++){
            for($g=97; $g<123; $c++){
              for($h=97; $h<123; $d++){
                echo chr($a).chr($b).chr($c).chr($d).chr($e).chr($f).chr($g).chr($h);
}}}}}}}}

Haven't tried though. Its straight forward one. for short code use recursion or something like that. by the way this only shows 8 char long numbers so for shorter ones you need more code, but the idea is about the same.

JackLeo
  • 4,355
  • 8
  • 36
  • 66
  • You need to have 8 chr in the echo to make it work and 8 different loop counters like $a, $b $c, $d, $e, $f, $g, $h. – shamittomar Feb 02 '11 at 12:18
  • I think a recursive function would work best here that goes until the total amount of characters is used – Ryan Aug 21 '11 at 20:35
  • Yes it would :) At the time being it seemed as the fastest answer without much thinking and easy way to understand what goes behind scene. – JackLeo Aug 22 '11 at 07:53